Skip to main content

pytracked

Reactive memoization with dependency-driven invalidation for Python.

Like functools.cache, but with automatic invalidation: when an input changes, all computed values that depend on it are flushed and lazily recomputed on next access.

Features

  • tracked — memoized attribute with automatic dependency recording
  • token — mutable input; writing a new value flushes all dependents transitively
  • circular — fixed-point iteration for mutually recursive computations
  • flush — manually invalidate cached values
  • observe — trace evaluation order and profile cache efficiency

Quick start

from pytracked import tracked, token

class Spreadsheet:
    a = token(1)
    b = token(2)
    total = tracked(lambda self: self.a + self.b)
    label = tracked(lambda self: f"Total: {self.total}")

s = Spreadsheet()
s.total    # → 3 (computed, cached)
s.total    # → 3 (cache hit)
s.a = 10   # flushes total and label transitively
s.total    # → 12 (recomputed)
s.label    # → "Total: 12" (recomputed)

Fixed-point computation

from pytracked import tracked, circular

class Grammar:
    # Compute nullable set iteratively until stable
    nullable = circular(lambda self: self._compute_nullable(), bottom=frozenset)

    def _compute_nullable(self):
        result = set()
        for rule in self.rules:
            if all(sym in self.nullable for sym in rule.rhs):
                result.add(rule.lhs)
        return frozenset(result)

How it works

During evaluation of a tracked attribute, any access to another tracked or token attribute is recorded as a dependency edge. When a token is written, the dependency graph is walked transitively to flush all stale cached values. Re-evaluation happens lazily on next access.

This implements the algorithm from:

Söderberg & Hedin, "Incremental Evaluation of Reference Attribute Grammars using Dynamic Dependency Tracking", LU-CS-TR:2012-249.

Metadata

Release files for pytracked 0.1.2

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

Source distribution (sdist)

Source distribution for pytracked 0.1.2
File Size Uploaded
pytracked-0.1.2.tar.gz 12.1 kB Details

Built distribution (wheel)

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

Total release size: 24.0 kB

Release files / pytracked-0.1.2.tar.gz

Download URL pytracked-0.1.2.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
76143adb60d520cd12a0c102ea5846fae165eb7b721887953f38ee1bb3e29497
BLAKE2b-256 checksum
How to use checksums
4e0b4b46b36c47d08b8922e37666ef4ad3353de2b8aa1454bc93c50b05dad374
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / pytracked-0.1.2-py3-none-any.whl

Download URL pytracked-0.1.2-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a0215020132b7cbde8852ac6270c97f6d32bc79ab82d372ea863c3b5cf1e714d
BLAKE2b-256 checksum
How to use checksums
24b3463e377bb4fff1fd50c1dd72f5e2d045896b19c042c8755811f1ccfb4e8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.2 This release

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