Skip to content

Instantly share code, notes, and snippets.

@Yangqing
Created May 13, 2013 22:48
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 Yangqing/5572180 to your computer and use it in GitHub Desktop.
Save Yangqing/5572180 to your computer and use it in GitHub Desktop.
ypickle: a simple script to achieve matlab type save and load, with the file compressed with zlib
"""ypickle.py implements an interface similar to pickle, but allows one to
perform a matlab-like store and load with zlib compression
"""
import cPickle as pickle
import zlib
class YPickle(object):
"""The abstract class that holds the data as attributes
"""
pass
def load(fid):
if type(fid) is str:
fid = open(fid, 'rb')
pickled = pickle.loads(zlib.decompress(fid.read()))
data = YPickle()
for key, val in pickled.iteritems():
setattr(data, key, val)
return data
def dump(fid, **kwargs):
if type(fid) is str:
fid = open(fid, 'wb')
pickled = pickle.dumps(kwargs)
fid.write(zlib.compress(pickled))
fid.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment