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
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 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dictipy-0.0.1.tar.gz.
File metadata
- Download URL: dictipy-0.0.1.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7eb4afac2e8c2d65401fabd535ae42fd6bdabe8893a3df423d7a4cb6c8f82600
|
|
| MD5 |
8af0cede7c077a263a7cc295b440075e
|
|
| BLAKE2b-256 |
56194793e7b228000066d5d43c3ae16444677ce597f8786b3feaa4099c56fc16
|
File details
Details for the file dictipy-0.0.1-py3-none-any.whl.
File metadata
- Download URL: dictipy-0.0.1-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd74e46ccd23e43f941649624718a428632676334463587187b1da74ff02a87d
|
|
| MD5 |
9590e1f5fe5716f4b02468e85524ad87
|
|
| BLAKE2b-256 |
575878d021dc5506e769016e67f2be00abfabaad8b2f53ab3b118b7b05441d56
|