00001 import pickle 00002 00003 class SimpleFormat(object): 00004 00005 def __init__(self, handle): 00006 super(SimpleFormat, self).__init__() 00007 self._handle = handle 00008 00009 def serialize_data(self, data): 00010 pickle.dump(data, self._handle) 00011 00012 def serialize_instance(self, instance): 00013 # store instance type 00014 name = instance.__class__.__module__ 00015 self.serialize_data(name) 00016 # store state of instance 00017 instance.serialize(self) 00018 00019 def deserialize_data(self): 00020 data = pickle.load(self._handle) 00021 return data 00022 00023 def deserialize_instance(self): 00024 # recreate instance 00025 name = self.deserialize_data() 00026 classname = name.split('.')[-1] 00027 mod = __import__(name, fromlist=[classname]) 00028 instance = getattr(mod, classname)() 00029 # restore state of instance 00030 instance.deserialize(self) 00031 return instance