Skip to main content

Deeply allow you to make your dataclasses more functional

Project description

Using this package you can do a deep conversion of your dataclass to a dictionary and initialize it from dict.

Getting it

To download deeply, either fork this github repo or simply use PyPI via pip.

$ pip install deeply

Usage

1 - Import

from deeply import Deeply
from dataclasses import dataclass

2 - Define your data classes with the following inheritance:

@dataclass
class A(Deeply):
    a1: int
    a2: int


@dataclass
class B(Deeply):
    b1: str
    b2: str

@dataclass
class C(Deeply):
    a: A
    b: B

3 - Create an instance of the class and you can already use it:

c = C(A(1, 2), B('foo', 'bar'))

c.to_web()
# {'a': {'a1': 1, 'a2': 2}, 'b': {'b1': 'foo', 'b2': 'bar'}}

4 - You can also initialize your class from a dictionary:

c = C.init_from_dict({
    'a': {
        'a1': 1,
        'a2': 2
    },
    'b': {
        'b1': 'foo',
        'b2': 'bar'
    }
})

# C(a=A(a1=1, a2=2), b=B(b1='foo', b2='bar'))

5 - And finally: you can set your own conversion rules in deep dict.

Deeply.rules: List[Tuple[Callable[[T], bool], Callable[[T], Any]]] = [
        (lambda obj: hasattr(obj, 'isoformat'), lambda obj: obj.isoformat()),
    ]

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

deeply-1.0.3.tar.gz (2.4 kB view hashes)

Uploaded Source

Supported by

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