Skip to main content

No project description provided

Project description

GeoSol Research Logo

Memoize (Class-Based Memoization)

Per-instance memoization for class methods with full type hint and docstring preservation.

Overview

gri-memoize provides a MemoizedClass base class and a @memoize decorator for caching method results within class instances. Unlike functools.lru_cache, which stores results in a global cache tied to the function object, gri-memoize keeps cached results on each instance -- so garbage collection, per-instance invalidation, and IDE tooling all work as expected.

Requires Python 3.14+.

Installation

pip install gri-memoize

For development:

git clone https://gitlab.com/geosol-foss/python/gri-memoize.git
cd gri-memoize
. .init_venv.sh

Quick Start

from gri_memoize import MemoizedClass, memoize


class Geometry(MemoizedClass):
    def __init__(self, radius: float):
        self._radius = radius

    @memoize
    def area(self) -> float:
        """Circle area (cached after first call)."""
        return 3.14159 * self._radius ** 2

    @memoize
    def circumference(self) -> float:
        """Circle circumference (cached after first call)."""
        return 2 * 3.14159 * self._radius


g = Geometry(5.0)
g.area()           # Computed
g.area()           # Returned from cache
g.clear_memoized_results()  # Invalidate all cached results
g.area()           # Recomputed

Why Not functools.lru_cache?

Feature lru_cache gri-memoize
Cache scope Global (per function) Per instance
Garbage collection Cache holds references, preventing GC Cache dies with instance
Cache invalidation cache_clear() clears all instances clear_memoized_results() per instance
IDE support Often loses docstrings and type hints in class context Full @wraps preservation
Argument hashing Uses __hash__ on all args Same, plus function name isolation

Usage

  1. Inherit from MemoizedClass
  2. Decorate methods with @memoize
  3. Call clear_memoized_results() if the underlying data changes
from gri_memoize import MemoizedClass, memoize


class Sensor(MemoizedClass):
    def __init__(self, readings: list[float]):
        self._readings = readings

    @memoize
    def mean(self) -> float:
        return sum(self._readings) / len(self._readings)

    @memoize
    def variance(self) -> float:
        m = self.mean()
        return sum((x - m) ** 2 for x in self._readings) / len(self._readings)

    def update(self, readings: list[float]) -> None:
        self._readings = readings
        self.clear_memoized_results()  # Invalidate stale cache

Methods with arguments are cached per unique argument combination:

class Matrix(MemoizedClass):
    @memoize
    def power(self, n: int) -> float:
        return self._value ** n

m = Matrix()
m.power(2)  # Cached for n=2
m.power(3)  # Cached separately for n=3

How It Works

Each MemoizedClass instance carries a memoized_results dictionary, initialized in __new__ to support multiple inheritance. The @memoize decorator hashes the function name, positional args, and sorted keyword args into a key. On cache hit, the stored result is returned without calling the function.

Dependencies

None. gri-memoize has no external dependencies.

Other Projects

Current list of other GRI FOSS Projects we are building and maintaining.

License

MIT License. See LICENSE for details.

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

gri_memoize-0.2.0.post1.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

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

gri_memoize-0.2.0.post1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file gri_memoize-0.2.0.post1.tar.gz.

File metadata

  • Download URL: gri_memoize-0.2.0.post1.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gri_memoize-0.2.0.post1.tar.gz
Algorithm Hash digest
SHA256 b0bc81d54df8bf2f94407e25aebc36c78fbdd84c9cc54edb8f4e7260b9f9687f
MD5 b88f3a85134345afc1e7e350acc4538e
BLAKE2b-256 7c4ee50a3740c9f01757fbaa3b73892bf64f9679aff4102f5ab3f4b71fa2d7a8

See more details on using hashes here.

File details

Details for the file gri_memoize-0.2.0.post1-py3-none-any.whl.

File metadata

  • Download URL: gri_memoize-0.2.0.post1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gri_memoize-0.2.0.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 6cf62675a3389df09fb2c8040d3c3888193cb8b427e964f3496dbe48fc44dfb9
MD5 509d1450b596945a596c3be7660c2df5
BLAKE2b-256 299dff85b7d76bd01dfcf21dd40d8cdebe6faf838f877fee7b61716e1ecc626f

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