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.

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.0.6.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

pymesis-0.0.6-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pymesis-0.0.6.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for pymesis-0.0.6.tar.gz
Algorithm Hash digest
SHA256 7f7b12ddb22d95b4c7e6add2c61ad03a225c42427e82fd1cbe9691214f1f8936
MD5 8d8136aaee5f763d66bdb3a18c0c16f5
BLAKE2b-256 3030b670a217ea31677af64bed6e3178ffdd302da965f825fe291750a50910c9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pymesis-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for pymesis-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 734462866aa85ec7cc158b36d16399d3d9b67b8d14c8a5ea2355acea0953e81b
MD5 7453f1b1de161973e030cddc76955d08
BLAKE2b-256 d637c7975669ae240869ecd4f0fade4549849a6e538f48b44930bd060f41792b

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