-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmemoize.py
More file actions
27 lines (26 loc) · 869 Bytes
/
memoize.py
File metadata and controls
27 lines (26 loc) · 869 Bytes
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
import hashlib
import pickle
import inspect
import os
def memoize(func):
is_method = 'self' in inspect.getfullargspec(func).args
def wrapper(*args, **kwargs):
fn = os.path.join("cache", func.__name__)
args_to_hash = ""
if len(args) > 0:
args_to_hash += str(args[1:] if is_method else args)
if len(kwargs) > 0:
args_to_hash += str(kwargs)
if len(args_to_hash) > 0:
fn += "_" + hashlib.md5(str(args_to_hash).encode()).hexdigest()
fn += ".pkl"
try:
os.makedirs('cache', exist_ok=True)
with open(fn, 'rb') as f:
return pickle.load(f)
except FileNotFoundError:
result = func(*args, **kwargs)
with open(fn, 'wb') as f:
pickle.dump(result, f)
return result
return wrapper