Skip to content

Instantly share code, notes, and snippets.

@rotaliator
Created January 31, 2022 20:54
Show Gist options
  • Save rotaliator/89df5f43d6c1cc31655f8d7f1496c95c to your computer and use it in GitHub Desktop.
Save rotaliator/89df5f43d6c1cc31655f8d7f1496c95c to your computer and use it in GitHub Desktop.
python file cache decorators
import os
import pickle
import hashlib
from functools import wraps, reduce
def default_filename_fn(args, kwargs):
base = reduce(lambda acc,x: acc + str(x), args, "")
return hashlib.sha1(base.encode()).hexdigest()
def dir_cache(directory, filename_fn=default_filename_fn):
def decorator(function):
@wraps(function)
def wrapper(*args, **kwargs):
if not os.path.exists(directory):
os.makedirs(directory)
filename = filename_fn(args, kwargs)
path = os.path.join(directory, filename)
if os.path.isfile(path):
with open(path, "rb") as f:
result = pickle.load(f)
else:
result = function(*args, **kwargs)
with open(path, "wb") as f:
pickle.dump(result, f)
return result
return wrapper
return decorator
def file_cache(filename):
def decorator(function):
@wraps(function)
def wrapper(*args, **kwargs):
if os.path.isfile(filename):
with open(filename, "rb") as f:
result = pickle.load(f)
else:
result = function(*args, **kwargs)
with open(filename, "wb") as f:
pickle.dump(result, f)
return result
return wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment