Skip to main content

Advanced converters for cattrs

Project description

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__.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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cattrs-extras-0.1.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

cattrs_extras-0.1.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file cattrs-extras-0.1.1.tar.gz.

File metadata

  • Download URL: cattrs-extras-0.1.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.10.0-pf9

File hashes

Hashes for cattrs-extras-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1b3a129bb044884acfade9d27061014cd40a5efafa63556588d687070459ef90
MD5 344a43e41ad80b088bc7dcc78906dada
BLAKE2b-256 86b21973698d367cb4342f4582701d15126d758d85a0e73b56592d53f4d8ce66

See more details on using hashes here.

File details

Details for the file cattrs_extras-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: cattrs_extras-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.10.0-pf9

File hashes

Hashes for cattrs_extras-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8c6a70325590df6924bd2ef442b492fb9d754e16addb3a4214fba1e665281562
MD5 b0554adb1a91ada57f7ed77a3a9d46f7
BLAKE2b-256 61f732f4ab4bc6ec6c5be307e9d8f3f34cb3a9e9abe2c13085ea2a197afd2f2b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page