dictipy

Dictipy creates the right dict also for nested objects using recursion.


License
MIT
Install
pip install dictipy==0.0.3

Documentation

Dictipy

Dictipy creates the right dict also for nested objects using recursion, whenever the standard Python __dict__() cannot.

Python 2.7, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11 PyPI version Build Status codecov

Table of contents

  1. Motivation
  2. Usage

1. Motivation

Using get_dict makes you able to recursively get dict of nested objects without explicitly overriding __repr__() function, making it usable for other purposes. It could be useful when you have very complex nested object and you want not to override each sub-object __repr__() function. Imagine for example an operation which produces a complex object which has to be serialized and sent through a REST protocol as a json. The json.dumps() cannot execute the task if the argument object is not a dict. Again, using simply the standard Python __dict__() function does not solve the problem if a nested object has to be considered.

2. Usage

Simply import get_dict function from dictipy and use it on any potentially serializable object.


Example 1: Nested objects.

from dictipy import dictipy


class Parent:

    def __init__(self, parent_field):
        self.parent_field = parent_field
        self.child = Child(1)


class Child:

    def __init__(self, child_field):
        self.child_field = child_field


if __name__ == "__main__":
    p = Parent(0)
    print("Standard Python dict:  ", p.__dict__)
    print("Python vars:  ", vars(p))
    print("Dictipy get_dict:      ", dictipy(p))

Result:

Standard Python dict:   {'parent_field': 0, 'child': <__main__.Child object at 0x0000021C530BFEB8>}
Python vars:   {'parent_field': 0, 'child': <__main__.Child object at 0x0000021C530BFEB8>}
Dictipy get_dict:       {'parent_field': 0, 'child': {'child_field': 1}}

Example 2: Json serialization.

from dictipy import dictipy
import json


class Parent:

    def __init__(self, parent_field):
        self.parent_field = parent_field
        self.child = Child(1)


class Child:

    def __init__(self, child_field):
        self.child_field = child_field


if __name__ == "__main__":
    p = Parent(0)
    j1 = json.dumps(p) # throws -> TypeError: Object of type Parent is not JSON serializable
    j2 = json.dumps(p.__dict__) # throws -> TypeError: Object of type Child is not JSON serializable
    j3 = json.dumps(vars(p)) # throws -> TypeError: Object of type Child is not JSON serializable
    j4 = json.dumps(dictipy(p)) # returns -> '{"parent_field": 0, "child": {"child_field": 1}}'