Created
December 4, 2014 16:29
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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