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)
- 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 Alias, 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'))
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
File details
Details for the file serpyco_rs-0.3.0.tar.gz
.
File metadata
- Download URL: serpyco_rs-0.3.0.tar.gz
- Upload date:
- Size: 33.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d848efb9f2c59603ad4f3e770981a42f1c133c100b6905f151efc26c54124bf3 |
|
MD5 | 1721733ff81c5e26818c9bf7be2e0614 |
|
BLAKE2b-256 | 02937d5b4fba89723679be33d2e9f2387e41cf6ae0519af096786b85e180f901 |
File details
Details for the file serpyco_rs-0.3.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: serpyco_rs-0.3.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: PyPy, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02b9667aa899be296eeabff5a07cbd074577085aa388169f8f43951469cb6be2 |
|
MD5 | 708fbbe0009d7bd63c3dbd7319ed3f5a |
|
BLAKE2b-256 | c54a4992cea11cd9779679f8f6dbbd1046db7892da9fba6b3bd9906a996bbd67 |
File details
Details for the file serpyco_rs-0.3.0-cp311-none-win_amd64.whl
.
File metadata
- Download URL: serpyco_rs-0.3.0-cp311-none-win_amd64.whl
- Upload date:
- Size: 183.0 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 625f5b47f10a02a2538eb767a4d9b0e9fb9dae6fd77fb6d5c11359c549172eec |
|
MD5 | 584f428f9ee3b7b506294f20016f0e9d |
|
BLAKE2b-256 | 430a09b1f677f8026ddbab61ecf97a0d6d9eeea2ca5684742c04ffcb5e574525 |
File details
Details for the file serpyco_rs-0.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: serpyco_rs-0.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2be6dc630d7bf6395a677c599f95cf60a325ab59070e72611eeba30c0a4dae71 |
|
MD5 | 434ea0913e5e39c4100c34278ee3d207 |
|
BLAKE2b-256 | c550b2e00b86b245f048c137a877888b370dcd29fec3b8eaca4c6680299aa067 |
File details
Details for the file serpyco_rs-0.3.0-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
.
File metadata
- Download URL: serpyco_rs-0.3.0-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
- Upload date:
- Size: 595.5 kB
- Tags: CPython 3.11, macOS 10.9+ universal2 (ARM64, x86-64), macOS 10.9+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c519dd71bc648cb734e83720cec76451aecf3e0c31752db009e92e677783b95 |
|
MD5 | 6e7910d03647c2987759c73286e6d757 |
|
BLAKE2b-256 | fa7442f8a731eff35aafe07f3ff788bf9d60d48e49ddcd842638352e18d373c1 |
File details
Details for the file serpyco_rs-0.3.0-cp310-none-win_amd64.whl
.
File metadata
- Download URL: serpyco_rs-0.3.0-cp310-none-win_amd64.whl
- Upload date:
- Size: 183.0 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcb3c3f18e5a89f73be9e74cdf98c27b666005ea30074499459e91d84745b662 |
|
MD5 | 96dfee41ca325c521d4ab8fad9517fe1 |
|
BLAKE2b-256 | e88f86f5788d8829054c87a6258eb92699309366fe1490ab6c49e0bfb0e6b226 |
File details
Details for the file serpyco_rs-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: serpyco_rs-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | edbdc2f7f1921f3762bb762ab4d4047373589397e7ec71b3850a89dea64206e3 |
|
MD5 | 0a1bbc259c8e9c023b9fe543e5ec66d3 |
|
BLAKE2b-256 | 9d8672e1762347949da3888b7ded6dce229c7a5839bc3abe4d560a4c484fbbcd |
File details
Details for the file serpyco_rs-0.3.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
.
File metadata
- Download URL: serpyco_rs-0.3.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
- Upload date:
- Size: 595.5 kB
- Tags: CPython 3.10, macOS 10.9+ universal2 (ARM64, x86-64), macOS 10.9+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88910dfcf0a5cb9e57177feb426663597887c06854b84a7b39422592342a44c5 |
|
MD5 | 0e9c9f82a8b2655818e81287df080241 |
|
BLAKE2b-256 | e6a9edfb702255f14bec3806a4f0682df053aa3e15d2a229646a914ce0e595ea |
File details
Details for the file serpyco_rs-0.3.0-cp39-none-win_amd64.whl
.
File metadata
- Download URL: serpyco_rs-0.3.0-cp39-none-win_amd64.whl
- Upload date:
- Size: 183.3 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5971bf12d5b75e50af11c4b01e455c5ccb2d85c1e20e7fb9be8ceee51273cfe3 |
|
MD5 | be7b4841b752be13ef60956edcd1d2a5 |
|
BLAKE2b-256 | d87db5d6c57b9a79d2b5fee99db5eb62c77cd3e2e02cd55cc5e75daa07f1f618 |
File details
Details for the file serpyco_rs-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: serpyco_rs-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bec3c7bec15c0da0b93ae9bb6f4d79d155d4f45986d3d9296b4c46f0b99b79a5 |
|
MD5 | 7ee2eeefd1d3e0bd36ff4162ca60fad4 |
|
BLAKE2b-256 | 3efa82d59bccdffd31fcc67e265ed9d6d6c3af417f6b2cdb5a1b4a818df0a19d |
File details
Details for the file serpyco_rs-0.3.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
.
File metadata
- Download URL: serpyco_rs-0.3.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
- Upload date:
- Size: 596.0 kB
- Tags: CPython 3.9, macOS 10.9+ universal2 (ARM64, x86-64), macOS 10.9+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9dfbdcea1183eb3a5d0f0c3b0a6bfb12905b80295067b1b43e9e0035c0bd61f |
|
MD5 | 0aec9d66e03931d6a88edbfa727b505b |
|
BLAKE2b-256 | a23b80016854367322fe22872e0590bed609131c6fa7e4f8ee6942a05df93cb7 |