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.
Table of contents
- Motivation
- 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
Built Distributions
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0600741ea74a2eec92e88f818bbc1841d289b1e0b794f59a486fbd438a7361e |
|
MD5 | 5e31b9aadf588cf642fd601e10fd6f56 |
|
BLAKE2b-256 | 74d8086240997f2cb9d00da6c7a3951a5b75c4af3baeb009dc8aab6beaaa515a |
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01c35443d1f57df9484e0935598790e8c958c763eed8f006c2ddd65356227ceb |
|
MD5 | 5cb120b2d6bd31326c718b8672438a47 |
|
BLAKE2b-256 | dcee10d065b2788a69185f6ddc4a58a4d53b7de789669f9abbc5e81491a0bc8d |