Skip to content

Instantly share code, notes, and snippets.

@roskakori
Created August 2, 2012 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roskakori/3241965 to your computer and use it in GitHub Desktop.
Save roskakori/3241965 to your computer and use it in GitHub Desktop.
Caching a list of data using pickle
'''
Example for caching a list of data using pickle.
'''
import cPickle as pickle
_CachePath = '/tmp/cache.pkl'
def expensiveToComputeData():
# These would be date retrieved from an external file involving lots fo
# computations, validation and so on. This particular example data are
# obviously easy to "compute".
return [
[123, 'hugo', '1965-03-27'],
[234, 'resi', '1983-09-02'],
[987, 'sepp', '2004-02-28'],
]
def writeCachedData():
with open(_CachePath, 'wb') as cacheFile:
pickler = pickle.Pickler(cacheFile, pickle.HIGHEST_PROTOCOL)
for row in expensiveToComputeData():
pickler.dump(row)
pickler.dump(None)
def cachedData():
with open(_CachePath, 'rb') as cacheFile:
unpickler = pickle.Unpickler(cacheFile)
row = unpickler.load()
while row is not None:
yield row
row = unpickler.load()
if __name__ == '__main__':
writeCachedData()
for row in cachedData():
print row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment