-
Notifications
You must be signed in to change notification settings - Fork 0
/
joblib_utils.py
50 lines (37 loc) · 1.55 KB
/
joblib_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'''
This provides a little wrapper around joblib, giving a default cache directory
which is three levels up from where the module is defined with a directory joblib_cache.
It also adds two convenience functions `is_cacheable` and `is_cached` to check if a
function was wrapped with joblib, and to check if a particular set of keywords and
arguments are already cached.
'''
import joblib
import os, inspect
__all__ = ['cache_path', 'cache_mem', 'func_cache', 'is_cacheable', 'is_cached', 'cache_hash']
print joblib.__version__
basepath, _ = os.path.split(__file__)
print 'basepath = ' , basepath
#basepath = /chandelierhome/skadir/hybrid_analysis
# When called from a notebook the answer is blank
cache_path = os.path.normpath(os.path.join(basepath, 'joblib_cache'))
print 'cache_path = ', cache_path
#cache_path = /chandelierhome/skadir/hybrid_analysis/joblib_cache
# When called from a notebook the answer is blank followed by 'joblib_cache'
cache_mem = joblib.Memory(cachedir=cache_path, verbose=0)
func_cache = cache_mem.cache
def is_cacheable(func):
return hasattr(func, 'get_output_dir')
def is_cached(func, *args, **kwds):
s = func.get_output_dir(*args, **kwds)
return os.path.exists(func.get_output_dir(*args, **kwds)[0])
def cache_hash(func, *args, **kwds):
_, hash = func.get_output_dir(*args, **kwds)
return hash
if __name__=='__main__':
@func_cache
def f(x):
print 'I am taking the cube'
return x*x*x
print f(2)
print f(2)
print cache_hash(f, 2)