Skip to main content

Convert heirarchies of dicts/list/sets/values (JSON?) to heirarchies of type annotated dataclasses.

Project description

dictaclass - dicts to dataclasses

Have you ever been handed an "example JSON" and wanted to have it in an intellisense-friendly structure? Just define the JSON structure as a hierarchy of dataclasses and run to_dataclass(MyJsonDatatclass, json.loads(source))

Well, the library works with whatever source of data you have, as long as you can represent the way json.loads does - dicts, lists, and values.

Features

  • Supports deeply nested dataclasses (duh)
  • Supports inheritance
  • Supports collections
    • list
    • set
    • dict
  • Supports frozen dataclasses

Limitations

  • Requires Python 3.7+
  • Cannot guess types.
  • Cannot use mixed types.
  • Cannot use Union[].
  • Cannot use Tuple[].

Installation

py -m pip install dictaclass

or

python3 -m pip install dictaclass

Example

from typing import Set

from dataclasses import dataclass
from dictaclass import to_dataclass

import json

@dataclass(frozen=True)
class Pair:
    first: str
    last: str

@dataclass(frozen=True)
class PairPair:
    a: Pair

@dataclass(frozen=True)
class Object:
    pairs: Set[PairPair]

v = to_dataclass(
    Object,
    {
        "pairs": [
            {"a": {"first": "f0", "last": "l0"}},
            {"a": {"first": "f1", "last": "l1"}},
        ]
    },
)
# or
v = to_dataclass(
    Object,
    json.loads(
        """
        {
            "pairs": [
                {"a": {"first": "f0", "last": "l0"}},
                {"a": {"first": "f1", "last": "l1"}}
            ]
        }
        """
    ),
)

assert isinstance(v, Object)
assert isinstance(v.pairs, set) # it was a list in the JSON
assert len(v.pairs) == 2
assert v.pairs == {
    PairPair(Pair("f0", "l0")),
    PairPair(Pair("f1", "l1"))
}

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

dictaclass-0.5.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

dictaclass-0.5.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file dictaclass-0.5.0.tar.gz.

File metadata

  • Download URL: dictaclass-0.5.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for dictaclass-0.5.0.tar.gz
Algorithm Hash digest
SHA256 3038511f155e844ef22436e419ef36d6fdb6f7b0a052c07373858f2b03a59aaa
MD5 9628f2bc43ec16bd15e00c362ed29355
BLAKE2b-256 3d879b222bdbb2a7a91319766d1f249d273ae3d1dedd28a3599a9b35ed94dac6

See more details on using hashes here.

File details

Details for the file dictaclass-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: dictaclass-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for dictaclass-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f5baaf2de43de1e73ffb6700e58252c9c2a28d24f079a50f04206d5396695a9
MD5 0afeed3899b1a2430174c6711e58a66f
BLAKE2b-256 5fcd69bb335d14efdadb444a091bd0761c2d76dab3d4bf6570ca1cc132d2b34e

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