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
from pprint import pprint
import datetime
import typing
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] = (), miles: typing.Dict[datetime.datetime, float] = None):
self.miles = miles
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': '2015-01-01 12:00.00',
'miles': {'2015-01-01 12:00.00': 0, '2016-01-01 12:00.00': '1000'}
}
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
ToDictEnabledadds thetodictmethod which enables deep serialization. - The
@des.initproperty tries to cast input variables according to their type hints before actual init is run. - The
@color_and_ownerproperty is not serialized. - The
__private_variableattribute is not serialized.
Running main yields
Input
{'color': 'blue',
'miles': {'2015-01-01 12:00.00': 0, '2016-01-01 12:00.00': '1000'},
'model': 'Volvo',
'owner': {'age': '22', 'name': 'Bob'},
'passengers': [{'age': 20, 'name': 'Alice'}, {'age': 19, 'name': 'Joe'}],
'registration_date': '2015-01-01 12:00.00'}
Car object:
<__main__.Car object at 0x7f4972796cf8>
Car as dict
{ 'color': 'blue',
'miles': { datetime.datetime(2015, 1, 1, 12, 0): 0.0,
datetime.datetime(2016, 1, 1, 12, 0): 1000.0},
'model': 'Volvo',
'owner': {'age': 22, 'name': 'Bob'},
'passengers': [{'age': 20, 'name': 'Alice'}, {'age': 19, 'name': 'Joe'}],
'registration_date': datetime.datetime(2015, 1, 1, 12, 0)}
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.
-
todictmethod will not serialize attributes starting with__and not serialize properties.
Metadata
Release files for deep-serializer 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| deep_serializer-0.0.6.tar.gz | 5.1 kB | Without provenance | Details for deep_serializer-0.0.6.tar.gz |
Release files / deep_serializer-0.0.6.tar.gz
| Download URL | deep_serializer-0.0.6.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
140dcb8dd15cdab8e27a64520a76dd83a7e0cdd41b2682c30b6acf31f1b2495f
|
|
BLAKE2b-256 checksum How to use checksums |
dfd3d7597c65ed78eb97aa5ef52288c6b8277fa4d4e5bad334a9d29922addeb0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.7
|
Security for deep-serializer 0.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.