Skip to main content

Library for creating a dependency graph of cached properties

Project description

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

    @use_property()
    def color(self) -> int:
        print("Calculating default color")
        return "yellow"

    @use_state()
    def radius(self) -> float:
        print("Calculating default radius")
        return 0.0

    @use_state()
    def height(self) -> float:
        print("Calculating default height")
        return 0.0

    @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()

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

usestate-0.5.0.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

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

usestate-0.5.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file usestate-0.5.0.tar.gz.

File metadata

  • Download URL: usestate-0.5.0.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for usestate-0.5.0.tar.gz
Algorithm Hash digest
SHA256 16096627b59dac5fcc2a015c8b0e13b052ee99aa6bb50451f6e57acabd8a02ab
MD5 5f58c447b99908a7fcff4c35f1b7d6a7
BLAKE2b-256 e32ca0b811f18b8b165b9d516e4ecbeae5daa4f3d52ad4234163d62fb5ae92cb

See more details on using hashes here.

File details

Details for the file usestate-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: usestate-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for usestate-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8cb47c20452859975d7ddb9d1a5f6b424e3c7506cf294d118c067c33f25541ba
MD5 7d51920fa916833c4e97799171f8f96c
BLAKE2b-256 4a845987166584343038caca7e4c474cf177f9402f9703231fcfe2d4557e7571

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