Convert a python dictionary into a dataclass
Project description
just-use-dataclass
Simple function to convert a python dict to a dataclass.
If you want to know how the library works, the best place to look at is in the tests
In short: it converts a dictionary to a dataclass by looking at the annotations in the dataclass and converting the data in the dictionary to it.
Installation
pip install just-use-dataclass
Usage and examples
Simple data type conversion
from dataclasses import dataclass
import typing
from just_use_dataclass import dict_to_dataclass
@dataclass
class Simple:
a: str
b: int
c: float
d: bool
e: typing.Optional[str]
f: typing.Optional[str]
data = {
"a": 1,
"b": "1",
"c": "2.0",
"d": True,
"e": None,
"f": 42,
}
simple = dict_to_dataclass(data, Simple)
print(simple)
# Output:
# Simple(a='1', b=1, c=2.0, d=True, e=None, f='42')
Nested dataclass conversion
from dataclasses import dataclass
from decimal import Decimal
from just_use_dataclass import dict_to_dataclass
@dataclass
class C:
value: str
@dataclass
class B:
value: int
c: C
@dataclass
class A:
value_1: Decimal
value_2: Decimal
b: B
data = {
"value_1": "23.23",
"value_2": 49.0001,
"b": {
"value": 1,
"c": {"value": "test"},
},
}
nested = dict_to_dataclass(data, A)
print(nested)
# Output:
# A(value_1=Decimal('23.23'), value_2=Decimal('49.0001'), b=B(value=1, c=C(value='test'))
Deeply nested typing conversion
from dataclasses import dataclass
import typing
from just_use_dataclass import dict_to_dataclass
@dataclass
class Crazy:
value: dict[str, dict[int, list[typing.Optional[str]]]]
data = {"value": {"test": {1: ["hey", None, 42]}}}
crazy = dict_to_dataclass(data, Crazy)
print(crazy)
# Output:
# Crazy(value={'test': {1: ['hey', None, '42']}})
Development
-
Fork and clone the repo
-
Install poetry or just pytest, mypy and black
poetry install
# or
pip install -U pytest mypy black
- Run tests
poetry run pytest .
- Linting / Formatting
poetry run mypy .
poetry run black .
Use in production
The library is really new. if you want to contribute feel welcome. Otherwise don't use it in production (yet) :)
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
Built Distribution
File details
Details for the file just_use_dataclass-0.1.1.tar.gz
.
File metadata
- Download URL: just_use_dataclass-0.1.1.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.13 Linux/6.2.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 311bbfe6dcefe1413d8f73095dafecc7f80d2834ce44a2fe17366ad729e6fd1a |
|
MD5 | 49c30fe37790969ac89d7508990fcc65 |
|
BLAKE2b-256 | 8390ce5580cb063ed5459a8d12d342ba898b9ea98178c80002f289ed16f5c2f7 |
File details
Details for the file just_use_dataclass-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: just_use_dataclass-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.13 Linux/6.2.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7819d0f35eb01363aafd8747bc3247f3bff663562483f33e15f096ba6314404a |
|
MD5 | e8b7c23b2fb04d5e5f10285da422c576 |
|
BLAKE2b-256 | 10efb2bb4f0cbbcae888de4f37fb72ca53d7539bb5a1ce7ffa1e796df63e07a6 |