Skip to main content

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

Project description

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

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 get_dict


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("Dictipy get_dict:      ", get_dict(p))

Result:

Standard Python dict:   {'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 get_dict
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(get_dict(p)) # returns -> '{"parent_field": 0, "child": {"child_field": 1}}'

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

dictipy-0.0.1.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

dictipy-0.0.1-py3-none-any.whl (3.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