Decorator to memorize function computations permanently to disk.
Project description
Funes
Funes lets you decorate functions in such a way that their computations are memorized permanently to disk.
It is named after the Borges short story Funes the Memories.
Background
This package allows the results of expensive functions be cached to disk, by decorating these functions with the @memorious
decorator. When that function is called again on an identical input, the cached result will be loaded from disk and returned.
This is achieved by pickling the result and input and writing it to disk, using a unique path that identifies the particular version of the function that is being cached (based on a hash that depends on its definition, thanks to the 'dill' package), as well as a key that depends on the hashed arguments of the function.
Memorious functions should not contain (possibly mutually) recursive definitions, or the stack will overflow. Their hash values WILL change if functions that depend on are changed, but these dependencies are only followed if they remain within the current module.
The pickled results will be cached under cache/funcname/hexhash/arg_hash.dill. These files can be deleted manually to clear the cache, or transported between computers.
Each pickled computation contains a dictionary with the following keys:
input
: ordered dict of function arguments + their provided (or defaulting) values
output
: whatever the function produced
time
: time in seconds the function took to run
A special global_seed
keyword argument can be provided to any memorious function to set the global random seed prior to running the function. This allows controlled stochasticity to be used, while maintaining the benefits of caching.
Examples
The following script demonstrates the basic idea behind funes:
from funes import memorious, load_cached_results
from time import sleep
@memorious
def double(x):
print('doubling', x)
sleep(0.5)
return x * 2
print("uncached (will be slow)")
for i in range(5):
double(i)
print("cached (will be fast)")
for i in range(5):
double(i)
print("uncached (will be slow, unique global seed)")
for seed in range(5):
double(0, global_seed=seed)
print("cached (will be fast, reuse global seed)")
for seed in range(5):
double(0, global_seed=seed)
print("all cached values")
print(list(load_cached_results(double)))
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 funes-0.0.1.tar.gz
.
File metadata
- Download URL: funes-0.0.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91d5e4900dfdea2d3f99ab1dd3f15006afbc21b4e831ff973f2ca431c33ccfa4 |
|
MD5 | 957077b5d2e6bfe2e18ffbd20dceee2b |
|
BLAKE2b-256 | 697da45aaf3483d9ba38475c9163d0ff27183c464cb1cb2dc956def57d6eef5e |