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.1.2.tar.gz (7.1 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.1.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dict2dataclass-0.1.2.tar.gz
  • Upload date:
  • Size: 7.1 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.1.2.tar.gz
Algorithm Hash digest
SHA256 1ce805072139db98b3cebe10b55a7591ebd217ae8578cb1ec4e1e9882416fb2a
MD5 58c13a10e4bfeff0c959195e3b791066
BLAKE2b-256 30c8a0da053c6b67ba1a544db56ce7ed62004f3dbbad0cca0cdbd19ce182bc85

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dict2dataclass-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.0 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.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5854eaaa22ce63797490f411f56081f6531ed5399baa37d321b565eb8e50bf0a
MD5 45e0fa7973d0082986bc4f809e696d1b
BLAKE2b-256 69b2addb9f96390f34e9d66e23c1339ab8a8286f6a97ff7e22623ecc4addf3df

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