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 parsersargument 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
isa 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)
Metadata
Release files for dict2any 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| dict2any-0.0.2.tar.gz | 9.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dict2any-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.7 kB
Release files / dict2any-0.0.2.tar.gz
| Download URL | dict2any-0.0.2.tar.gz |
|---|---|
| Size | 9.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
35717e16dd7ca3fb2cb9a78367202c809f4000db5ed8389a5156005b0803cafc
|
|
BLAKE2b-256 checksum How to use checksums |
da03a1ec7a410af9b5d6fe7292d23b910dc8d0ef27aa754bfd97665d2ac08bff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Release files / dict2any-0.0.2-py3-none-any.whl
| Download URL | dict2any-0.0.2-py3-none-any.whl |
|---|---|
| Size | 12.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
71d5573b373bea723e0c1383a0e70edc6266db88eb5ad5f59d41e15b0f32b464
|
|
BLAKE2b-256 checksum How to use checksums |
d96dbdaa37f9af9f80633eaca215338d061fc9f59c32b4b4c986f93a640cd75f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Security for dict2any 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.