我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用google.protobuf.message.Clear()。
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])
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'))