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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| usestate-0.6.0.tar.gz | 17.7 kB | Without provenance | Details for usestate-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| usestate-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for usestate-0.6.0-py3-none-any.whl |
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
|
Security for UseState 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.