Afo*_*box 4 python protocol-buffers python-3.x
我无法访问普通协议缓冲区消息中的(嵌套)枚举。我尝试了任何一种方式,嵌套或与DataNodeManagement!分开:
syntax = "proto3";
message DataNodeManagement {
string name = 1;
string id = 2;
string origin = 3;
ConnectionType con_type = 4;
enum ConnectionType {
UNKNOWN = 0;
MQTT = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用此代码在我的消息中填充数据:
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())
Run Code Online (Sandbox Code Playgroud)
它抱怨:
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'
Run Code Online (Sandbox Code Playgroud)
各自:
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'
Run Code Online (Sandbox Code Playgroud)
我正在使用这些版本:
python --version
Python 3.6.6 :: Anaconda custom (64-bit)
protoc --version
libprotoc 3.6.1
Run Code Online (Sandbox Code Playgroud)
作为初学者,有没有什么特别的地方被我忽略了?
小智 6
您必须跳过 enum-name 才能访问 enum 中的值。正如在协议缓冲区 python 教程中可以看到的,枚举是在消息中定义的
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;
}
Run Code Online (Sandbox Code Playgroud)
在阅读消息部分中,枚举被访问
import addressbook_pb2
addressbook_pb2.Person.MOBILE
Run Code Online (Sandbox Code Playgroud)
所以在你的例子中应该是 data_node_pb2.DataNodeManagement.MQTT
| 归档时间: |
|
| 查看次数: |
2638 次 |
| 最近记录: |