forked from Kuhlman-Lab/ThermoMPNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.py
39 lines (35 loc) · 1.35 KB
/
cache.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
import os
import uuid
import pickle
import functools
import inspect
def stringify_cache_key(key):
return uuid.uuid3(uuid.NAMESPACE_DNS, str(key)).hex
def cache(cache_key, version=0.0, disable=False):
"""Cache the result of a function call on disk for speedup"""
def inner_cache(f):
f_sig = inspect.signature(f)
@functools.wraps(f)
def cached_f(cfg, *args, **kwargs):
# ensure that default args are properly passed to cache key
bound = f_sig.bind(cfg, *args, **kwargs)
bound.apply_defaults()
_, *args = bound.args
kwargs = bound.kwargs
key = stringify_cache_key(cache_key(cfg, *args, **kwargs))
cache_file = f"{cfg.platform.cache_dir}/functions/{f.__name__}/{version}/{key}.pkl"
if not disable:
try:
with open(cache_file, "rb") as fh:
ret = pickle.load(fh)
return ret
except (FileNotFoundError, EOFError):
pass
ret = f(cfg, *args, **kwargs)
cache_folder = "/".join(cache_file.split("/")[:-1])
os.makedirs(cache_folder, exist_ok=True)
with open(cache_file, "wb") as fh:
pickle.dump(ret, fh)
return ret
return cached_f
return inner_cache