gpt4 book ai didi

python - 无法访问 python 3 中的(嵌套)枚举类型(proto3)

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:49 25 4
gpt4 key购买 nike

我无法访问普通 Protocol Buffer 消息中的(嵌套)枚举。我尝试了任何一种方式,嵌套或与 DataNodeManagement 分开!:

syntax = "proto3";

message DataNodeManagement {
string name = 1;
string id = 2;
string origin = 3;
ConnectionType con_type = 4;
enum ConnectionType {
UNKNOWN = 0;
MQTT = 1;
}
}

我正在使用此代码在我的消息中填充数据:

config = data_node_pb2.DataNodeManagement()
config.name = "Scanner1"
config.id = key
config.origin = "PC1"
config.con_type = data_node_pb2.ConnectionType.MQTT
# or
# config.con_type = data_node_pb2.DataNodeManagement.ConnectionType.MQTT

datasource.advertise_data_node(config.SerializeToString())

它提示:

Traceback (most recent call last):
File "scanner-connector.py", line 144, in <module>
config.con_type = data_node_pb2.ConnectionType.MQTT
AttributeError: 'EnumTypeWrapper' object has no attribute 'MQTT'

各自:

Traceback (most recent call last):
File "scanner-connector.py", line 144, in <module>
config.con_type = data_node_pb2.DataNodeManagement.ConnectionType.MQTT
AttributeError: type object 'DataNodeManagement' has no attribute 'ConnectionType'

我正在使用这些版本:

python --version
Python 3.6.6 :: Anaconda custom (64-bit)

protoc --version
libprotoc 3.6.1

作为初学者,我是否忽略了任何特别的事情?

最佳答案

您必须跳过枚举名称才能访问枚举中的值。在protocol-buffers python-tutorial中可以看出枚举在消息中定义

message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;

enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}

message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}

repeated PhoneNumber phones = 4;
}

Reading a Message 部分枚举被访问

import addressbook_pb2
addressbook_pb2.Person.MOBILE

因此在您的示例中它应该是 data_node_pb2.DataNodeManagement.MQTT

关于python - 无法访问 python 3 中的(嵌套)枚举类型(proto3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52533044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com