Skip to main content

Additional functionality for Python dataclasses

Project description

dataclass-extensions

Additional functionality for Python dataclasses

Installation

Python 3.10 or newer is required. You can install the package from PyPI:

pip install dataclass-extensions

Features

Encode/decode to/from JSON-safe dictionaries

from dataclasses import dataclass
from dataclass_extensions import decode, encode


@dataclass
class Fruit:
    calories: int
    price: float

@dataclass
class FruitBasket:
    fruit: Fruit
    count: int

basket = FruitBasket(fruit=Fruit(calories=200, price=1.0), count=2)
assert encode(basket) == {"fruit": {"calories": 200, "price": 1.0}, "count": 2}
assert decode(FruitBasket, encode(basket)) == basket

You can also define how to encode/decode non-dataclass types:

from dataclasses import dataclass
from dataclass_extensions import decode, encode


class Foo:
    def __init__(self, x: int):
        self.x = x

@dataclass
class Bar:
    foo: Foo

encode.register_encoder(lambda foo: {"x": foo.x}, Foo)
decode.register_decoder(lambda d: Foo(d["x"]), Foo)

bar = Bar(foo=Foo(10))
assert encode(bar) == {"foo": {"x": 10}}
assert decode(Bar, encode(bar)) == bar

Polymorphism through registrable subclasses

from dataclasses import dataclass
from dataclass_extensions import Registrable, decode, encode


@dataclass
class Fruit(Registrable):
    calories: int
    price: float

@Fruit.register("banana")
@dataclass
class Banana(Fruit):
    calories: int = 200
    price: float = 1.25

@Fruit.register("apple")
@dataclass
class Apple(Fruit):
    calories: int = 150
    price: float = 1.50
    variety: str = "Granny Smith"

@dataclass
class FruitBasket:
    fruit: Fruit
    count: int

basket = FruitBasket(fruit=Apple(), count=2)
assert encode(basket) == {
    "fruit": {
        "type": "apple",  # corresponds to the registered name
        "calories": 150,
        "price": 1.5,
        "variety": "Granny Smith",
    },
    "count": 2,
}
assert decode(FruitBasket, encode(basket)) == basket

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

dataclass_extensions-0.2.12.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

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

dataclass_extensions-0.2.12-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file dataclass_extensions-0.2.12.tar.gz.

File metadata

  • Download URL: dataclass_extensions-0.2.12.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for dataclass_extensions-0.2.12.tar.gz
Algorithm Hash digest
SHA256 7d07af1d273b19a47aaa9ee4bc3c4266dcd22c9d6908ec0057edf829d1aeabb6
MD5 608b20e6d6962e565a5a0882e5fdff23
BLAKE2b-256 f415949915ee97a4c02a1161cd0a34f3c0022996b1e6b6c123cd974aa2679cac

See more details on using hashes here.

File details

Details for the file dataclass_extensions-0.2.12-py3-none-any.whl.

File metadata

File hashes

Hashes for dataclass_extensions-0.2.12-py3-none-any.whl
Algorithm Hash digest
SHA256 5976a52b117aeb4044626f718696384c8191b9b42804d0871b74db374f8d59b8
MD5 f47c05e386aafa0f084ac643e2d5474d
BLAKE2b-256 5b6aab3cbe301675777247bd9fe52963cdeb612f991b783ab2cb2182061e3d32

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