Serialize/deserialize Python objects from/to typed structures.
Project description
serdataclasses
This library has the following goals:
- "Deserialize" unstructured Python types into structured, type-hinted Python types (dataclasses.dataclass, typing.NamedTuples).
- "Serialize" structured, type-hinted Python objects into unstructured Python types (eg, the reverse)
- Provide the user clear error messages in the event that serde fails.
- Require no type changes on the part of the user. No need to give your containers a special type to help this library perform serde, it works out of the box.
- Work correctly for all forms of NamedTuples and dataclasses. Unfortunately, prior to Python 3.8, the dataclasses had some deficiencies. Mainly,
dataclasses.InitVar
was a singleton whose contained type could not be inspected at runtime. For this reason, only Python 3.8+ is supported.
No external dependencies. Python 3.8+.
Installation
# With pip
pip install serdataclasses
# With poetry
poetry add serdataclasses
Usage
import dataclasses
import typing
import serdataclasses
@dataclasses.dataclass
class SmallContainer:
my_str: str
@dataclasses.dataclass
class BigContainer:
my_int: int
my_list: typing.List[SmallContainer]
MY_DATA = {
"my_int": 1,
"my_list": [
{ "my_str": "rawr" },
{ "my_str": "woof" },
],
}
# Deserialization
MY_STRUCTURED_DATA = serdataclasses.load(MY_DATA, BigContainer)
print("Deserialization:", MY_STRUCTURED_DATA)
# Serialization
MY_UNSTRUCTURED_DATA_AGAIN = serdataclasses.dump(MY_STRUCTURED_DATA)
print("Serialization:", MY_UNSTRUCTURED_DATA_AGAIN)
Result:
Deserialization: BigContainer(my_int=1, my_list=[SmallContainer(my_str='rawr'), SmallContainer(my_str='woof')])
Serialization: {'my_int': 1, 'my_list': [{'my_str': 'rawr'}, {'my_str': 'woof'}]}
Local Development
Local development for this project is quite simple.
Dependencies
Install the following tools manually.
Recommended
Set up development environment
make setup
Run Tests
make test
Notes
- Initially inspired by undictify and a PR I helped with. serdataclasses's goals are different; it's exclusively focused on serde instead of general function signature overrides.
- I also notice some striking similarities with a library called typedload (great minds think alike, I guess :p). I renamed my top-level functions to "load" and "dump" in typedload's homage. Unfortunately, as of version
1.20
, typedload does not handle all types of dataclasses elegantly (mainly, InitVar). Since typedload supports Python 3.5+, it never will elegantly handle all dataclasses without lots of unfortunate conditionals in the codebase. If you must use Python 3.7-, I suggest looking into typedload.
Written by
Samuel Roeca samuel.roeca@gmail.com
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
serdataclasses-0.4.0.tar.gz
(7.8 kB
view hashes)
Built Distribution
Close
Hashes for serdataclasses-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebebb303967ae0b95ec9a1df4676ece3ea497d4341e418dd90c82a63f3db5d4d |
|
MD5 | 8baa1bfed5e16a42495350a82283b49a |
|
BLAKE2b-256 | 59720c036bcc5bb8820d9337d55d1623ede6744d87c35d6e7e707442f20ca7eb |