Skip to content

Instantly share code, notes, and snippets.

@Pangoraw
Created August 29, 2022 13:37
Show Gist options
  • Save Pangoraw/050dd947822306ba8a243832c5c1818e to your computer and use it in GitHub Desktop.
Save Pangoraw/050dd947822306ba8a243832c5c1818e to your computer and use it in GitHub Desktop.
from pathlib import Path
import pickle
import functools
import hashlib
def hash_args(args, kwargs):
assert all(isinstance(s, str) for s in kwargs.values())
assert all(isinstance(s, str) for s in args)
m = hashlib.md5()
for s in args:
m.update(s.encode())
for k, v in kwargs:
m.update(k.encode())
m.update(v.encode())
return m.hexdigest()
def file_cache(func):
"""
Caches the result of this function call on the filesystem by hashing the arguments.
"""
path = Path("./.file_cache/")
path.mkdir(exist_ok=True)
@functools.wraps(func)
def wrapper(*args, **kwargs):
file_path = path / (func.__name__ + "_" + str(hash_args(args, kwargs)))
file_path = file_path.with_suffix(".pckl")
if file_path.exists():
with open(file_path, "rb") as f:
output = pickle.load(f)
return output
output = func(*args, **kwargs)
with open(file_path, "wb") as f:
pickle.dump(output, f)
return output
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment