a simple decorator to cache the results of computationally heavy functions
Project description
A simple decorator to cache the results of computationally heavy functions. The package automatically serialize and deserialize depending on the format of the save path.
Currently it supports:
.json
.json.gz
.json.bz
.json.lzma
.pkl
.pkl.gz
.pkl.bz
.pkl.lzma
.pkl.zip
.npy
.npz
.csv
.csv.gz
.csv.bz2
.csv.zip
.csv.xz
.xlsx
How do I install this package?
As usual, just download it using pip:
pip install cache_decorator
Tests Coverage
Since some software handling coverages sometime get slightly different results, here’s three of them:
Examples of Usage
To cache a function or a method you just have to decorate it with the cache decorator.
from time import sleep
from cache_decorator import Cache
@Cache()
def x(a, b):
sleep(3)
return a + b
class A:
@Cache()
def x(self, a, b):
sleep(3)
return a + b
By default the cache is differentiate by the parameters passed to the function. One can specify which parameters should be ignored.
from time import sleep
from cache_decorator import Cache
@Cache(args_to_ignore=["verbose"])
def x(a, verbose=False):
sleep(3)
if verbose:
print("HEY")
return a
Multiple arguments can be specified as a list of strings with the name of the arguments to ignore.
from time import sleep
from cache_decorator import Cache
@Cache(args_to_ignore=["verbose", "multiprocessing"])
def x(a, verbose=False, multiprocessing=False):
sleep(3)
if verbose:
print("HEY")
return a
The default cache directory is ./cache but this can be setted by passing the cache_dir parameter to the decorator or by setting the environment variable CACHE_DIR. In the case both are setted, the parameter folder has precedence over the environment one.
from time import sleep
from cache_decorator import Cache
@Cache(cache_dir="/tmp")
def x(a):
sleep(3)
return a
The path format can be modified by passing the cache_path parameter. This string will be formatted with infos about the function, its parameters and, if it’s a method, the self attributes.
De default path is:
from time import sleep
from cache_decorator import Cache
@Cache(cache_path="{cache_dir}/{file_name}_{function_name}/{_hash}.pkl")
def x(a):
sleep(3)
return a
But can be modified giving cache a more significative name, for example we can add the value of a into the file name.
from time import sleep
from cache_decorator import Cache
@Cache(cache_path="{cache_dir}/{file_name}_{function_name}/{a}_{_hash}.pkl")
def x(a):
sleep(3)
return a
Depending on the extension of the file, different serialization and deserialization dispatcher will be called.
from time import sleep
from cache_decorator import Cache
@Cache(cache_path="/tmp/{_hash}.pkl.gz")
def x(a):
sleep(3)
return a
@Cache(cache_path="/tmp/{_hash}.json")
def x(a):
sleep(3)
return {"1":1,"2":2}
@Cache(cache_path="/tmp/{_hash}.npy")
def x(a):
sleep(3)
return np.array([1, 2, 3])
@Cache(cache_path="/tmp/{_hash}.npz")
def x(a):
sleep(3)
return np.array([1, 2, 3]), np.array([1, 2, 4])
Cache also might have a validity duration.
from time import sleep
from cache_decorator import Cache
@Cache(
cache_path="/tmp/{_hash}.pkl.gz",
validity_duration="24d"
)
def x(a):
sleep(3)
return a
In this example the cache will be valid for the next 24 days. and on the 25th day the cache will be rebuilt. The duration can be written as a time in seconds or as a string with unit. The units can be “s” seconds, “m” minutes, “h” hours, “d” days, “w” weeks.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file cache_decorator-1.1.7.tar.gz
.
File metadata
- Download URL: cache_decorator-1.1.7.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1ec8788ee2972912146846d5f2b4f40bd1567bbfd4411271ffba332300d558c |
|
MD5 | bceb327652fe00eb4b38bcfcc7b8c92b |
|
BLAKE2b-256 | 4365e2552388ec95b518ee32f9e9637133847e825f5972c7cef2ff45cbb39a52 |