cattrs-extras
This package contains advanced converter classes for cattrs, a great serialization library built around attrs.
Key features
- Support for additional types: Decimal, bool, datetime, date, timedelta
- Alternative structuring algorithm capable of handling complex Unions without registering additional hooks
- Human-readable exceptions on structuring failure
- Support for Tortoise ORM models serialization (including relations)
- Additional class and Tortoise field for reversed enumerations (serialized to member name instead of value)
Installation
DEV=0 PYTHON=python make install # remove PYTHON to use pyenv
make build
Usage
from enum import Enum
from decimal import Decimal
from datetime import datetime
from attr import dataclass
from cattrs_extras.converter import Converter
class Color(Enum):
RED = 'RED'
GREEN = 'GREEN'
@dataclass(kw_only=True)
class Apple:
weight: Decimal
color: Color
best_before: datetime
sweet: bool
converter = Converter()
raw_apple = {
'weight': '200.5',
'color': 'RED',
'best_before': '2020-04-02T12:00:00',
'sweet': 'true'
}
apple = converter.structure(raw_apple, Apple)
assert apple == Apple(weight=Decimal('200.5'), color=Color.RED, best_before=datetime(2020, 4, 2, 12, 0), sweet=True)
raw_apple = converter.unstructure(apple)
assert raw_apple == {'weight': '200.5', 'color': 'RED', 'best_before': 1585818000.0, 'sweet': True}
Tortoise ORM
Important note: Tortoise ORM have chosen pydantic as a serialization library so better to stick with it. However pydantic support is still WIP, you can check current status here.
from cattrs_extras.tortoise.converter import TortoiseConverter
from cattrs_extras.tortoise.model import Model
from tortoise import fields
# TODO: ReversedCharEnumField example
class AppleModel(Model):
id = fields.IntField(pk=True)
weight = fields.DecimalField(20, 10)
color = fields.CharEnumField(Color)
best_before = fields.DateField()
sweet = fields.BooleanField()
# NOTE: Replace with module name of your models
tortoise_converter = TortoiseConverter('cattrs_extras.tortoise.model')
apple_model = tortoise_converter.structure(raw_apple, AppleModel)
assert apple_model == AppleModel(weight=Decimal('200.5'), color=Color.RED, best_before=datetime(2020, 4, 2, 12, 0), sweet=True)
raw_apple = tortoise_converter.unstructure(apple_model)
assert raw_apple == {'id': None, 'weight': '200.5', 'color': 'RED', 'best_before': 1585774800.0, 'sweet': True}
Limitations
- PEP 563 – Postponed Evaluation of Annotations is not supported at the moment. Attempt to import
__future__.annotationsin module containing models will lead to exception. However you can still use strings as typehints. - Backward relations in Tortoise models are ignored during structuring even if fetched. Not sure if we should fix it.
Metadata
Release files for cattrs-extras 0.2.0
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 |
|---|---|---|---|---|
| cattrs-extras-0.2.0.tar.gz | 10.0 kB | Without provenance | Details for cattrs-extras-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cattrs_extras-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for cattrs_extras-0.2.0-py3-none-any.whl |
Total release size: 20.5 kB
Release files / cattrs-extras-0.2.0.tar.gz
| Download URL | cattrs-extras-0.2.0.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
05204f7b4ca9f9e74d0142b4e7853b6f9e7af7bf24fda7b322a7229116bfa0ea
|
|
BLAKE2b-256 checksum How to use checksums |
ff8fdefa9bf67b4cf984aea99fa11f65be2777f25ff493a7d416f5979526413c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.10.1 Linux/5.15.10-arch1-1
|
Release files / cattrs_extras-0.2.0-py3-none-any.whl
| Download URL | cattrs_extras-0.2.0-py3-none-any.whl |
|---|---|
| Size | 10.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ffdb2a731c101fdda2381dcb5ea7cee8fb870409644c1c13eea49ad1cd577c22
|
|
BLAKE2b-256 checksum How to use checksums |
aee7f267681fd7d8717cd1814ec09f5508edc41728f5f503e3a40213519f9257
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.10.1 Linux/5.15.10-arch1-1
|
Security for cattrs-extras 0.2.0
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.