当Python Thrift解码由结构索引的映射时,如何避免出现“ TypeError:unhashable type”

时间:2018-09-19 21:48:14

标签: python dictionary thrift

文件model.thrift包含以下Thrift模型:

struct Coordinate {
    1: required i32 x;
    2: required i32 y;
}

struct Terrain {
    1: required map<Coordinate, i32> altitude_samples;
}

请注意,我们有一个由结构(坐标)索引的地图(altitude_samples)。

我使用Thrift编译器生成Python编码和解码类:

thrift -gen py model.thrift

我使用以下Python代码从文件中解码Terrain对象:

#!/usr/bin/env python

import sys
sys.path.append('gen-py')

import thrift.protocol.TBinaryProtocol
import thrift.transport.TTransport
import model.ttypes

def decode_terrain_from_file():
    file = open("terrain.dat", "rb")
    transport = thrift.transport.TTransport.TFileObjectTransport(file)
    protocol = thrift.protocol.TBinaryProtocol.TBinaryProtocol(transport)
    terrain = model.ttypes.Terrain()
    terrain.read(protocol)
    print(terrain)

if __name__ == "__main__":
    decode_terrain_from_file()

运行该程序时,出现以下错误:

(env) $ python py_decode.py 
Traceback (most recent call last):
  File "py_decode.py", line 19, in <module>
    decode_terrain_from_file()
  File "py_decode.py", line 15, in decode_terrain_from_file
    terrain.read(protocol)
  File "gen-py/model/ttypes.py", line 119, in read
    self.altitude_samples[_key5] = _val6
TypeError: unhashable type: 'Coordinate

1 个答案:

答案 0 :(得分:0)

问题是Thrift编译器无法自动为Coordinate类生成哈希函数。

您必须手动添加哈希函数,如下所示:

#!/usr/bin/env python

import sys
sys.path.append('gen-py')

import thrift.protocol.TBinaryProtocol
import thrift.transport.TTransport
import model.ttypes

model.ttypes.Coordinate.__hash__ = lambda self: hash((self.x, self.y))

def decode_terrain_from_file():
    file = open("terrain.dat", "rb")
    transport = thrift.transport.TTransport.TFileObjectTransport(file)
    protocol = thrift.protocol.TBinaryProtocol.TBinaryProtocol(transport)
    terrain = model.ttypes.Terrain()
    terrain.read(protocol)
    print(terrain)

if __name__ == "__main__":
    decode_terrain_from_file()

请注意,C ++生成的代码也会发生类似的问题。在C ++情况下,您需要手动将Coordinate :: operator <添加到程序中。 Thrift会为Coordinate :: operator <生成声明,但不会生成Coordinate :: operator <的实现。同样,其原因是Thrift无法理解结构的语义,因此无法猜测比较运算符的正确实现。

bool Coordinate::operator<(const Coordinate& other) const
{
    if (x < other.x) {
        return true;
    } else if (x > other.x) {
        return false;
    } else if (y < other.y) {
        return true;
    } else {
        return false;
    }
}