more efficient way to pickle a string more efficient way to pickle a string numpy numpy

more efficient way to pickle a string


Try using a later version of the pickle protocol with the protocol parameter to pickle.dumps(). The default is 0 and is an ASCII text format. Ones greater than 1 (I suggest you use pickle.HIGHEST_PROTOCOL). Protocol formats 1 and 2 (and 3 but that's for py3k) are binary and should be more space conservative.


Solution:

import zlib, cPickledef zdumps(obj):  return zlib.compress(cPickle.dumps(obj,cPickle.HIGHEST_PROTOCOL),9)def zloads(zstr):  return cPickle.loads(zlib.decompress(zstr))  >>> len(zdumps(z))128


z.dumps() is already pickled string i.e., it can be unpickled using pickle.loads():

>>> z = numpy.zeros(1000, numpy.uint8)>>> s = z.dumps()>>> a = pickle.loads(s)>>> all(a == z)True