Hi, I found a way to achieve this.
It can be made by implementing a sort of interface (abstract class)
that defines a getTuple and setTuple methods.
class iSerializable(object):
"""Serializable Object with a Tuple representation of data"""
def getTuple(self):
"""Return a Tuple that represents the data of the object"""
raise NotImplementedError( "Should have implemented
iSerializable.getTuple()" )
def setTuple(self, aDataTuple):
"""Set all data values of the object from a Tuple
representation"""
raise NotImplementedError( "Should have implemented
iSerializable.setTuple(aDataTuple)" )
Then, the above example will be:
class MyObj(iSerializable):
def __init__(self):
self.value1 = 0
self.value2 = 0
def getTuple(self):
return (self.value1, self.value2)
def setTuple(self, aDataTuple):
self.value1 = aDataTuple[0]
self.value2 = aDataTuple[1]
class MyObjList(iSerializable):
def __init__(self):
self.lst = []
def append(self, aMyObj):
self.lst.append(aMyObj)
def getTuple(self):
res = ()
res = tuple([o.getTuple() for o in self.lst])
return res
def setTuple(self, aDataTuple):
self.lst = []
for t in aDataTuple:
a = MyObj()
a.setTuple(t)
self.lst.append(a)
MyList = MyObjList()
a = MyObj()
a.value1 = 11
a.value2 = 12
MyList.append(a)
a = MyObj()
a.value1 = 21
a.value2 = 22
MyList.append(a)
buffer = msgpack.packb(MyList.getTuple())
print buffer
MyOtherList = MyObjList()
MyOtherList.setTuple(msgpack.unpackb(buffer))
Enjoy.
al