A Python package to help with serialization and deserialization of dataclasses through the help of a common interface while also insuring the parsed data is properly typed and handled in many situations.
Project description
Mooss - Serialize
⚠️ This package is a woro-in-progress, it is not suitable nor reliable yet ⚠️
A Python package to help with serialization and deserialization of dataclasses through the help of a common interface
while also insuring the parsed data is properly typed and handled in many situations.
This package was created because I often found myself needing to deserialize nested dataclasses with complex value
types, and because all other solutions I found were either too bloated or didn't work properly with what I had.
It is by no mean a replacement for other packages, but [...].
Usage
[Mention how it uses annotations !]
Requirements
- Python 3.9 or newer.
Installation
Run one of the following commands to install the package:
python -m pip install --upgrade mooss-serialize
pip install --upgrade mooss-serialize
Creating classes
The following classes have more complex and fluid typing for their variables that will help illustrate the main
advantage of this package over oneliners and other simpler deserializers.
from dataclasses import dataclass
from typing import Optional, Union
from mooss.serialize.interface import ISerializable
@dataclass
class Address(ISerializable):
country: str
city: str
zip_code: Optional[int]
# TODO: non-serializable bool
street: str = "Unknown"
@dataclass
class Person(ISerializable):
name: str
address: Union[Address, str, None]
Preparing the raw data
[Can be from json, toml or raw as a dict]
# All of the fields with nested 'ISerializable' classes
data_person_full: dict = {
"name": "John Smith",
"address": {
"country": "Belgium",
"city": "Brussels",
"zip_code": 1000,
"street": "Rue de la Tribune",
},
}
Parsing the data
person_full = Person.from_dict(data_person_full)
print(person_full)
Supported type annotations
The following types are supported by the interface:
- Primitives:
str
,int
,float
,bool
Any other object type should be ignored and if possible, instantiated as a dict.
Contributing
If you want more information on how to contribute to this package, you should refer to develop.md.
License
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
Hashes for mooss_serialize-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 455a448a8dbf6d94308704d45f741610a2ecd400c3a695d09a52180b42b0ab19 |
|
MD5 | 42437e2f4e29f28c6b242726e33b8f72 |
|
BLAKE2b-256 | fbbf4d2533e1ac26d2db2dce1f6d786d98d299995e3a78abc3291df562cfc667 |