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 - dict
s, list
s, 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.0.2.tar.gz
(7.6 kB
view hashes)
Built Distribution
Close
Hashes for dictaclass-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 993bafc4dccf6b7b6c5b7810e982defa444f45392cde2c640e1a62255abb3f7b |
|
MD5 | 18ca06da614e2eda609c1f712c120a7a |
|
BLAKE2b-256 | c49a8e723ae514a1512afdbce0bbf4dd08e7d8e38821660644636d6d58d68296 |