Created
August 2, 2012 23:31
-
-
Save roskakori/3241965 to your computer and use it in GitHub Desktop.
Caching a list of data using pickle
This file contains 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
''' | |
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