Skip to main content

Change tracking library for frozen dataclasses

Project description

effect

CI CD GitHub Release Lines

Change tracking library for frozen dataclasses.

Get

pip install effectt

Example

from dataclasses import dataclass

from effect import (
    Effect, Identified, LifeCycle, New, existing, just, new, mutated, Existing
)


@dataclass(kw_only=True, frozen=True, slots=True)
class A(Identified[str]):
    id: str
    line: str


@dataclass(kw_only=True, frozen=True, slots=True)
class X(Identified[str]):
    id: str
    a: A | None
    number: int


type SomeX = (
    Existing[X]  # No effects / changes
    | New[X]  # Only `New[X]`
    | Effect[X, A, X]  # `New[A]` and `Mutated[X]`
)


def some_x_when(*, x: X | None, number: int) -> SomeX:
    if x is None:
        return new(X(id="X", a=None, number=number))

    if x.a is not None:
        return existing(x)

    a = new(A(id="A", line=str()))
    x = mutated(X(
        id=x.id,
        number=number,
        a=just(a),
    ))

    return a & x

    # Or without `just`:
    # return a.then(lambda a: mutated(X(
    #     id=x.id,
    #     number=number,
    #     a=a,
    # )))


x = X(id="X", number=4, a=None)

some_x: LifeCycle[X | A]  # All effects / changes for X and A
some_x = some_x_when(x=x, number=8)

assert just(some_x).number == 8
assert some_x.new_values == (A(id='A', line=''),)
assert some_x.mutated_values == (X(id='X', a=A(id='A', line=''), number=4),)
assert some_x.deleted_values == tuple()

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

effectt-0.1.1.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

effectt-0.1.1-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file effectt-0.1.1.tar.gz.

File metadata

  • Download URL: effectt-0.1.1.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.9

File hashes

Hashes for effectt-0.1.1.tar.gz
Algorithm Hash digest
SHA256 456c56b1e6935ba3aef22168fb618bacb9908587c57a7aea0da4154e0789ec27
MD5 84b5dcb044a6ccd3b0c65931e40ed5a4
BLAKE2b-256 3830ea51f40bdb13406d735b2e1ee7034587083a765fd1af397a7b83e297da04

See more details on using hashes here.

File details

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

File metadata

  • Download URL: effectt-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.9

File hashes

Hashes for effectt-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 25033fb842716564803a5d8774654bec743a8ad0b1e9733d1d6a8fe69d7d439f
MD5 a4cd15d9bb6855f26351e0f38d1ef74c
BLAKE2b-256 651ca068bf70f4c7263f41c9dbfbc1fab10c7c439e64958c52c90900be63a442

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