Skip to main content

Spiff up your dataclasses with extra features.

Project description

PyPI - Version GitHub Actions Workflow Status Coverage Status Read the Docs License: MIT pre-commit

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

from fancy_dataclass import JSONDataclass

@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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fancy_dataclass-0.10.1.tar.gz (323.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fancy_dataclass-0.10.1-py3-none-any.whl (83.7 kB view details)

Uploaded Python 3

File details

Details for the file fancy_dataclass-0.10.1.tar.gz.

File metadata

  • Download URL: fancy_dataclass-0.10.1.tar.gz
  • Upload date:
  • Size: 323.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for fancy_dataclass-0.10.1.tar.gz
Algorithm Hash digest
SHA256 206d8cf20425ececc4523ad665fb8e0e1663a0540899656a0e4c617b58d08798
MD5 ae1777aa0a14ad22b47c33fae73f3664
BLAKE2b-256 5a602f1cf5b12389d45665e721ac6abb472c20e46852c7277d76a545c4d9f3ac

See more details on using hashes here.

File details

Details for the file fancy_dataclass-0.10.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fancy_dataclass-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4025db30e7b24f08b40a4bf0a6841f819d1fe9cf5ff4b321ca4dd86deb61f42c
MD5 1ed69bcd41e6063de9de1d77eae307ff
BLAKE2b-256 d89f58b98c4b91eb0a11785bd43e946313502e9f44f1753ebd3d96bceb0cc38c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page