Skip to content

Instantly share code, notes, and snippets.

@perryism
Created February 15, 2024 19:20
Show Gist options
  • Save perryism/9984a5f905f2c5149949a8ec1e039e22 to your computer and use it in GitHub Desktop.
Save perryism/9984a5f905f2c5149949a8ec1e039e22 to your computer and use it in GitHub Desktop.
"""
I found myself waiting for LLM model to give me result. My result is always the same since I am working on the lower level library.
Example
@memoize("test.pkl")
def sum(x, y):
return x + y
"""
import os, pickle, logging
def memoize(file_gen):
def wrap(func):
cached_file = file_gen(func)
def wrapper(*args, **kwargs):
if os.path.exists(cached_file):
logging.info("Using cached file")
with open(cached_file, "rb") as f:
return pickle.load(f)
else:
to_be_cache = func(*args, **kwargs)
with open(cached_file, "wb") as f:
pickle.dump(to_be_cache, f)
return to_be_cache
return wrapper
return wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment