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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

dictipy-0.0.4-py3.8.egg (3.9 kB view details)

Uploaded Source

dictipy-0.0.4-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file dictipy-0.0.4-py3.8.egg.

File metadata

  • Download URL: dictipy-0.0.4-py3.8.egg
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.0

File hashes

Hashes for dictipy-0.0.4-py3.8.egg
Algorithm Hash digest
SHA256 f0600741ea74a2eec92e88f818bbc1841d289b1e0b794f59a486fbd438a7361e
MD5 5e31b9aadf588cf642fd601e10fd6f56
BLAKE2b-256 74d8086240997f2cb9d00da6c7a3951a5b75c4af3baeb009dc8aab6beaaa515a

See more details on using hashes here.

File details

Details for the file dictipy-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: dictipy-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.0

File hashes

Hashes for dictipy-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 01c35443d1f57df9484e0935598790e8c958c763eed8f006c2ddd65356227ceb
MD5 5cb120b2d6bd31326c718b8672438a47
BLAKE2b-256 dcee10d065b2788a69185f6ddc4a58a4d53b7de789669f9abbc5e81491a0bc8d

See more details on using hashes here.

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