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.9 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.11.0.tar.gz (324.1 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.11.0-py3-none-any.whl (83.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fancy_dataclass-0.11.0.tar.gz
Algorithm Hash digest
SHA256 936ecba753855d7727256d6584c6c3106a6be96e80eaa96686aed264eaaf834b
MD5 e4a61eb67de8fbd1e1d07766a9a3f2e6
BLAKE2b-256 782d0109341652556255d78c8cad881308089d32c7d7f3089210cf334e221da8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fancy_dataclass-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca2d15f2a2f8e143fa5f16ab005d7e0cd42bc33f3274a7279ccdf23d580bdad9
MD5 5b9ae1c6854fed318d8a76d8b7b3be80
BLAKE2b-256 c1874e6950610b23f093fb8d017b2d21b66e413f589f302c9f13afd3d8d0c19c

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