Created
May 13, 2013 22:48
-
-
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
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
"""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