Skip to content

Instantly share code, notes, and snippets.

@GMadorell
Created December 4, 2014 16:29
Show Gist options
  • Save GMadorell/7e9b0b24fe82db85bf0c to your computer and use it in GitHub Desktop.
Save GMadorell/7e9b0b24fe82db85bf0c to your computer and use it in GitHub Desktop.
Cache the result of a function as a pickle dump so we don't have to load it again the next time we want to use it.
def cache_in_file(filepath, object_creation_function):
if os.path.exists(filepath):
with open(filepath, "r") as dump_file:
obj = pickle.load(dump_file)
else:
obj = object_creation_function()
with open(filepath, "w") as dump_file:
pickle.dump(obj, dump_file)
return obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment