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.6.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5b3468294cf9dfa2802b3260ebfbb54eb2818454b5e2a0427f12bdccd067984 |
|
MD5 | 17689dd7a088d415e7906747bb2d66ff |
|
BLAKE2b-256 | 0a1bb7d7feb1135ffd452fda0a53da47625cc2f71d411d65ac210036f3446d16 |
Hashes for serpyco_rs-0.6.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae9dd9da14b10a153941369d420b4367e55998aae61d563bc35b7c9cf4e7dbf2 |
|
MD5 | 68b7f6668100662112eb089d30e1867f |
|
BLAKE2b-256 | a41d0629cba72e7f005f0d519506ade79e58994c0a6417f0e0bb17d36ad4c2e5 |
Hashes for serpyco_rs-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2cb852c7a183eb9d3a7157465577f64277a058c8815ef9784fd9fd6af705748 |
|
MD5 | 4c94b9a82f0d9be9aa25e08c91d16075 |
|
BLAKE2b-256 | 7793cf9135cdcbf9d3ff86793399660fec947bc81118d67cf50fed8bdb8a5b4d |
Hashes for serpyco_rs-0.6.0-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca1315071b6fa9a486554b5d876326e8b75af8f7ec1c33232c30f2fa07da5f49 |
|
MD5 | aea365968420e886c3ded6d7bcec3633 |
|
BLAKE2b-256 | 45cfd3072c5c4605cad3bf6400a823bd4dc1f3e14e84f37d1cf413819f9ad2a1 |
Hashes for serpyco_rs-0.6.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fe3bb9262bab48bd1de46ec1bfc1f99c5fa863baf803d98516165191ac83bba |
|
MD5 | 826bbe75fb7a66000a16eb6fa8c50191 |
|
BLAKE2b-256 | 7645d9c1686c753223c8d3c8989f32a1cc12a35367653ad3728182ca9a13ff83 |
Hashes for serpyco_rs-0.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb6d4e0fefa80c1147f679742c3cc76639bd94efd203a5132842fc1222f3cf0c |
|
MD5 | 98f2da959130396894165e5de682cd41 |
|
BLAKE2b-256 | e2a7301a43d64034b98fe2daa9af3fa84bdd3aa2e134fd93f092acc2984a815a |
Hashes for serpyco_rs-0.6.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3339cf8b7753311e26f0d5aa5c671701f7585c71d2f507bd0874d38c74f7fc81 |
|
MD5 | e025cc74f4d580013e070d0b456bdece |
|
BLAKE2b-256 | 2d2fd9461028cb73331c78f1735b92cae5a51a6c0e9002831866508e00c8a381 |
Hashes for serpyco_rs-0.6.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1afd05cda7fea80a66ea0eb0123ac32909576fa88b472fbf639b83ebf3a8c59 |
|
MD5 | 24f7a0f55f38f7bac6ad56affcbcab31 |
|
BLAKE2b-256 | 89dfce3980f9ec75c844642707e5db851b2ecd28d105f40631d1aa848f35e1c8 |
Hashes for serpyco_rs-0.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 484f3a8d023a7dce19512c26470fad14342e13636b2bcf06844f7066549c0a0a |
|
MD5 | ca351deb1225922cb5e7f7b338d8abb3 |
|
BLAKE2b-256 | de3a4f4a9e60db682a77f76ccd86cdb0c82a5d659f225f344a04e9d422b807d1 |
Hashes for serpyco_rs-0.6.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bceb8c75a16921996d334bb1de003a14d7eda4b5e0e011e79e1e1fcc37301a23 |
|
MD5 | 34aae1d9b40da2c97d458de472ccf6cf |
|
BLAKE2b-256 | 9c68e45336e993463049caf039e0216880756d22a75108487324dbf1e90f5d20 |