Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.