Metadata-Version: 2.1
Name: dictipy
Version: 0.0.2
Summary: Dictipy creates the right dict also for nested objects using recursion.
Home-page: https://github.com/gioelecrispo/dictipy.git
Author: Gioele Crispo
Author-email: crispogioele@gmail.com
License: MIT
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3


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.

.. code-block:: python

   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: 

.. code-block:: python

   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.

.. code-block:: python

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


