Dict 2 Dataclass
A lightweight Python utility to seamlessly convert between dictionaries and dataclasses. This is useful when you want to access dictionary values as class attributes or serialize dataclasses back to dictionaries.
Features
- 🚀 Easy Conversion: Convert dictionaries to dataclasses and vice versa.
- 🔗 Nested Support: Handles nested dataclasses effortlessly.
- 🧩 Intuitive API: Simple and clean syntax for integration.
Installation
pip install dict2dataclass
Usage
Note The
FromDictandToDictclasses are compatible. You can use both in the same dataclass for full bidirectional conversion.
Convert Dictionary to Dataclass
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
# Example dictionary
data = {
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Springfield",
"state": "IL"
}
}
# Convert to dataclass
person = Person.from_dict(data)
print(person.name) # Output: John Doe
print(person.address.city) # Output: Springfield
Convert Dataclass to Dictionary
from dict2dataclass import ToDict
from dataclasses import dataclass
@dataclass
class Address(ToDict):
street: str
city: str
state: str
@dataclass
class Person(ToDict):
name: str
age: int
address: Address
# Create a dataclass instance
person = Person(
name="John Doe",
age=30,
address=Address(
street="123 Main St",
city="Springfield",
state="IL"
)
)
# Convert to dictionary
data = person.to_dict()
print(data)
# Output: {'name': 'John Doe', 'age': 30, 'address': {'street': '123 Main St', 'city': 'Springfield', 'state': 'IL'}}
Combining FromDict and ToDict
For full bidirectional support, you can inherit from both FromDict and ToDict:
from dict2dataclass import FromDict, ToDict
from dataclasses import dataclass
@dataclass
class Address(FromDict, ToDict):
street: str
city: str
state: str
@dataclass
class Person(FromDict, ToDict):
name: str
age: int
address: Address
Metadata
Release files for dict2dataclass 0.4.0
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 | Actions |
|---|---|---|---|---|
| dict2dataclass-0.4.0.tar.gz | 7.6 kB | Without provenance | Details for dict2dataclass-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dict2dataclass-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for dict2dataclass-0.4.0-py3-none-any.whl |
Total release size: 16.8 kB
Release files / dict2dataclass-0.4.0.tar.gz
| Download URL | dict2dataclass-0.4.0.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0f13cc288e3e57c50ae2b20dc9eeb6ba48ba3816b6561f9218dad10625abb855
|
|
BLAKE2b-256 checksum How to use checksums |
d6ce5470adc844af4f4d15afef6279f36648181f92311cbdc39219fcfc3e2f1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.9.23 Linux/6.11.0-1015-azure
|
Release files / dict2dataclass-0.4.0-py3-none-any.whl
| Download URL | dict2dataclass-0.4.0-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5c0f8c0640f52dd37620f1a523e805b6ddd90fe980a7d8ec8ff74842d0937cdc
|
|
BLAKE2b-256 checksum How to use checksums |
5c0208cbb15f81a878fd1c636b94f08d97cd60a4fa22cd9b1011b88877236610
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.9.23 Linux/6.11.0-1015-azure
|
Security for dict2dataclass 0.4.0
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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.