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)
- Literal[str, ...]
- Tagged unions (restricted)
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
ser = Serializer(A, omit_none=True) # or Serializer(Annotated[A, OmitNone])
print(ser.dump(A(required_val=None, optional_val=None)))
>>> {'required_val': None}
Tagged unions
Supports tagged joins with discriminator field.
All classes in the union must be dataclasses or attrs with discriminator field Literal[str]
.
The discriminator field is always mandatory.
from typing import Annotated, Literal
from dataclasses import dataclass
from serpyco_rs import Serializer
from serpyco_rs.metadata import Discriminator
@dataclass
class Foo:
type: Literal['foo']
value: int
@dataclass(kw_only=True)
class Bar:
type: Literal['bar'] = 'bar'
value: str
ser = Serializer(list[Annotated[Foo | Bar, Discriminator('type')]])
print(ser.load([{'type': 'foo', 'value': 1}, {'type': 'bar', 'value': 'buz'}]))
>>> [Foo(type='foo', value=1), Bar(type='bar', value='buz')]
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.8.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92d1ac441954bacfb6cafffac78bdebfbfbc772d93a4a7e04cb283dce86fea31 |
|
MD5 | 24af402d34dd2f6b0e9878a51f3ac309 |
|
BLAKE2b-256 | c651698c34745fed88d0c3ab6a92173d0c0e524f3edb485d972e9a3110f02894 |
Hashes for serpyco_rs-0.8.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 018e9f416f96527899a562bb4ca662bb7d2c33f5ff153d2d884e8b8bdc46c38d |
|
MD5 | f525bb3e0b384e85b141ad787aa844a3 |
|
BLAKE2b-256 | 0a51d3995e198fbf091b3993b4f12521a38f887afd0e56b3402e1f47a1f993fb |
Hashes for serpyco_rs-0.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6f9edb30607595387e9772af9058d8c56ef40b4518ad58d6440cd17a9ccd8bc |
|
MD5 | dd678708491d857f9c3fc016ad5cae2c |
|
BLAKE2b-256 | 62721becadb6047a7cf438376413f42500a197e2a9e78515b9b28c4df5ed0034 |
Hashes for serpyco_rs-0.8.0-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6259ffd2e61dbe39bf98431b3cfbb1b6ec6b238df87a766bcfe09467e26bc0c2 |
|
MD5 | a22fd13e80c84678eda4f1283c2da850 |
|
BLAKE2b-256 | fd397537369ab090989713634777ab1c6b2003e389e5ec73197ec3ca431d76e3 |
Hashes for serpyco_rs-0.8.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a354532506200d73d57e2556be3252b6d8c122e1bcc15323906fbd968ecb2b0b |
|
MD5 | 9d83de86638fa39917afcb2c65cd71fa |
|
BLAKE2b-256 | ff775d6595b82651be6347649aeb8f2beae6ebe6890e1a2a708052f32955fe19 |
Hashes for serpyco_rs-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43644ba135656b51451bd261dc5055a0fa69a033f2df595612e7b595d2c48f2c |
|
MD5 | 98a44842848f7085153e587a31056e5d |
|
BLAKE2b-256 | ce5c249ce8db31879d719eff7a84fcb177ad9c2d83be1fd1f2b99b9445c8156e |
Hashes for serpyco_rs-0.8.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c8c31248eba810804cb1985fe5c2afcc085a81a8432a5ba2d48fc5df3f55c44 |
|
MD5 | 94e9cee3b66652eff6fc5abc1e64f690 |
|
BLAKE2b-256 | edafade4085cc90baa0df790c150a1f94c9867a91db64bdcc89c89bbeaa9567f |
Hashes for serpyco_rs-0.8.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad98e8dae067243759316438bc14759603d57871cb3c1e471d39e27538ce8ff8 |
|
MD5 | a2f21856b092bd19e12e8fe5fbacc732 |
|
BLAKE2b-256 | aab369160b02bd1b5296a90d04637a768596db418718b32715b32231491a9a44 |
Hashes for serpyco_rs-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c9ad32fe88fffe85653bd8d053b33b45aa79bc248accc73d2ab47a0464f1d01 |
|
MD5 | b9e81924ea71d05011cd4853ab39f6aa |
|
BLAKE2b-256 | 40920521adb905ec9c3a3f7822d205fc01e6e70a8dcc1ec42a3fb13ddb83be6f |
Hashes for serpyco_rs-0.8.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 843e9a45f9231a13a0fe1191ec49d110e15fbfa670cdbea415acf50301809b18 |
|
MD5 | 25b00454fb6e06fac000d3cba10bb71f |
|
BLAKE2b-256 | 34620e6fdea32a1c3a728e2239a5be2a6479ac01fb801507dc4103facd946e65 |