attrs-serde
A serialization addon for attrs.
person_dict = {"contact": {"personal": {"name": "John"}, "phone": "555-112233"}}
name_path = ["contact", "personal", "name"]
phone_path = ["contact", "phone"]
@serde
@attrs
class Person(object):
name = attrib(metadata={"to": name_path, "from": name_path})
phone = attrib(metadata={"to": phone_path, "from": phone_path})
>>> p = Person.from_dict(person_dict)
Person(name=John phone=555-112233)
>>> p.to_dict
{"contact": {"personal": {"name": "John"}, "phone": "555-112233"}}
Quick Start
Install using pip/pipenv/etc. (we recommend poetry for sane dependency management):
$ poetry add attrs-serde
Decorate with serde for automatic to_dict and from_dict. Provide paths in metadata:
from- path to fetch field value fromto- path to serialize value into (creates nested dictionaries as needed)
Example:
from attrs_serde import serde
from attr import attrs, attrib
@serde
@attrs
class Person(object):
name = attrib(metadata={"to": name_path, "from": name_path})
phone = attrib(metadata={"to": phone_path, "from": phone_path})
Custom from and to keys (in case you or a different extension use those):
from attrs_serde import serde
from attr import attrs, attrib
@serde(from_key="get", to_key="set")
@attrs
class Person(object):
name = attrib(metadata={"get": name_path, "set": name_path})
phone = attrib(metadata={"get": phone_path, "set": phone_path})
Performance
attrs-serde works with cytoolz (mostly C implementation) and so presents very low overhead over what attrs already does.
Against manual object construction:
------------------------------------------------------------------------------------- benchmark 'deserialization': 2 tests ------------------------------------------------------------------------------------
Name (time in ns) Min Max Mean StdDev Median IQR Outliers OPS (Kops/s) Rounds Iterations
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_deser_baseline 583.2500 (1.0) 5,037.3500 (1.0) 641.4743 (1.0) 161.4237 (1.0) 603.8500 (1.0) 33.7500 (1.0) 2315;3276 1,558.9089 (1.0) 77828 20
test_deser_serde 1,976.0000 (3.39) 88,504.0000 (17.57) 2,226.3774 (3.47) 1,195.7336 (7.41) 2,127.0000 (3.52) 110.0000 (3.26) 484;1576 449.1601 (0.29) 86806 1
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Serialization against attr's asdict:
-------------------------------------------------------------------------- benchmark 'serialization': 2 tests --------------------------------------------------------------------------
Name (time in us) Min Max Mean StdDev Median IQR Outliers OPS (Kops/s) Rounds Iterations
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_ser_baseline 2.6600 (1.0) 130.4550 (1.33) 2.9098 (1.0) 1.3230 (1.0) 2.7940 (1.0) 0.1320 (1.0) 302;882 343.6625 (1.0) 46642 1
test_ser_serde 5.0390 (1.89) 98.4540 (1.0) 5.6411 (1.94) 2.2398 (1.69) 5.4465 (1.95) 0.2890 (2.19) 491;912 177.2706 (0.52) 32936 1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Thanks:
To all Contributors - you make this happen, thanks!
Copyright
Copyright (c) 2018 @jondot. See LICENSE for further details.
Metadata
Release files for attrs-serde 0.2.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| attrs-serde-0.2.4.tar.gz | 4.2 kB | Without provenance | Details for attrs-serde-0.2.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| attrs_serde-0.2.4-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for attrs_serde-0.2.4-py2.py3-none-any.whl |
Total release size: 8.4 kB
Release files / attrs-serde-0.2.4.tar.gz
| Download URL | attrs-serde-0.2.4.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ae820a338cd9dc5aa832a66d13d0193006be96ceb43b331708636566d002efba
|
|
BLAKE2b-256 checksum How to use checksums |
8c3bc840ffc3f76531c9c39786e732e25ea60b6283d0e8becb14c293eeef683f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/0.12.17 CPython/3.7.3 Darwin/19.0.0
|
Release files / attrs_serde-0.2.4-py2.py3-none-any.whl
| Download URL | attrs_serde-0.2.4-py2.py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c7a8e451f041a1f93bf9ad7477fe5e5ff2d1ad7219c6b35dd91e3e2485caa84c
|
|
BLAKE2b-256 checksum How to use checksums |
2889e4e25e3003c1017634bb5e021019012c0dfc2f65e6217f9ac7d935ee4053
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/0.12.17 CPython/3.7.3 Darwin/19.0.0
|
Security for attrs-serde 0.2.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.