Skip to main content

Memoization decorator for Python, with optional TTL (measured in time or function calls) for the cached results.

Project description

pymesis

Memoization decorator for Python, with optional TTL (measured in time or function calls) for the cached results.

Testing and linting codecov GitHub release (latest by date including pre-releases) PyPI

Installation

Using pipenv (recommended):

pipenv install pymesis

Using pip:

pip install pymesis

Usage

Basic usage:

from pymesis import memoize
from time import time, sleep

@memoize
def slowFunction(*args, **kwargs):
    sleep(1)
    return 'Completed'

start = time()
print(slowFunction('some', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # First call is slow

start = time()
print(slowFunction('some', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # Second call is fast, as data is cached

start = time()
print(slowFunction('some', 'new', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # This call is slow, as attributes have changed

With TTL:

from pymesis import memoize, TTLUnit
from time import time, sleep

@memoize(ttl=1, ttl_unit=TTLUnit.CALL_COUNT) # Only return cached result once, then go back to calling flowFunction
def slowFunction(*args, **kwargs):
    sleep(1)
    return 'Completed'

start = time()
print(slowFunction('some', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # First call is slow

start = time()
print(slowFunction('some', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # Second call is fast

start = time()
print(slowFunction('some', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # Third call is slow, as cache has expired (TTL=1).

Note that functions are assumed to be unchanged as long as the name is unchanged. Redefined function (with decorator applied again) will return cached result of similar call to the original function.

The decorator works with methods as well as functions. Note that the same method on two different instances of the same class are considered different methods, therefore a call to the second will not give the cached result from the first.

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

pymesis-0.1.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

pymesis-0.1.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file pymesis-0.1.1.tar.gz.

File metadata

  • Download URL: pymesis-0.1.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

File hashes

Hashes for pymesis-0.1.1.tar.gz
Algorithm Hash digest
SHA256 55a02311588de0903390c031b6e76660b06f6721c16638cb5817055e60bc02cb
MD5 1c6ebb6fd6d759d90f436e550a908393
BLAKE2b-256 65b39d6ca95c4d7addb9745bb5a840fb953477271163f241fb575e6dbe9fe1b1

See more details on using hashes here.

File details

Details for the file pymesis-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pymesis-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

File hashes

Hashes for pymesis-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 47e42fda3a889255e8bfd1b48fd35ab7255730461366cf98001141fc07a297d2
MD5 749b7ca6f1abb47f0e17cc7a671f7ef6
BLAKE2b-256 04b852d060cfe9e6bc0009eb9a159a559ffd37f1ab8ccb2565074f35ad9ec179

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page