如何在Python中使用prototbuf映射?

2024-06-28 10:25:47 发布

您现在位置:Python中文网/ 问答频道 /正文

给出一个原型定义

message EndpointResult {
    int32 endpoint_id = 1;
    // property id as key
    map<int32, TimeSeries> properties = 2;
}

message TimeSeries {
    repeated TimeEntry value = 2;
}

message TimeEntry {
    int32 time_unit = 1;
    float value = 2;
}

我希望在EndpointResult类中填充映射。我尝试过docs中建议的不同方法,但都给我带来了一个错误。在

设置测试类

^{pr2}$

然后尝试不同的方法:

end_point_rslt.properties[0] = ts

ValueError: Direct assignment of submessage not allowed

end_point_rslt.properties[0].submessage_field = ts

AttributeError: Assignment not allowed (no field "submessage_field" in protocol message object).

end_point_rslt.properties.get_or_create(0)
end_point_rslt.properties[0] = ts

ValueError: Direct assignment of submessage not allowed

end_point_rslt.properties.get_or_create(0)
end_point_rslt.properties[0].submessage_field = ts

AttributeError: Assignment not allowed (no field "submessage_field" in protocol message object).

end_point_rslt.properties = {0 : ts}

AttributeError: Assignment not allowed to repeated field "properties" in protocol message object.

end_point_rslt.properties.get_or_create(0)
end_point_rslt.properties = {0 : ts}

TypeError: Can't set composite field

任何在python中使用协议缓冲区映射的例子都将不胜感激!


Tags: infieldmessagenotpropertiesprotocolendpoint
1条回答
网友
1楼 · 发布于 2024-06-28 10:25:47

在看了这些文档之后,我意识到问题是我给字典分配了一个类。在

正确的语法是

end_point_rslt = nom.EndpointResult()
end_point_rslt.endpoint_id=0
te = end_point_rslt.properties[0].value.add()
te.time_unit = 0
te.value = 5.

相关问题 更多 >