cattrs-extras-0.2.0
توضیحات
Advanced converters for cattrs
| ویژگی | مقدار |
|---|---|
| سیستم عامل | - |
| نام فایل | cattrs-extras-0.2.0 |
| نام | cattrs-extras |
| نسخه کتابخانه | 0.2.0 |
| نگهدارنده | [] |
| ایمیل نگهدارنده | [] |
| نویسنده | Lev Gorodetskiy |
| ایمیل نویسنده | github@droserasprout.space |
| آدرس صفحه اصلی | https://github.com/droserasprout/cattrs-extras |
| آدرس اینترنتی | https://pypi.org/project/cattrs-extras/ |
| مجوز | - |
# cattrs-extras
This package contains advanced converter classes for [cattrs](https://github.com/Tinche/cattrs), a great serialization library built around [attrs](https://github.com/python-attrs/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
```shell-script
DEV=0 PYTHON=python make install # remove PYTHON to use pyenv
make build
```
## Usage
```python
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](https://github.com/samuelcolvin/pydantic) as a serialization library so better to stick with it. However pydantic support is still WIP, you can check current status [here](https://tortoise-orm.readthedocs.io/en/latest/contrib/pydantic.html).
```python
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](https://www.python.org/dev/peps/pep-0563/) is not supported at the moment. Attempt to import `__future__.annotations` in 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.
نیازمندی
| مقدار | نام |
|---|---|
| >=1.9.0,<2.0.0 | cattrs |
| >=0.6.12,<0.7.0 | dateutils |
| >=1.1.8,<2.0.0 | pytimeparse |
| >=0.18.0,<0.19.0) | tortoise-orm |
| >=4.0.1,<5.0.0 | typing-extensions |
| >=0.7.1,<0.8.0 | typing-inspect |
زبان مورد نیاز
| مقدار | نام |
|---|---|
| >=3.8,<4.0 | Python |
نحوه نصب
نصب پکیج whl cattrs-extras-0.2.0:
pip install cattrs-extras-0.2.0.whl
نصب پکیج tar.gz cattrs-extras-0.2.0:
pip install cattrs-extras-0.2.0.tar.gz