Package for serializing python object & dataclasses
Project description
Serializer
A serialization module for python objects
Quickstart
pip install .....
from typing import List
from serializer import serializable
@serializable
class Role:
role_type: str
attributes: List[str]
@serializable
class Person:
name: str
age: int
height: float
weight: float
address: str
role: List[Role]
p = Person(
name="Paul", age=25, height=1.70, weight=83.5, address="earth",
role=[Role(role_type='human', attributes=['speak', 'eat', 'sleep'])]
)
print(p.to_dict())
{ 'name': 'Paul', 'age': 25, 'height': 1.7, 'weight': 83.5, 'address': 'earth', 'role': [ { 'role_type': 'human', 'attributes': ['speak', 'eat', 'sleep'] } ] }
Serializable wrapper extends dataclass so you can treat it like a normal dataclass.
Mixin
It is possible to have a class extend abstract class SerializableMixin
from serializer import SerializableMixin, serializer
class Test(SerializableMixin):
def __init__(self, name: str):
self.name = name
def __serialize__(self):
return dict(name=serializer(self.name))
>> Test(name='Paul').to_dict()
{ 'name': 'Paul' }
For object to be seriazable they only need to implement a __serialize__
method.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
py-serializer-0.9.5.tar.gz
(4.3 kB
view hashes)
Built Distribution
Close
Hashes for py_serializer-0.9.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20eb9079fd8f1aaa5997f49935cad6a491a6fe643342aec81e9230fe16aa66c9 |
|
MD5 | 02ab828f9df4ef1e0113b33348a61c96 |
|
BLAKE2b-256 | 672a497ac10704a44865d4641c7f2f21163769348446a0cd12a5bed37611649a |