Convert dicts into dataclasses. Supports lists, dicts and unions.
Project description
Dict 2 Dataclass
This is a simple python script that converts a dictionary to a class. It is useful when you want to access the dictionary values as class attributes.
It performs type checking and will raise a ValueError if the dictionary does not match the class attributes.
Usage
from dict2dataclass import FromDict
from dataclasses import dataclass
@dataclass
class Address(FromDict):
street: str
city: str
state: str
@dataclass
class Person(FromDict):
name: str
age: int
address: Address
data = {
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Springfield",
"state": "IL"
}
}
person = Person.from_dict(data)
print(person.name) # John Doe
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dict2dataclass-0.2.0.tar.gz.
File metadata
- Download URL: dict2dataclass-0.2.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.12.8 Darwin/24.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bd84240c035f6738a46533c8f761f0c2390d4bfe2906f8139418ec767fb26df
|
|
| MD5 |
84b9855008d31220705174912d630e63
|
|
| BLAKE2b-256 |
0bdbf73f46fb8f23b6103805c47b05605102cb872cdde62e1549ffd9a08f00e3
|
File details
Details for the file dict2dataclass-0.2.0-py3-none-any.whl.
File metadata
- Download URL: dict2dataclass-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.12.8 Darwin/24.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b9c32b5d33dfae26bd73a4b27ff5a337f718a1cb918cf736c61f8d63fe30b47
|
|
| MD5 |
499813569218ecb3b8d3b61cbd638442
|
|
| BLAKE2b-256 |
a728b5b9c537e876d4811bc513456a4b0ac1d309936502bf15e26f0453c9c426
|