Spiff up your dataclasses with extra features.
Project description
Basics
🤵🏻♂️ Fancy Dataclass: A library to spiff up your dataclasses with extra features.
Introduction
Python 3.7 introduced the dataclasses
module which lets you write "statically typed" classes using the type hinting mechanism.
By inspecting dataclasses' type annotations, it is possible to endow them with special powers that help cut down on boilerplate code in a wide variety of domains, such as:
- JSON/TOML conversion: convert dataclasses to JSON/TOML files and vice versa
- Configuration management: store global configurations and use them anywhere in your program
- SQL persistence: define SQL tables, and save/load objects from a database
- CLI parsing: parse command-line arguments and store their values in a dataclass, then use them to execute your main program logic
- Subprocess calls: generate command-line arguments to be passed to another program
fancy_dataclass
borrows ideas from other excellent libraries such as marshmallow
, pydantic
, and argparse_dataclass
, while aiming to be as lightweight as possible in terms of its dependencies and learning curve.
How to install
pip install fancy-dataclass
Requires Python 3.8 or higher.
Example
Regular dataclass
@dataclass
class Person:
name: str
age: int
height: float
hobbies: list[str]
Fancy dataclass
@dataclass
class Person(JSONDataclass):
name: str
age: int
height: float
hobbies: list[str]
Usage:
>>> person = Person(
name='John Doe',
age=47,
height=71.5,
hobbies=['reading', 'juggling', 'cycling']
)
>>> print(person.to_json_string(indent=2))
{
"name": "John Doe",
"age": 47,
"height": 71.5,
"hobbies": [
"reading",
"juggling",
"cycling"
]
}
Documentation
Read the official documentation here.
The documentation is made with Material for MkDocs and is hosted by Read the Docs.
View the Changelog here.
License
This library is distributed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fancy_dataclass-0.7.3.tar.gz
.
File metadata
- Download URL: fancy_dataclass-0.7.3.tar.gz
- Upload date:
- Size: 545.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ef5a46724b45e54e08842251454e7ffad271ea9914851436455fe0b66c70f87 |
|
MD5 | 9ebc9df27a4298e3b9c4165fce34be50 |
|
BLAKE2b-256 | 06b2a292b77fa22ce7e2a470d500cafd7fffea92d12f721b3f2c1f6c823ae066 |
File details
Details for the file fancy_dataclass-0.7.3-py3-none-any.whl
.
File metadata
- Download URL: fancy_dataclass-0.7.3-py3-none-any.whl
- Upload date:
- Size: 354.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e5569addb7ba686a077760a5880e790068708a364fafd6677a4b9b0d3f9fbd1 |
|
MD5 | 0f3f311aeaa8fb99fc4767488e05220d |
|
BLAKE2b-256 | 01c3c0434dcc3717ba95e09c4418b16ed99c5cb22daef06591b24b401ce2cfad |