Here is some problem with pack/unpack tuples. As I know msgpack not distinguish between list and tuple and there is not hook to force list or tuple be ExtType. It generates frustrating problems.
See simple example class with hash - nothing special:
import msgpack
class Period(object):
def __init__(self, key):
self.key = key
def __hash__(self):
return hash(self.key)
def __eq__(self, other):
self.key == self.key
def encode(o):
if type(o) is Period:
return msgpack.ExtType(0, msgpack.dumps(o.__dict__))
def decode_ext(code, data):
if code == 0:
o = Period.__new__(Period)
o.__dict__ = msgpack.loads(data)
return o
o = {Period((2016, 7)): 112, Period((2016, 8)): 231}
print o
s = msgpack.dumps(o, default=encode)
print s
o2 = msgpack.loads(s, ext_hook=decode_ext)
print o2
It generates problem during unpacking which cannot be solved easily I think:
C:\root\Python27-64\python.exe "C:/Users/Cezary Wagner/PycharmProjects/msgpack_learn/src/02_tuple_wrong_pack.py"
Traceback (most recent call last):
{<__main__.Period object at 0x0000000002941668>: 231, <__main__.Period object at 0x0000000002941AC8>: 112}
File "C:/Users/Cezary Wagner/PycharmProjects/msgpack_learn/src/02_tuple_wrong_pack.py", line 28, in <module>
��
o2 = msgpack.loads(s, ext_hook=decode_ext)
��key��������
File "msgpack/_unpacker.pyx", line 139, in msgpack._unpacker.unpackb (msgpack/_unpacker.cpp:139)
��key�����p
File "C:/Users/Cezary Wagner/PycharmProjects/msgpack_learn/src/02_tuple_wrong_pack.py", line 8, in __hash__
return hash(self.key)
TypeError: unhashable type: 'list'
Process finished with exit code 1
Do you have any idea how to reconstruct tuple to tuples and list to lists using msgpack if it possible at all?
Here is some problem with pack/unpack tuples. As I know msgpack not distinguish between list and tuple and there is not hook to force list or tuple be ExtType. It generates frustrating problems.
See simple example class with hash - nothing special:
It generates problem during unpacking which cannot be solved easily I think:
Do you have any idea how to reconstruct tuple to tuples and list to lists using msgpack if it possible at all?