Skip to main content

simple library to create objects from dictionaries

Project description


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


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'})

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

dedict-1.0.7.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

dedict-1.0.7-py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 3

Supported by

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