cache the results of slow-running functions to disk, with multi-process safety
Project description
pyfilecache
cache the results of slow-running functions to disk, with multi-process safety
Install
pip install pyfilecache
Examples
Basic Usage
# examples/add.py
from pyfilecache import file_cache
@file_cache
def add(a, b):
print(f'adding {repr(a)} and {repr(b)}')
return a + b
print(repr(add(1, 2)))
print(repr(add(1, 2)))
print(repr(add('1', '2')))
print(repr(add('1', '2')))
$ python3 examples/add.py
adding 1 and 2
3
3
adding '1' and '2'
'12'
'12'
$ python3 examples/add.py
3
3
'12'
'12'
Customized Reader and Writer
# examples/df.py
import pandas as pd
from functools import partial
from pyfilecache import file_cache
@file_cache(
reader=partial(pd.read_csv, index_col=0),
writer=pd.DataFrame.to_csv
)
def func(a, b):
print("func called with args", a, b)
return pd.DataFrame(
data = {
'col1': [1, 2],
'col2': [a, b]
}
)
print(func(3, 4))
print(func(3, 4))
$ python3 examples/df.py
func called with args 3 4
col1 col2
0 1 3
1 2 4
col1 col2
0 1 3
1 2 4
$ python3 examples/df.py
col1 col2
0 1 3
1 2 4
col1 col2
0 1 3
1 2 4
Multi-Process Safety
# examples/process.py
from pyfilecache import file_cache
import multiprocessing
@file_cache
def func(a, b):
print(a, b)
return a + b
def loop():
while True:
print(func(1, 2))
print(func('1', '2'))
print(func.fp(1, 2))
func.clear()
print(func(1, 2))
print(func('1', '2'))
func.clear()
print(func(1, 2))
print(func(3, 4))
print(func(5, 6))
if __name__ == '__main__':
ps = [multiprocessing.Process(target=loop) for _ in range(10)]
list(map(multiprocessing.Process.start, ps))
list(map(multiprocessing.Process.join, ps))
Cache Non-Pure Function with Different Versions
[!NOTE] nonlocal variables used in function (e.g.
outer) and version string (e.g."v0","v1") must correspond
# examples/versions.py
from pyfilecache import file_cache
@file_cache
def func(x: int):
print(f'multiply {outer} and {x}')
return outer * x
outer = 100
print(func(2))
print(func(10))
outer = 5
print(func['v0'](2))
print(func['v0'](10))
outer = 1
print(func['v1'](2))
print(func['v1'](10))
$ python3 examples/versions.py
multiply 100 and 2
200
multiply 100 and 10
1000
multiply 5 and 2
10
multiply 5 and 10
50
multiply 1 and 2
2
multiply 1 and 10
10
$ python3 examples/versions.py
200
1000
10
50
2
10
Functions with Different Signatures
# examples/signatures.py
from pyfilecache import file_cache
@file_cache
def func(x = 1000):
print('OK')
return x
print(func())
@file_cache
def func(x = 100):
print('OK')
return x
print(func())
@file_cache
def func(x: int = 100) -> int:
print('OK')
return x
print(func())
$ python3 examples/signatures.py
OK
1000
OK
100
OK
100
$ python3 examples/signatures.py
1000
100
100
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyfilecache-0.1.5.tar.gz
(5.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyfilecache-0.1.5.tar.gz.
File metadata
- Download URL: pyfilecache-0.1.5.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ead3d30f302378834c18007af4b6bb401c0a5eae1b7b258c711ef33ffb4f332b
|
|
| MD5 |
ba2b625057469defacda6f2b2f26cc21
|
|
| BLAKE2b-256 |
de8fe7f11de9ef971c2bb57c5f6a36adeaa903b8c5ab2ee13e13a61d89e8b619
|
File details
Details for the file pyfilecache-0.1.5-py3-none-any.whl.
File metadata
- Download URL: pyfilecache-0.1.5-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abe4f472ce3ce41f2b02d731403e1d95bf1486fcfcfe93b7f3ad06e13ef61370
|
|
| MD5 |
5550fac6192fa6c50faa18c95a7c4129
|
|
| BLAKE2b-256 |
6c268e8dbe1fd0d8b206dbc4c032a714941bb26529161550c129996bd8a4f91e
|