class PickleableObject(object):
"""Generic pickleable class
"""
def save(self,filename,clobber=False):
"""given an object, e.g. a taxon dictionary,
pickle it into the specified filename
Set clobber = True to allow overwriting of the
output file"""
import cPickle
f = safeOFW(filename,clobber=clobber,append=False)
cPickle.dump(self,f)
f.close()
@classmethod
def restore(cls,filename):
"""given a filename, return an object containing the
pickled data in that file"""
import cPickle
f = open(filename,'rb')
obj = cPickle.load(f)
return obj
I've been meaning to move this from a class that Peter wrote it in to originally at my request. It's quite nice to have it globally available.
Sweet. I need to start safeOFW'ing myself.
ReplyDeletetotally, and now that I put your save and restore methods in a separate class it is easy to add those to any class!
ReplyDelete