Skip to main content

dataclasses with verifiers/converters

Project description

rx-dataclasses

type verification and convert

from typing import *
from rx.operators import map as rxmap
from rxdata import dataclass, field, operators

@rxmap
def try_convert(n):
    return int(n) if isinstance(n, str) and n.isalpha() else n

@dataclass
class Data:
    # only int.
    number: int                         = field(default=1,
                                                invoke=operators.typeguard())

    # only int or string convertable to int. Always results in int.
    convert_or_die: Union[int]          = field(default=1,
                                                invoke=[try_convert,
                                                        operators.typeguard()])

    # If is string and convertable to int - perform conversion. `typing.Any` type defined explicitly during `invoke` (just for example).
    convert_or_accept: Union[int, str]  = field(default=1,
                                                invoke=[try_convert,
                                                        operators.typeguard(Any)])


> data = Data(None)
TypeError: type of number must be int; got NoneType instead

> data = Data(convert_or_die=None)
TypeError: type of convert_or_die must be int; got NoneType instead

> Data(convert_or_accept=None)
Data(number=1, convert_or_die=1, convert_or_accept=None)


data = Data()

data.number = '3'
> TypeError: type of number must be int; got str instead

data.number = 2
> Data(number=2, convert_or_die=1, convert_or_accept=1)

data.convert_or_die = 2
> Data(number=2, convert_or_die=2, convert_or_accept=1)

data.convert_or_accept = '3'
> Data(number=2, convert_or_die=2, convert_or_accept=3)

data.convert_or_accept = '3s'
> Data(number=2, convert_or_die=2, convert_or_accept='3s')

data.convert_or_accept = type
> Data(number=2, convert_or_die=2, convert_or_accept=<class 'type'>)

description

reactive python3 dataclasses - with attrs-like behaviour

  • implicit and explicit typing verification during both init and/or setattr
  • customize preprocessing (convert) during both init and/or setattr
  • reactive attributes

install

> pip3 install rx-dataclasses

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

rx-dataclasses-0.0.4.tar.gz (18.2 kB view details)

Uploaded Source

File details

Details for the file rx-dataclasses-0.0.4.tar.gz.

File metadata

  • Download URL: rx-dataclasses-0.0.4.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.5

File hashes

Hashes for rx-dataclasses-0.0.4.tar.gz
Algorithm Hash digest
SHA256 2e84cb066b0aa4bd6b2e75c8f2b35c1c03977ae018ef41fc8158595424e767cc
MD5 663e5726b542fa6e69214c7efaf918d4
BLAKE2b-256 840b5533bd82be30e6ed9d904dcff8a85cf61bbadebeeba5dfb636e16cd8c463

See more details on using hashes here.

Supported by

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