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
git clone https://github.com/danhje/pymesis.git
cd pymesis
pipenv install
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
Built Distribution
File details
Details for the file pymesis-0.0.1.tar.gz
.
File metadata
- Download URL: pymesis-0.0.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0dec13ccd152aa43bf669673c0870a6131862be0fe480238da65e0040a23851c |
|
MD5 | 5da0db16e0a7bb3033094b04855b2b56 |
|
BLAKE2b-256 | 4f8e063b5cce73e11ae2a8173e35cb9c57fa76d46532072fd888dea16e99e89a |
File details
Details for the file pymesis-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: pymesis-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.1 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/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3ec179c059b9992d01b1be210ca78087403fce60c8578b7884a25058b2f39c2 |
|
MD5 | 71d402385f94006360ff4c73b2cb2c72 |
|
BLAKE2b-256 | 18cf22f71bb53a56483b2d80eaac96935aecde6632ef25681165edcfe0304fc0 |