Skip to main content

A collection of reusable Python patterns (starting with a Multiton with TTL-based cache expiry)

Project description

rarg-python-patterns

A small, self-contained collection of reusable Python patterns. The first (and currently only) pattern is a Multiton: an implementation of the Multiton pattern with TTL-based cache expiry, importable from rarg_python_patterns.multiton.

Multiton

A Multiton wraps a factory function and its arguments. The underlying instance is created lazily on first access to .instance and cached, keyed on the (frozen, hashable) factory and arguments. Any Multiton constructed with the same factory and arguments shares the cached instance. Multiton objects are themselves hashable, equality-comparable and pickleable (with pickle, cloudpickle and dill), provided their arguments are too.

Cached instances expire after ttl seconds of inactivity; accessing .instance resets the TTL. Expired entries are swept on every access via a min-heap ordered by expiry time.

from rarg_python_patterns import Multiton


def open_connection(url: str, timeout: float = 1.0) -> Connection:
    ...


# The resource is only created when `.instance` is first accessed.
resource = Multiton(open_connection, "https://www.python.org", timeout=10.0)
response = resource.instance.request("GET", "/foo/bar.html")

# Override the default TTL (300s) per-instance.
resource = Multiton(open_connection, "https://www.python.org").with_args(ttl=60.0)

Installation

pip install rarg-python-patterns

The package has no required runtime dependencies. numpy is an optional dependency: when installed, numpy.ndarray factory arguments are frozen by their bytes, shape and dtype so they can be used as cache keys.

Development

This package uses uv.

uv sync
uv run --group test py.test tests/
uv run --dev pre-commit run -a

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

rarg_python_patterns-0.0.3.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

rarg_python_patterns-0.0.3-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file rarg_python_patterns-0.0.3.tar.gz.

File metadata

  • Download URL: rarg_python_patterns-0.0.3.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for rarg_python_patterns-0.0.3.tar.gz
Algorithm Hash digest
SHA256 fb4d2a8c46510ca6a4af4a74c72af1558ef0f30bff04bb77d05d98b9335dd333
MD5 5c6f6e53d16c1f4fbac268337ad2218a
BLAKE2b-256 096c3bb2b9db559d5107295d8782f477ae9f58efe197ae3fff3113d30deeb41a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rarg_python_patterns-0.0.3.tar.gz:

Publisher: ci.yml on ratt-ru/rarg-python-patterns

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rarg_python_patterns-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for rarg_python_patterns-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6411d9c7ec76d671154dedea54db19f11a04f35ec397d3ed26bd0c92505b0934
MD5 cc801ce4e71a27f13df0277ff7a36e03
BLAKE2b-256 303fb581d56ff903510242434cd978b36eb95f15406ab31b581f64c24fbf5cb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for rarg_python_patterns-0.0.3-py3-none-any.whl:

Publisher: ci.yml on ratt-ru/rarg-python-patterns

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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