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.2.tar.gz (6.3 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.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rarg_python_patterns-0.0.2.tar.gz
  • Upload date:
  • Size: 6.3 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.2.tar.gz
Algorithm Hash digest
SHA256 1302c591efd730a0df16b20c4fc0e0187a3a107f74f56a50738f15d00305d1ec
MD5 fe7cbcb9ea181ac1d0393620ae3ac606
BLAKE2b-256 567d1ee41bcbe6cc4905dbdc4f8ef4606f45a11e9da8f692b7a5ca79550ecf8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rarg_python_patterns-0.0.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for rarg_python_patterns-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a65a72850d1eaf80b2137ac1ab162c739ac990727cd197bd59882c5b10bebe1d
MD5 bbc27fbdde47280f7159c32483247e42
BLAKE2b-256 58bf3383d024614a36494a2e6195e457018f8dd27a52275b4f997b83186c2e93

See more details on using hashes here.

Provenance

The following attestation bundles were made for rarg_python_patterns-0.0.2-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