Skip to main content

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

Project description

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 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 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.

Files for dictipy, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size dictipy-0.0.3-py3-none-any.whl (4.2 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size dictipy-0.0.3.tar.gz (3.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page