Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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 protobuf的HasField函数需要哪些参数?_Python_Protocol Buffers - Fatal编程技术网

Python protobuf的HasField函数需要哪些参数?

Python protobuf的HasField函数需要哪些参数?,python,protocol-buffers,Python,Protocol Buffers,考虑以下结构 message Fly { uint32 dtime = 1; } 但是HasField函数不起作用: >>> d.ListFields()[0][0].name 'dtime' >>> d.ListFields()[0][0].full_name 'Fly.dtime' >>> >>> d.HasField('dtime') Traceback (most recent call last): F

考虑以下结构

message Fly {
  uint32 dtime = 1;
}
但是
HasField
函数不起作用:

>>> d.ListFields()[0][0].name
'dtime'
>>> d.ListFields()[0][0].full_name
'Fly.dtime'
>>> 
>>> d.HasField('dtime')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 825, in HasField
    raise ValueError(error_msg % field_name)
ValueError: Protocol message has no non-repeated submessage field "dtime"
>>> d.HasField('Fly.dtime')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 825, in HasField
    raise ValueError(error_msg % field_name)
ValueError: Protocol message has no non-repeated submessage field "Fly.dtime"
>d.ListFields()[0][0]。名称
“dtime”
>>>d.ListFields()[0][0]。全名
“Fly.dtime”
>>> 
>>>d.HasField('dtime'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/…/lib/python2.7/site packages/google/protobuf/internal/python_message.py”,第825行,在HasField中
提升值错误(错误消息%field\u name)
ValueError:协议消息没有非重复子消息字段“dtime”
>>>d.HasField('Fly.dtime'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/…/lib/python2.7/site packages/google/protobuf/internal/python_message.py”,第825行,在HasField中
提升值错误(错误消息%field\u name)
ValueError:协议消息没有非重复子消息字段“Fly.dtime”

HasField
需要什么参数?

我想你误解了
HasField()
的作用。它不检查protobuf类型是否通过名称定义特定字段。它所做的是检查给定消息字段的名称后,该字段是否为当前实例设置


如前所述,在proto3中为非消息字段调用HasField将产生错误。

那么如果
dtime
是消息类型,它会工作吗?我正在尝试获取已设置的字段列表(因为protobuf为基元类型设置了默认值),我可以通过迭代ListFields来实现这一点,但我一直在寻找很好的解决方案。@rkuska:Proto3删除了基元字段的set和not set的概念。您的代码可以使用proto2,但不能使用proto3。您可能要做的是将字段与其默认值进行比较。@KentonVarda不同,因为有时值集可能与默认值相同,我正在寻找一种解决方案,以显示哪些可选参数未设置。即使是
ListFields
也无法工作,因为它会忽略那些设置了默认值的参数。@rkuska:对。在proto3下,整数没有set/notset这样的东西。只有默认值/没有默认值。如果你需要,你需要回到proto2。至少,这是我的理解。