Skip to content

Instantly share code, notes, and snippets.

@GitMoIO
Created November 11, 2014 22:19
Show Gist options
  • Save GitMoIO/d606015a28ad52addd25 to your computer and use it in GitHub Desktop.
Save GitMoIO/d606015a28ad52addd25 to your computer and use it in GitHub Desktop.
create a pickle file for caching data in python From http://pynewb.tumblr.com/post/16870464613/python-pickle-file-caching
import os
import cPickle as pickle
def cached_data():
cache_path = "/tmp/cached-data.pickle"
if not os.path.exists(cache_path):
# The cache doesn't exist, create it and populate it
result = generate_data()
cache_file = open(cache_path,'wb')
# Write it to the result to the file as a pickled object
# Use the binary protocol for better performance
pickle.dump(result, cache_file, protocol=1)
cache_file.close()
return pickle.load(open(cache_path,'rb'))
def generate_data():
"""
This function actually generates the data when it isn't cached.
The data generated is often expensive to compute so caching helps with performance.
"""
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment