Store function results across executions using cache files
Project description
File Memoizer
This Python package makes it easy to store function results across executions using cache files. Underlying functionality is provided by cachier, but this package adds a few conveniences, such as being able to ignore parameters that won't serialize.
Installation
Installation is via pip
:
pip install file-memoizer
Usage
To cache a function's value, annotate it by calling the memoize
function as follows:
import file_memoizer
@file_memoizer.memoize()
def double(n):
return 2 * n
By default the cached values remain valid for a day. This can be changed
with the cache_ttl
parameter:
import datetime
import file_memoizer
seven_days = cache_ttl=datetime.timedelta(days=7)
@file_memoizer.memoize(cache_ttl=seven_days)
def triple(n):
return 3 * n
Cache files are stored in $HOME/.file-memoizer
, with one file per
combination of input parameters. An alternate location can be specified
with the cache_directory
parameter:
import datetime
import file_memoizer
custom_path = '/path/to/store/files'
@file_memoizer.memoize(cache_directory=custom_path)
def quadruple(n):
return 4 * n
Normally all function arguments must be hashable for it to be safely cached. However,
there are situations where it's okay to ignore them, such as an object method whose
return value doesn't depend on the object's internal state. In these cases, set
unhashable_args='ignore'
as shown below:
class Arithmetic():
@staticmethod
@file_memoizer.memoize()
def quintuple(n):
return 5 * n
@file_memoizer.memoize(unhashable_args='ignore')
def multiply(self, x, y):
return x * y
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
Built Distribution
Hashes for file_memoizer-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de142cfe1da024ebd05d6854c2efde8c2812ed4ebb265c59ff32efe6841e0f01 |
|
MD5 | 23069f82ef3e0e92819221cf73b6a1f8 |
|
BLAKE2b-256 | 4fd5ef8c0de48467b362d67c0b4444284162381a9a4fcd7bb9dae8ea0a24c9d5 |