Skip to main content

Declarively apply converter functions to class attributes.

Project description

Exert

>> Declaratively apply converter functions to class attributes. <<

Installation

Install via pip:

pip install exert

Usage

Use this to declaratively apply arbitrary converter functions to the attributes of a class. For example:

from __future__ import annotations

from typing import Annotated
from exert import exert, Mark


@exert
class Foo:
    a: Annotated[int, Mark(lambda x: x**2)]
    b: Annotated[float, Mark(lambda x: x / 2)]

    def __init__(self, a: int, b: float) -> None:
        self.a = a
        self.b = b


foo = Foo(2, 42.0)

print(foo.a) # prints 4
print(foo.b) # prints 21.0

Here, the lambda function tagged with Mark is the converter.

Use with dataclasses

Dataclasses can also be used to avoid writing the initializer by hand. For example:

...

from dataclasses import dataclass


@exert
@datclasses
class Foo:
    a: Annotated[int, Mark(lambda x: x**2)]
    b: Annotated[float, Mark(lambda x: x / 2)]


foo = Foo(2, 42.0)

print(foo.a) # prints 4
print(foo.b) # prints 21.0

Apply multiple converters sequentially

Multiple converters are allowed. For example:

...

@exert
@dataclass
class Foo:
    a: Annotated[int, Mark(lambda x: x**2, lambda x: x**3)]
    b: Annotated[float, Mark(lambda x: x / 2, lambda x: x / 3)]


foo = Foo(2, 42.0)

print(foo.a) # prints 64  [2**2=4, 4**3=64]
print(foo.b) # prints 7.0 [42.0/2=21.0, 21.0/3=7.0]

Here, the converters are applied sequentially. The result of the preceding converter is fed into the succeeding converter as input. You've to make sure that the number of the returned values of the preceding converter matches that of the succeeding converter.

Exclude annotated fields

If you don't wrap converters with Mark, the corresponding field won't be transformed:

...

@exert
@dataclass
class Foo:
    a: Annotated[int, Mark(lambda x: x**2, lambda x: x**3)]
    b: Annotated[float, lambda x: x / 2, lambda x: x / 3]


foo = Foo(2, 42.0)

print(foo.a)  # prints 64  [2**2=4, 4**3=64]
print(foo.b)  # prints 42.0 [This field was ignored]

Since the converters in field b weren't tagged with Mark, no conversion happened.

Apply common converters without repetition

Common converters can be applied to multiple fields without repetition:

...

@exert(converters=(lambda x: x**2,))
@dataclass
class Foo:
    a: Annotated[int, None]
    b: Annotated[float, None]


foo = Foo(2, 42.0)

print(foo.a)  # prints 4      [2**2=4]
print(foo.b)  # prints 1764.0 [42.0**2=1764.0]

Apply common and marked converters together

You can apply a sequence of common converters and marked converters together. By default, the common converters are applied first and then the tagged converters are applied sequentially:

...

@exert(converters=(lambda x: x**2, lambda x: x**3))
@dataclass
class Foo:
    a: Annotated[int, Mark(lambda x: x / 100)]
    b: Annotated[float, None]


foo = Foo(2, 42.0)

print(foo.a)  # prints 0.64         [2**2=4, 4**3=64, 64/100=0.64]
print(foo.b)  # prints 5489031744.0 [42.0**2=1764, 1764**3=5489031744.0]

You can also, choose to apply the common converters after the tagged ones. For this, you'll need to set the apply_last parameter to True:

...

@exert(
    converters=(lambda x: x**2, lambda x: x**3),
    apply_last=True,
)
@dataclass
class Foo:
    a: Annotated[int, Mark(lambda x: x / 100)]
    b: Annotated[float, None]


foo = Foo(2, 42.0)

print(foo.a)  # prints 6.401e-11 [2/100=0.02, 0.02**2=0.004, 0.0004**3=6.401e-11]
print(foo.b)  # prints 5489031744.0 [42.0**2=1764, 1764**3=5489031744.0]

Simple data validation

The snippet below ensures that the attributes of the class Foo conforms to the constraints imposed by the converter functions.

from __future__ import annotations

import json
from dataclasses import dataclass
from functools import partial
from typing import Annotated, Sized

from exert import Mark, exert


def assert_len(x: Sized, length: int) -> Sized:
    """Assert that the incoming attribute has the expected length."""

    assert len(x) == length
    return x


def assert_json(x: dict) -> dict:
    """Assert that the incoming attribute is JSON serializable."""

    try:
        json.dumps(x)
    except TypeError:
        raise AssertionError(f"{x} is not JSON serializable")
    return x


@exert
@dataclass
class Foo:
    a: Annotated[tuple[int, ...], Mark(partial(assert_len, length=3))]
    b: Annotated[dict, Mark(assert_json)]


foo = Foo(a=(1, 2, 3), b={"a": 1, "b": 2})
print(foo)

This will raise AssertionError if the value of the attributes violate the constraints.

✨ 🍰 ✨

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

exert-0.3.3.tar.gz (5.9 kB view hashes)

Uploaded Source

Built Distributions

exert-0.3.3-cp311-cp311-musllinux_1_1_x86_64.whl (93.3 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

exert-0.3.3-cp311-cp311-musllinux_1_1_i686.whl (93.3 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

exert-0.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (97.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

exert-0.3.3-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (97.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

exert-0.3.3-cp311-cp311-macosx_10_9_x86_64.whl (53.3 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

exert-0.3.3-cp310-cp310-musllinux_1_1_x86_64.whl (93.2 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

exert-0.3.3-cp310-cp310-musllinux_1_1_i686.whl (93.9 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

exert-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (97.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

exert-0.3.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (97.5 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

exert-0.3.3-cp310-cp310-macosx_10_9_x86_64.whl (53.9 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

exert-0.3.3-cp39-cp39-musllinux_1_1_x86_64.whl (93.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

exert-0.3.3-cp39-cp39-musllinux_1_1_i686.whl (93.7 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

exert-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (97.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

exert-0.3.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (97.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

exert-0.3.3-cp39-cp39-macosx_10_9_x86_64.whl (53.9 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

exert-0.3.3-cp38-cp38-musllinux_1_1_x86_64.whl (92.3 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

exert-0.3.3-cp38-cp38-musllinux_1_1_i686.whl (92.9 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

exert-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (96.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

exert-0.3.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (96.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

exert-0.3.3-cp38-cp38-macosx_10_9_x86_64.whl (53.4 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

exert-0.3.3-cp37-cp37m-musllinux_1_1_x86_64.whl (79.8 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

exert-0.3.3-cp37-cp37m-musllinux_1_1_i686.whl (81.8 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

exert-0.3.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (82.7 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

exert-0.3.3-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (84.0 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

exert-0.3.3-cp37-cp37m-macosx_10_9_x86_64.whl (51.8 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

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