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
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
dict2dataclass-0.1.1.tar.gz
(7.0 kB
view details)
Built Distribution
File details
Details for the file dict2dataclass-0.1.1.tar.gz
.
File metadata
- Download URL: dict2dataclass-0.1.1.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87b138cf39a671f384948466d6e8060929b0a0918ebcdeac32598bcba9ae3a69 |
|
MD5 | 5287f6d7145527cb0ebb3d795c3f0419 |
|
BLAKE2b-256 | a9ec3e49b99e3a143e5328bcb3eb3c89914a56665bf77071151725ab8d7c6654 |
File details
Details for the file dict2dataclass-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: dict2dataclass-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85ee775f3f784df3c1501053a14234c7675047e7571f1a90f155ae93fd117acc |
|
MD5 | da7cab375ececba136be471ee9110038 |
|
BLAKE2b-256 | 1f25c55a231e1511518bf81bd2909dd6cd8f53bf5e540de08199b03670c0d1fc |