Skip to main content

simple library to create objects from dictionaries

Project description

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

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 details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

Details for the file dedict-1.0.7.tar.gz.

File metadata

  • Download URL: dedict-1.0.7.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for dedict-1.0.7.tar.gz
Algorithm Hash digest
SHA256 3d2cf39441e2450f2ce7d830e01f3c57b26bdc0564174e2d90dd63dab34f4b21
MD5 f26a825a672758097da7e4dcaa57fd78
BLAKE2b-256 1734109aef09ecfc2363d80d579519e7386d99f5839092b8439599d9d91046cd

See more details on using hashes here.

File details

Details for the file dedict-1.0.7-py3-none-any.whl.

File metadata

  • Download URL: dedict-1.0.7-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for dedict-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d83487bbc60160dff3270752f6631344e031bf1abd83a37ac49a5e601009703f
MD5 650df4ca2b456d2f320c0617a7fb78eb
BLAKE2b-256 3f79f3befcb9cc4a61d817c93829448202c2e009cbca24d2fffd6b7b14a7e8fd

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