Store function results across executions using cache files
Project description
File Memoizer
NOTICE: This package is deprecated and will no longer be updated, as the functionality it provides has now been inplemented directly in cachier. Please use that package directly.
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
File details
Details for the file file_memoizer-0.4.0.tar.gz
.
File metadata
- Download URL: file_memoizer-0.4.0.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-1035-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b550a26759f16003d0713164cf5733cecc662fd0e03ab3bc49d617ce6f6a48d6 |
|
MD5 | 23c1ea7ae853ff1db0f9379d35a52789 |
|
BLAKE2b-256 | 30ef92a4d50e6c7eb78171fba75c524c2b079406925a8966d8dfbfa1774e6a25 |
File details
Details for the file file_memoizer-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: file_memoizer-0.4.0-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-1035-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe1f6a7c59c936997ea238dc02638d5bf1c13a7756cf99b21ba70386be1a52da |
|
MD5 | ab97d1939abd3927eafc6dc81fe6a11b |
|
BLAKE2b-256 | cf399536583b929fcdf6db43fd30b53f7aec601420ac963b3297577c43aafbd7 |