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.4.tar.gz (8.0 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.4-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rarg_python_patterns-0.0.4.tar.gz
  • Upload date:
  • Size: 8.0 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.4.tar.gz
Algorithm Hash digest
SHA256 9139450d82cc178c9e310f65b4fabf523ba4fbf5ea79af5cdcb16f521a116cce
MD5 3880187db1bedf674a770bddac0b1aa0
BLAKE2b-256 feb53e1624b95101c445ca554049108c4abc47a18dabdb260555157bdb49b45a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for rarg_python_patterns-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8b3cbfee87d591d8340ef89ffd9795c1c49d59ef89bb93b6037186e3f81a9064
MD5 972204b18f6f824f508139f35194cea6
BLAKE2b-256 868ee9c1e9d1f2f2c7992aeda34ca2e44e05372c7aedadd58fe8b03e4f6548a9

See more details on using hashes here.

Provenance

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