Dedict
Dedict is a simple dependency-less library that allows you to create data objects from dictionaries. It currently works on Python 3.6 and 3.7
Why Dedict?
There's already a few libraries out there that allow you to create objects from json/dictionaries, such as jsonpickle. However these libraries usually rely on metadata stored in the input structure to create the object. That means the serialized object has noisy metadata in it and that the dictionary/json must have been generated with the library to be able to be transformed back into an object.
There's also jsonstruct which fixes the metadata issue, but requires dataclasses to have "default" values defined for their parameters so that it can read the types at runtime and understand how to create the object.
Dedict aims to strive away from these methods, and allow the creation of objects without relying on metadata inside the input data or arbitrary default values within the data class. Instead, dedict makes clever use of type hints and variable annotations that were introduced with Python 3.6
Install
pip install dedict
Usage example
class SomeDataClass(Dedictable):
something: str
whatever: int
some_data_class = SomeDataClass.dedict({'something': 'test', 'whatever': 100})
# output: SomeDataClass(something='test', whatever=100)
Dedict also works recursively, meaning you can parse complex data structures
class SomeChildClass(Dedictable):
a: str
b: str
class Complex(Dedictable):
some_string: str
child: SomeChildClass
complex = Complex.dedict({'some_string': 'test', 'child': {'a': 'hello', 'b': 'world'}})
Built-in object model validation
Dedict also ensures the data passed is actually a valid representation of the object you're trying to create, by checking whether your data structure has fields that do not belong in the target object
class Sample(Dedictable):
something: str
whatever: str
sample = Sample.dedict({'something': 'else', 'whatever': 'you want', 'hello': 'world'})
# raises: AttributeError('object Sample has no attribute named hello')
By default, dedict will not care about missing attributes in the input data structure, and will consider them optional. However you can enforce full validation by instead using the DedictableStrict implementation.
class Strict(DedictableStrict):
a: str
b: str
c: str
s = Strict.dedict({'b': 'hello', 'c': 'world'})
# raises: AttributeError('dictionary was missing mandatory attribute a to create an object of type Sample')
However, if you want full validation but also need to set some attributes as optional, dedict provides a new type annotation to mark attributes as optional when using the strict implementation.
class Strict(DedictableStrict):
a: OptionalAttr[str]
b: str
c: str
s = Strict.dedict({'b': 'hello', 'c': 'world'})
Release files for dedict 1.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dedict-1.0.7.tar.gz | 3.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dedict-1.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size:7.7 kB
Release files / dedict-1.0.7.tar.gz
| Download URL | dedict-1.0.7.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3d2cf39441e2450f2ce7d830e01f3c57b26bdc0564174e2d90dd63dab34f4b21
|
|
BLAKE2b-256 checksum How to use checksums |
1734109aef09ecfc2363d80d579519e7386d99f5839092b8439599d9d91046cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.6.8
|
Release files / dedict-1.0.7-py3-none-any.whl
| Download URL | dedict-1.0.7-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d83487bbc60160dff3270752f6631344e031bf1abd83a37ac49a5e601009703f
|
|
BLAKE2b-256 checksum How to use checksums |
3f79f3befcb9cc4a61d817c93829448202c2e009cbca24d2fffd6b7b14a7e8fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.6.8
|