No project description provided
Project description
serpyco-rs: a serializer for python dataclasses
What is serpyco-rs ?
Serpyco is a serialization library for Python 3.9+ dataclasses that works just by defining your dataclasses:
import dataclasses
import serpyco_rs
@dataclasses.dataclass
class Example:
name: str
num: int
tags: list[str]
serializer = serpyco_rs.Serializer(Example)
result = serializer.dump(Example(name="foo", num=2, tags=["hello", "world"]))
print(result)
>> {'name': 'foo', 'num': 2, 'tags': ['hello', 'world']}
serpyco-rs works by analysing the dataclass fields and can recognize many types : list
, tuple
, Optional
...
You can also embed other dataclasses in a definition.
The main use-case for serpyco-rs is to serialize objects for an API, but it can be helpful whenever you need to transform objects to/from builtin Python types.
Installation
Use pip to install:
$ pip install serpyco-rs
Features
- Serialization and deserialization of dataclasses
- Validation of input/output data
- Very fast
- Support recursive schemas
Supported field types
There is support for generic types from the standard typing module:
- Decimal
- UUID
- Time
- Date
- DateTime
- Enum
- List
- Dict
- Mapping
- Sequence
- Tuple (fixed size)
Benchmark
macOS Monterey / Apple M1 Pro / 16GB RAM / Python 3.11.0
dump
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
serpyco_rs | 0.05 | 22188.2 | 1 |
serpyco | 0.05 | 20878.5 | 1.06 |
mashumaro | 0.06 | 15602.7 | 1.42 |
pydantic | 2.66 | 375.6 | 59 |
marshmallow | 1.05 | 951.7 | 23.33 |
load with validate
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
serpyco_rs | 0.23 | 4400.1 | 1 |
serpyco | 0.28 | 3546.4 | 1.24 |
mashumaro | 0.23 | 4377.7 | 1.01 |
pydantic | 2.01 | 497.3 | 8.86 |
marshmallow | 4.55 | 219.9 | 20.03 |
load (only serpyco and serpyco_rs supported load without validate)
Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
---|---|---|---|
serpyco_rs | 0.07 | 13882.9 | 1 |
serpyco | 0.08 | 12424.5 | 1.12 |
mashumaro | 0.23 | 4382.9 | 3.17 |
pydantic | 2.02 | 494.4 | 28.09 |
marshmallow | 4.59 | 217.5 | 63.8 |
Supported annotations
serpyco-rs
supports changing load/dump behavior with typing.Annotated
.
Currently available:
- Alias
- FiledFormat (CamelCase / NoFormat)
- NoneFormat (OmitNone / KeepNone)
- Min / Max
- MinLength / MaxLength
Alias
Alias
is needed to override the field name in the structure used for load
/ dump
.
from dataclasses import dataclass
from typing import Annotated
from serpyco_rs import Serializer
from serpyco_rs.metadata import Alias
@dataclass
class A:
foo: Annotated[int, Alias('bar')]
ser = Serializer(A)
print(ser.load({'bar': 1}))
>> A(foo=1)
print(ser.dump(A(foo=1)))
>> {'bar': 1}
FiledFormat
Used to have response bodies in camelCase while keeping your python code in snake_case.
from dataclasses import dataclass
from typing import Annotated
from serpyco_rs import Serializer
from serpyco_rs.metadata import CamelCase, NoFormat
@dataclass
class B:
buz_filed: str
@dataclass
class A:
foo_filed: int
bar_filed: Annotated[B, NoFormat]
ser = Serializer(Annotated[A, CamelCase]) # or ser = Serializer(A, camelcase_fields=True)
print(ser.dump(A(foo_filed=1, bar_filed=B(buz_filed='123'))))
>> {'fooFiled': 1, 'barFiled': {'buz_filed': '123'}}
print(ser.load({'fooFiled': 1, 'barFiled': {'buz_filed': '123'}}))
>> A(foo_filed=1, bar_filed=B(buz_filed='123'))
NoneFormat
Via OmitNone
we can drop None values for non required fields in the serialized dicts
from dataclasses import dataclass
from serpyco_rs import Serializer
@dataclass
class A:
required_val: bool | None
optional_val: bool | None = None
serializer = Serializer(A, omit_none=True) # or Serializer(Annotated[A, OmitNone])
print(serializer.dump(A(required_val=None, optional_val=None)))
>>> {'required_val': None}
Min / Max
Supported for int
/ float
/ Decimal
types and only for validation on load.
from typing import Annotated
from serpyco_rs import Serializer
from serpyco_rs.metadata import Min, Max
ser = Serializer(Annotated[int, Min(1), Max(10)])
ser.load(123)
>> SchemaValidationError: [ErrorItem(message='123 is greater than the maximum of 10', instance_path='', schema_path='maximum')]
MinLength / MaxLength
MinLength
/ MaxLength
can be used to restrict the length of loaded strings.
from typing import Annotated
from serpyco_rs import Serializer
from serpyco_rs.metadata import MinLength
ser = Serializer(Annotated[str, MinLength(5)])
ser.load("1234")
>> SchemaValidationError: [ErrorItem(message='"1234" is shorter than 5 characters', instance_path='', schema_path='minLength')]
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
Built Distributions
Hashes for serpyco_rs-0.5.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c0b93af0fe23b71eaa8f97d46ee247a4a3eacbc4f491db7c5e6cdb84ae5d2b4 |
|
MD5 | 689d3e6e08cfcbcc1d043404ba7caca8 |
|
BLAKE2b-256 | 9326f9dd59db50dac0a90eab282883f636e37d158427a8e8afcf007028a4a9ac |
Hashes for serpyco_rs-0.5.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b64823ce2ba22c157bec73a6b12b830b2952884fd754ee4a563748c9cf21dc2 |
|
MD5 | 532429924a7216f4c14a4610c18a46de |
|
BLAKE2b-256 | 3668f8edf1c75f3857c10b55ca04ecc3df7ea12991425a889f70a4044ff097f1 |
Hashes for serpyco_rs-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e51a541326ba3557e6eb54827afd54461665652dd9b7027a176cc2fb9ec46b4 |
|
MD5 | 8a0893d2a848eb08ec39e90d4326911f |
|
BLAKE2b-256 | b8a850c869f6ddb55695cc2bf520f80d7b7935105d3ebec819662a34f20fea94 |
Hashes for serpyco_rs-0.5.0-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32d187bff95bb77cc172b57715c3a4fa6b2e1eb22520ee6995ecd6fad193524f |
|
MD5 | 21bce21e5daec8e90682ab452a2738fb |
|
BLAKE2b-256 | 2277b5e486626334f1387ad1e41429320c5dde12fb3786a6792f475163b59707 |
Hashes for serpyco_rs-0.5.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e618d7abbbb823e4b7e4fcdb595dd6396eb2548d5ff5041621901e946f170fe |
|
MD5 | c05f5630f45edcc3c16235950124118d |
|
BLAKE2b-256 | a7353bf6832054e5ecea5a364088c5fd816325dc90cec4da4427968d1a6f1637 |
Hashes for serpyco_rs-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fd78454fe5b0c7d996f16bc08768fafe47681b3793c9a587573df2c9c21a043 |
|
MD5 | 9029758364c0c03e92a690e0db8a2916 |
|
BLAKE2b-256 | a9c75b85156fac36b4e88557d386ff82a4cf6a5ab5b632404538d21ac395d779 |
Hashes for serpyco_rs-0.5.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 978f1ef62175d5d0e731fdfa4015a24f5aa30c43b758c635542c7a807ebd9c62 |
|
MD5 | 42699bf23a318baefb60f6d414089d22 |
|
BLAKE2b-256 | f0bf8bbe3c460fa4f086547048e8095692213045fce75ae9c808e4584ba3a714 |
Hashes for serpyco_rs-0.5.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbe17fa00ea9fc3c32b916a1c0f68baf72c414e91e27039011e063ecd4f176c9 |
|
MD5 | 2d8c5d509a1361e617b03367f59289df |
|
BLAKE2b-256 | 2d7bcbc71cc8d54a840ebbff57fe1be3885d3ffaa026d515873d9999c75c7bc1 |
Hashes for serpyco_rs-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38bfca4927fed1328b0a7d12f56732d35fb07deae4707f2bafc9d5be59501b16 |
|
MD5 | 1837c8e53009682655b68c3e2c3ac70a |
|
BLAKE2b-256 | 978e30d98140dbec1891bcde9a2d762248cfbe0768976f2c6c526b16c90c76f8 |
Hashes for serpyco_rs-0.5.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62a69c4dd66e4db8eb8a9ad1d0fe64cd8e0c79f2260b76d33a65810cbdf0645e |
|
MD5 | 20fef25ddcc576a411972d357a133cf0 |
|
BLAKE2b-256 | 3322abfb6a0cc956a43b745b88449841c4419b9c127b6d1adf8d564717deb2b3 |