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. Note that all function arguments must be hashable
for it to be cached else a TypeError will be thrown.
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
The memoizer will automatically ignore a function's first parameter if named self, so that instance methods can be cached. It is the caller's responsibility to ensure the result of the method does not depend on the state of the object's internals. This is most useful when the object is being used to call an external service.
import requests
class ExampleAPIClient():
@file_memoizer.memoize()
def get(self, url):
return self.session.get(url)
def __init__(self):
self.session = requests.Session()
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.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 660866953d7fe6e04d6c895d4a7192fa2d916cec833cbdbca823c5027516c868 |
|
MD5 | 29f7f14f0ae433a5888416d83334b8c3 |
|
BLAKE2b-256 | 85d76773c9acd423fb3b6392191764190c248287da47765e306df67bbcd17694 |