Skip to main content

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

dict2dataclass-0.3.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dict2dataclass-0.3.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file dict2dataclass-0.3.0.tar.gz.

File metadata

  • Download URL: dict2dataclass-0.3.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.8 Darwin/24.1.0

File hashes

Hashes for dict2dataclass-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1156f2279a6bd9b8b2c4b988966364e7aa4cd1d02380795434697a484d859b06
MD5 41b84d6e9a618cc85d2daeb120eba933
BLAKE2b-256 e91e54b47290ba18eef05aa8630fc731c846aeda99c4e0199188b81667ddd855

See more details on using hashes here.

File details

Details for the file dict2dataclass-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: dict2dataclass-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 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

Hashes for dict2dataclass-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 868aaae86fa4daa3c82832a55a2fd477ef4215e24420bba891342ce7c0a7350e
MD5 ea28972cd915d4ab7780ea5b57f0a0af
BLAKE2b-256 a7d8dcfa054b2406209de81335cc9631d972f877adf728e6f69565b7c4342d4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page