Skip to content

Instantly share code, notes, and snippets.

@def
Created February 23, 2012 11:31
Show Gist options
  • Save def/1892477 to your computer and use it in GitHub Desktop.
Save def/1892477 to your computer and use it in GitHub Desktop.
serialize
def load_obj(data):
try:
return cPickle.loads(zlib.decompress(data))
except zlib.error:
return cPickle.loads(data)
def dump_obj(obj):
# In [24]: %timeit zlib.compress(cPickle.dumps(a, protocol=cPickle.HIGHEST_PROTOCOL), 1)
# 1000 loops, best of 3: 1.74 ms per loop
# In [25]: %timeit zlib.compress(cPickle.dumps(a))
# 10 loops, best of 3: 34.9 ms per loop
pickled_data = cPickle.dumps(obj, protocol=cPickle.HIGHEST_PROTOCOL)
zipped_data = zlib.compress(pickled_data, 1)
return zipped_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment