本文整理汇总了Python中google.protobuf.message.Clear方法的典型用法代码示例。如果您正苦于以下问题:Python message.Clear方法的具体用法?Python message.Clear怎么用?Python message.Clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.protobuf.message
的用法示例。
在下文中一共展示了message.Clear方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testSortingRepeatedCompositeFieldsStable
# 需要导入模块: from google.protobuf import message [as 别名]
# 或者: from google.protobuf.message import Clear [as 别名]
def testSortingRepeatedCompositeFieldsStable(self, message_module):
"""Check passing a custom comparator to sort a repeated composite field."""
message = message_module.TestAllTypes()
message.repeated_nested_message.add().bb = 21
message.repeated_nested_message.add().bb = 20
message.repeated_nested_message.add().bb = 13
message.repeated_nested_message.add().bb = 33
message.repeated_nested_message.add().bb = 11
message.repeated_nested_message.add().bb = 24
message.repeated_nested_message.add().bb = 10
message.repeated_nested_message.sort(key=lambda z: z.bb // 10)
self.assertEqual(
[13, 11, 10, 21, 20, 24, 33],
[n.bb for n in message.repeated_nested_message])
# Make sure that for the C++ implementation, the underlying fields
# are actually reordered.
pb = message.SerializeToString()
message.Clear()
message.MergeFromString(pb)
self.assertEqual(
[13, 11, 10, 21, 20, 24, 33],
[n.bb for n in message.repeated_nested_message])
示例2: testOneofClear
# 需要导入模块: from google.protobuf import message [as 别名]
# 或者: from google.protobuf.message import Clear [as 别名]
def testOneofClear(self, message_module):
m = message_module.TestAllTypes()
m.oneof_uint32 = 11
m.Clear()
self.assertIsNone(m.WhichOneof('oneof_field'))
m.oneof_bytes = b'bb'
self.assertEqual('oneof_bytes', m.WhichOneof('oneof_field'))
示例3: testAssignInvalidEnum
# 需要导入模块: from google.protobuf import message [as 别名]
# 或者: from google.protobuf.message import Clear [as 别名]
def testAssignInvalidEnum(self):
"""Assigning an invalid enum number is not allowed in proto2."""
m = unittest_pb2.TestAllTypes()
# Proto2 can not assign unknown enum.
with self.assertRaises(ValueError) as _:
m.optional_nested_enum = 1234567
self.assertRaises(ValueError, m.repeated_nested_enum.append, 1234567)
# Assignment is a different code path than append for the C++ impl.
m.repeated_nested_enum.append(2)
m.repeated_nested_enum[0] = 2
with self.assertRaises(ValueError):
m.repeated_nested_enum[0] = 123456
# Unknown enum value can be parsed but is ignored.
m2 = unittest_proto3_arena_pb2.TestAllTypes()
m2.optional_nested_enum = 1234567
m2.repeated_nested_enum.append(7654321)
serialized = m2.SerializeToString()
m3 = unittest_pb2.TestAllTypes()
m3.ParseFromString(serialized)
self.assertFalse(m3.HasField('optional_nested_enum'))
# 1 is the default value for optional_nested_enum.
self.assertEqual(1, m3.optional_nested_enum)
self.assertEqual(0, len(m3.repeated_nested_enum))
m2.Clear()
m2.ParseFromString(m3.SerializeToString())
self.assertEqual(1234567, m2.optional_nested_enum)
self.assertEqual(7654321, m2.repeated_nested_enum[0])