Introduction
touch_cache is a smart memoization library that tracks the files opened during the process.
The cache is automatically invalidated when one of the used file is modified (more recent) than the cache.
touch_cache also watch any change in the code of the function itself, or the values of the global variables used by them.
It is particularly suitable for a dev environements in data science, for speeding dev iterations / debug of a pipeline having some heavy steps, while making sure the output is always up to date with input data and parameters.
Installation
pip install touch_cache
Usage
Basic function
This library provides a single decorator touch_cache.
The files used in the process are tracked via the usage of the standard function open()
from touch_cache import touch_cache
from pathlib import Path
INPUT_FILE = "data/input.dat"
@touch_cache
def long_running_function(param1:int):
print("Running ...")
with open(INPUT_FILE) as f :
res = some_long_process(f)
return res
# First call
res = long_running_function(1)
# will print "Running ..."
# Second call
res = long_running_function(1)
# will us the cached value and NOT print 'Running ...'
# Touch INPUT file
# This may be any external update of it
Path(INPUT_FILE).touch()
# Thirds call
res = long_running_function(1)
# Input file modified ==> will invalidate the cache and run again
# "Running ..."
Configure cache folder
By default, cache data will be pickled on dick using cloudpickle in the folder .cache.
You can choose another folder with set_cache_dir().
from touch_cache import set_cache_dir
set_cache_dir("/some/folder")
Clean cache
Manually track files
touch_cache hooks to the standard function open() to track the files used. This works in most cases (including Pandas).
some external libraries might use low level C code that doesn't directly calls open().
In such case, you need to manually tell touch_cache that a file has been used, with the function using_file().
from touch_cache import using_file, touch_cache
from third_parthy_library import load_some_file
@touch_cache
def long_running_function(param1:int):
# Manually register this input file
using_file(INPUT_FILE)
intermediate = load_some_file(INPUT_FILE)
res = some_long_process(intermediate)
return res
License
Author
Metadata
Release files for touch-cache 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| touch_cache-0.1.0.tar.gz | 6.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| touch_cache-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.7 kB
Release files / touch_cache-0.1.0.tar.gz
| Download URL | touch_cache-0.1.0.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b68304b679e37f63e4142c22d2339a0908d629cb7ed0e849cd379d223d778ce2
|
|
BLAKE2b-256 checksum How to use checksums |
a002820ef608502256a36a15ea26d48f7d1de2a58df04a0ac88d3230113be2b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.12
|
Release files / touch_cache-0.1.0-py3-none-any.whl
| Download URL | touch_cache-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
23374f1af94beeb369624109768fbaa90d9f749eb23c862cfcc551a1c163b779
|
|
BLAKE2b-256 checksum How to use checksums |
171b4dfb229b5fa2339474ce7c6699bd0aa7f190d7f9ec0bfa6168446a59f8a6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.12
|