Skip to content

Instantly share code, notes, and snippets.

@z-------------
Created March 24, 2020 13:20
Show Gist options
  • Save z-------------/a966a7480976be4a8c23edebbd97d50f to your computer and use it in GitHub Desktop.
Save z-------------/a966a7480976be4a8c23edebbd97d50f to your computer and use it in GitHub Desktop.
def pickle_load(filename):
return pickle.load(open(filename, "rb"))
def pickle_dump(data, filename):
return pickle.dump(data, open(filename, "wb"))
def pickle_get(filename, fn, *args, fresh=False, verbose=False):
"""Try to load data from a pickle file. Compute it with `fn` and save it if it doesn't exist."""
if not fresh and file_exists(filename):
if verbose:
print("using pickled save from %s." % filename)
return pickle_load(filename)
else:
if verbose:
print("computing data...")
data = fn(*args)
pickle_dump(data, filename)
if verbose:
print("wrote to %s." % filename)
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment