Skip to main content

UseState

Wrap your class's instance methods with @use_state, @use_property, and @use_lazy_generated_state to create a dependency graph of cached properties that only regenerate when their dependencies change.

Uses Python's data descriptors to proxy (to the graph) access of properties. Graph nodes are stored on the instances of classes that use the decorators, for sane garbage collection sake.

Installing

Available on PyPI as UseState

$ pip install UseState

Examples

See examples folder

#!/usr/bin/env python3

import math

from UseState import use_state, use_property, use_lazy_generated_state

class Cylinder:
    def __init__(self, radius: float, height: float) -> None:
        self.radius = radius
        self.height = height

    color: str = use_property()

    radius: float = use_state()

    height: float = use_state()

    @use_lazy_generated_state({"radius"})
    def area(self) -> float:
        print("Calculating area")
        return self.radius * self.radius * math.pi

    @use_lazy_generated_state({"area", "height"})
    def volume(self) -> float:
        print("Calculating volume")
        return self.area * self.height

def main() -> None:
    c = Cylinder(4.0, 8.0)

    print(c.area)
    # Calculating area
    # 50.26548245743669

    print(c.volume)
    # Calculating volume
    # 402.1238596594935

    c.radius = 4.0 # doesn't cause invalidation because value is same

    print(c.volume)
    # 402.1238596594935

    c.radius = 1.0 # invalidates area, which invalidates volume

    print(c.volume)
    # Calculating area
    # Calculating volume
    # 25.132741228718345

    print(c.area) # cached by previous call to volume
    # 3.141592653589793

    print(c.volume)
    # 25.132741228718345

if __name__ == "__main__":
    main()

Metadata

Release files for UseState 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for UseState 0.6.0
File Size Uploaded
usestate-0.6.0.tar.gz 17.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for UseState 0.6.0
File Interpreter ABI Platform
usestate-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.7 kB

Release files / usestate-0.6.0.tar.gz

Download URL usestate-0.6.0.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
85095662fd64a6960f4c667f609022d9e0141370ce575a3329cbe35269142230
BLAKE2b-256 checksum
How to use checksums
18618d1da4a0a0773b6c91423477d911c9db0f51cecc4ae98458263f548ca8ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / usestate-0.6.0-py3-none-any.whl

Download URL usestate-0.6.0-py3-none-any.whl
Size 19.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e67815f97637f4af04324c5520085e9e52c8d4ff81c7eadaa5f0b04e5b814d0
BLAKE2b-256 checksum
How to use checksums
d4a76819a9f1e3157d5de044378a22c83f1f61ed840fd16479d87edee6c3f005
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page