Skip to main content

Package for deep serializing and deserializing

Project description

Python Deep Serializer

Package provides for

  • Serializing nested Python objects. That is creating a nested dictionary from a nested Python object.

  • Deserializing nested Python objects, that is initializing nested Python objects with a nested dictionary.

One use case is when you have a web api and users post nested json which you use to initialize (nested) Python objects.

Example

import datetime
import typing
from pprint import pprint

from deep_serializer import des
from deep_serializer.ser import ToDictEnabled


class Person(ToDictEnabled):

    @des.cast  # casting input
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age


class Car(ToDictEnabled):

    @des.cast  # casting input
    def __init__(self, model: str, owner: Person, color: str, registration_date: datetime.datetime,
                 passengers: typing.List[Person] = () ):
        self.model = model
        self.owner = owner
        self.color = color
        self.registration_date = registration_date
        self.passengers = passengers
        self.__private_variable = 'secret'  # private attribute not serialized by todict

    @property
    def color_and_owner(self) -> str:  # property not serialized by todict
        return '%s-%s' % (self.color, self.owner.name)


if __name__ == '__main__':
    car_dict = {
        'color': 'blue',
        'model': 'Volvo',
        'owner': {'age': '22', 'name': 'Bob'},
        'passengers': [{'age': 20, 'name': 'Alice'}, {'age': 19, 'name': 'Joe'}],
        'registration_date': '205-01-02 15:22.01'
    }

    print('\n\nInput')
    pprint(car_dict)

    car = Car(**car_dict)
    print('\n\nCar object:\n%s' % car)

    print('\n\nCar as dict')
    pprint(car.todict(), indent=4)

Note that

  • The ToDictEnabled adds the todict method which enables deep serialization.
  • The @des.init property tries to cast input variables according to their type hints before actual init is run.
  • The @color_and_owner property is not serialized.
  • The __private_variable attribute is not serialized.

Running main yields

Input
{'color': 'blue',
 'model': 'Volvo',
 'owner': {'age': '22', 'name': 'Bob'},
 'passengers': [{'age': 20, 'name': 'Alice'}, {'age': 19, 'name': 'Joe'}],
 'registration_date': '205-01-02 15:22.01'}


Car object:
<__main__.Car object at 0x7f018d24ce10>


Car as dict
{   'color': 'blue',
    'model': 'Volvo',
    'owner': {'age': 22, 'name': 'Bob'},
    'passengers': [{'age': 20, 'name': 'Alice'}, {'age': 19, 'name': 'Joe'}],
    'registration_date': datetime.datetime(205, 1, 2, 15, 22)}

Caveats

  • Code is virtually untested, except on a few minor private projects.

  • Casting is far from complete. For example, the typing.Tuple[] type hinting does not work, and those input variables will not be casted.

  • Only works with Python3, only tested with Python3.6 and Python3.7.

  • todict method will not serialize attributes starting with __ and not serialize properties.

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

deep_serializer-0.0.1.tar.gz (4.8 kB view hashes)

Uploaded Source

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