dict to/from dataclasses
Project description
dict2any
Problem: You need to initialize a nested data structure (such as a dataclass containing another dataclass)
Solution: use dict2any.parse(MyClass, data)
and it will recursively build up your data structure using type hints.
Usage
from dataclasses import dataclass
from dict2any import parse
@dataclass
class Package:
name: str
@dataclass
class Config:
packages: list[Package]
config = parse(Config, {"packages": [{"name": "wow"}, {"name": "amazing"}]})
print(config)
# Config(packages=[Package(name='wow'), Package(name='amazing')])
Advanced usage
Custom Parsing
The parsers
argument lets you pass in additional parsers, to further customize or extend the functionality.
To do so, you must implement the Parser protocol. There are two methods: can_parse
, and parse
which you should implement. Take a look at any of the builtin parsers for inspiration
Implementing can_parse
The first parser which returns True
to can_parse
is the one that wins, and will parse that data type. Instead of a strict ordering (e.g try the DictParser, then the ListParser, etc etc.), the algorithm works in stages. It is expected that only one parser will match for a data type for a given stage. The stages are as follows:
- Override: This stage is checked first. No builtin parsers use this stage. Instead, it's an opportunity for any custom parsers to "win" and be the one chosen to parse a data type
- Exact: This parser knows how to parse this exact data type. For example, a certain class, or when an object
is
a certain type - Fallback: The parser is a little more generic, and handles subclasses or other coerceable classes.
- LastChance: Generically try to handle all classes
For example, the DictParser can_parse the following types in Stage.Exact
:
- dict
- OrderedDict
- dict[str, str]
and it can additionally parse the following types in Stage.Fallback
:
- TypedDict
- UserDict
- class MyDict(dict)
- Any class which implements collections.abc.Mapping or collections.abc.MutableMapping
Alternative solutions
- pydantic (tons of features)
- dataclass-wizard (only for dataclasses)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dict2any-0.0.2.tar.gz
.
File metadata
- Download URL: dict2any-0.0.2.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35717e16dd7ca3fb2cb9a78367202c809f4000db5ed8389a5156005b0803cafc |
|
MD5 | 7d7a802c65009380e94846df97e231cc |
|
BLAKE2b-256 | da03a1ec7a410af9b5d6fe7292d23b910dc8d0ef27aa754bfd97665d2ac08bff |
File details
Details for the file dict2any-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: dict2any-0.0.2-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71d5573b373bea723e0c1383a0e70edc6266db88eb5ad5f59d41e15b0f32b464 |
|
MD5 | 49aa37d9c88e862a2afe9f04d5194b90 |
|
BLAKE2b-256 | d96dbdaa37f9af9f80633eaca215338d061fc9f59c32b4b4c986f93a640cd75f |