Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法访问python 3中的(嵌套)枚举类型(proto3)_Python_Python 3.x_Protocol Buffers - Fatal编程技术网

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

无法访问python 3中的(嵌套)枚举类型(proto3),python,python-3.x,protocol-buffers,Python,Python 3.x,Protocol Buffers,我无法访问普通协议缓冲区消息中的(嵌套)枚举。我尝试了两种方法,嵌套或与DataNodeManagement分离: syntax = "proto3"; message DataNodeManagement { string name = 1; string id = 2; string origin = 3; ConnectionType con_type = 4; enum ConnectionType { UNKNOWN = 0; MQTT = 1;

我无法访问普通协议缓冲区消息中的(嵌套)枚举。我尝试了两种方法,嵌套或与
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'

作为一个初学者,我有什么特别忽略的吗?

您必须跳过枚举名称才能访问枚举中的值。 如图所示,枚举是在消息中定义的

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;
}
在该部分中,可通过访问枚举

import addressbook_pb2
addressbook_pb2.Person.MOBILE

因此,在您的示例中,它应该是
data\u node\u pb2.DataNodeManagement.MQTT

,讽刺的是,您现在链接到的位置在示例代码中使用了语法
addressbook\u pb2.Person.PhoneType.MOBILE
。但是,它的语法
addressbook\u pb2.Person.HOME
更高。此外,只有后者对我有效,关于这一点,我可能会提出一个单独的问题。后续,两者都应该有效,请参阅讨论。
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;
}
import addressbook_pb2
addressbook_pb2.Person.MOBILE