我们从Python开源项目中,提取了以下47个代码示例,用于说明如何使用google.protobuf.message.repeated_float()。
def testPositiveInfinity(self, message_module): if message_module is unittest_pb2: golden_data = (b'\x5D\x00\x00\x80\x7F' b'\x61\x00\x00\x00\x00\x00\x00\xF0\x7F' b'\xCD\x02\x00\x00\x80\x7F' b'\xD1\x02\x00\x00\x00\x00\x00\x00\xF0\x7F') else: golden_data = (b'\x5D\x00\x00\x80\x7F' b'\x61\x00\x00\x00\x00\x00\x00\xF0\x7F' b'\xCA\x02\x04\x00\x00\x80\x7F' b'\xD2\x02\x08\x00\x00\x00\x00\x00\x00\xF0\x7F') golden_message = message_module.TestAllTypes() golden_message.ParseFromString(golden_data) self.assertTrue(IsPosInf(golden_message.optional_float)) self.assertTrue(IsPosInf(golden_message.optional_double)) self.assertTrue(IsPosInf(golden_message.repeated_float[0])) self.assertTrue(IsPosInf(golden_message.repeated_double[0])) self.assertEqual(golden_data, golden_message.SerializeToString())
def testNegativeInfinity(self, message_module): if message_module is unittest_pb2: golden_data = (b'\x5D\x00\x00\x80\xFF' b'\x61\x00\x00\x00\x00\x00\x00\xF0\xFF' b'\xCD\x02\x00\x00\x80\xFF' b'\xD1\x02\x00\x00\x00\x00\x00\x00\xF0\xFF') else: golden_data = (b'\x5D\x00\x00\x80\xFF' b'\x61\x00\x00\x00\x00\x00\x00\xF0\xFF' b'\xCA\x02\x04\x00\x00\x80\xFF' b'\xD2\x02\x08\x00\x00\x00\x00\x00\x00\xF0\xFF') golden_message = message_module.TestAllTypes() golden_message.ParseFromString(golden_data) self.assertTrue(IsNegInf(golden_message.optional_float)) self.assertTrue(IsNegInf(golden_message.optional_double)) self.assertTrue(IsNegInf(golden_message.repeated_float[0])) self.assertTrue(IsNegInf(golden_message.repeated_double[0])) self.assertEqual(golden_data, golden_message.SerializeToString())
def testNotANumber(self, message_module): golden_data = (b'\x5D\x00\x00\xC0\x7F' b'\x61\x00\x00\x00\x00\x00\x00\xF8\x7F' b'\xCD\x02\x00\x00\xC0\x7F' b'\xD1\x02\x00\x00\x00\x00\x00\x00\xF8\x7F') golden_message = message_module.TestAllTypes() golden_message.ParseFromString(golden_data) self.assertTrue(isnan(golden_message.optional_float)) self.assertTrue(isnan(golden_message.optional_double)) self.assertTrue(isnan(golden_message.repeated_float[0])) self.assertTrue(isnan(golden_message.repeated_double[0])) # The protocol buffer may serialize to any one of multiple different # representations of a NaN. Rather than verify a specific representation, # verify the serialized string can be converted into a correctly # behaving protocol buffer. serialized = golden_message.SerializeToString() message = message_module.TestAllTypes() message.ParseFromString(serialized) self.assertTrue(isnan(message.optional_float)) self.assertTrue(isnan(message.optional_double)) self.assertTrue(isnan(message.repeated_float[0])) self.assertTrue(isnan(message.repeated_double[0]))
def testNotANumber(self): golden_data = (b'\x5D\x00\x00\xC0\x7F' b'\x61\x00\x00\x00\x00\x00\x00\xF8\x7F' b'\xCD\x02\x00\x00\xC0\x7F' b'\xD1\x02\x00\x00\x00\x00\x00\x00\xF8\x7F') golden_message = unittest_pb2.TestAllTypes() golden_message.ParseFromString(golden_data) self.assertTrue(isnan(golden_message.optional_float)) self.assertTrue(isnan(golden_message.optional_double)) self.assertTrue(isnan(golden_message.repeated_float[0])) self.assertTrue(isnan(golden_message.repeated_double[0])) # The protocol buffer may serialize to any one of multiple different # representations of a NaN. Rather than verify a specific representation, # verify the serialized string can be converted into a correctly # behaving protocol buffer. serialized = golden_message.SerializeToString() message = unittest_pb2.TestAllTypes() message.ParseFromString(serialized) self.assertTrue(isnan(message.optional_float)) self.assertTrue(isnan(message.optional_double)) self.assertTrue(isnan(message.repeated_float[0])) self.assertTrue(isnan(message.repeated_double[0]))
def testNotANumber(self): golden_data = ('\x5D\x00\x00\xC0\x7F' '\x61\x00\x00\x00\x00\x00\x00\xF8\x7F' '\xCD\x02\x00\x00\xC0\x7F' '\xD1\x02\x00\x00\x00\x00\x00\x00\xF8\x7F') golden_message = unittest_pb2.TestAllTypes() golden_message.ParseFromString(golden_data) self.assertTrue(isnan(golden_message.optional_float)) self.assertTrue(isnan(golden_message.optional_double)) self.assertTrue(isnan(golden_message.repeated_float[0])) self.assertTrue(isnan(golden_message.repeated_double[0])) # The protocol buffer may serialize to any one of multiple different # representations of a NaN. Rather than verify a specific representation, # verify the serialized string can be converted into a correctly # behaving protocol buffer. serialized = golden_message.SerializeToString() message = unittest_pb2.TestAllTypes() message.ParseFromString(serialized) self.assertTrue(isnan(message.optional_float)) self.assertTrue(isnan(message.optional_double)) self.assertTrue(isnan(message.repeated_float[0])) self.assertTrue(isnan(message.repeated_double[0]))
def testSortingRepeatedScalarFieldsDefaultComparator(self, message_module): """Check some different types with the default comparator.""" message = message_module.TestAllTypes() # TODO(mattp): would testing more scalar types strengthen test? message.repeated_int32.append(1) message.repeated_int32.append(3) message.repeated_int32.append(2) message.repeated_int32.sort() self.assertEqual(message.repeated_int32[0], 1) self.assertEqual(message.repeated_int32[1], 2) self.assertEqual(message.repeated_int32[2], 3) message.repeated_float.append(1.1) message.repeated_float.append(1.3) message.repeated_float.append(1.2) message.repeated_float.sort() self.assertAlmostEqual(message.repeated_float[0], 1.1) self.assertAlmostEqual(message.repeated_float[1], 1.2) self.assertAlmostEqual(message.repeated_float[2], 1.3) message.repeated_string.append('a') message.repeated_string.append('c') message.repeated_string.append('b') message.repeated_string.sort() self.assertEqual(message.repeated_string[0], 'a') self.assertEqual(message.repeated_string[1], 'b') self.assertEqual(message.repeated_string[2], 'c') message.repeated_bytes.append(b'a') message.repeated_bytes.append(b'c') message.repeated_bytes.append(b'b') message.repeated_bytes.sort() self.assertEqual(message.repeated_bytes[0], b'a') self.assertEqual(message.repeated_bytes[1], b'b') self.assertEqual(message.repeated_bytes[2], b'c')
def testExtendFloatWithNothing(self, message_module): """Test no-ops extending repeated float fields.""" m = message_module.TestAllTypes() self.assertSequenceEqual([], m.repeated_float) # TODO(ptucker): Deprecate this behavior. b/18413862 for falsy_value in MessageTest.FALSY_VALUES: m.repeated_float.extend(falsy_value) self.assertSequenceEqual([], m.repeated_float) m.repeated_float.extend([]) self.assertSequenceEqual([], m.repeated_float)
def testExtendFloatWithIterable(self, message_module): """Test extending repeated float fields with iterable.""" m = message_module.TestAllTypes() self.assertSequenceEqual([], m.repeated_float) m.repeated_float.extend(MessageTest.TestIterable([])) self.assertSequenceEqual([], m.repeated_float) m.repeated_float.extend(MessageTest.TestIterable([0.0])) self.assertSequenceEqual([0.0], m.repeated_float) m.repeated_float.extend(MessageTest.TestIterable([1.0, 2.0])) self.assertSequenceEqual([0.0, 1.0, 2.0], m.repeated_float) m.repeated_float.extend(MessageTest.TestIterable([3.0, 4.0])) self.assertSequenceEqual([0.0, 1.0, 2.0, 3.0, 4.0], m.repeated_float)
def setMessage(self, message): message.repeated_int32.append(1) message.repeated_int64.append(1) message.repeated_uint32.append(1) message.repeated_uint64.append(1) message.repeated_sint32.append(1) message.repeated_sint64.append(1) message.repeated_fixed32.append(1) message.repeated_fixed64.append(1) message.repeated_sfixed32.append(1) message.repeated_sfixed64.append(1) message.repeated_float.append(1.0) message.repeated_double.append(1.0) message.repeated_bool.append(True) message.repeated_nested_enum.append(1)
def testPositiveInfinity(self): golden_data = (b'\x5D\x00\x00\x80\x7F' b'\x61\x00\x00\x00\x00\x00\x00\xF0\x7F' b'\xCD\x02\x00\x00\x80\x7F' b'\xD1\x02\x00\x00\x00\x00\x00\x00\xF0\x7F') golden_message = unittest_pb2.TestAllTypes() golden_message.ParseFromString(golden_data) self.assertTrue(IsPosInf(golden_message.optional_float)) self.assertTrue(IsPosInf(golden_message.optional_double)) self.assertTrue(IsPosInf(golden_message.repeated_float[0])) self.assertTrue(IsPosInf(golden_message.repeated_double[0])) self.assertEqual(golden_data, golden_message.SerializeToString())
def testNegativeInfinity(self): golden_data = (b'\x5D\x00\x00\x80\xFF' b'\x61\x00\x00\x00\x00\x00\x00\xF0\xFF' b'\xCD\x02\x00\x00\x80\xFF' b'\xD1\x02\x00\x00\x00\x00\x00\x00\xF0\xFF') golden_message = unittest_pb2.TestAllTypes() golden_message.ParseFromString(golden_data) self.assertTrue(IsNegInf(golden_message.optional_float)) self.assertTrue(IsNegInf(golden_message.optional_double)) self.assertTrue(IsNegInf(golden_message.repeated_float[0])) self.assertTrue(IsNegInf(golden_message.repeated_double[0])) self.assertEqual(golden_data, golden_message.SerializeToString())
def testSortingRepeatedScalarFieldsDefaultComparator(self): """Check some different types with the default comparator.""" message = unittest_pb2.TestAllTypes() # TODO(mattp): would testing more scalar types strengthen test? message.repeated_int32.append(1) message.repeated_int32.append(3) message.repeated_int32.append(2) message.repeated_int32.sort() self.assertEqual(message.repeated_int32[0], 1) self.assertEqual(message.repeated_int32[1], 2) self.assertEqual(message.repeated_int32[2], 3) message.repeated_float.append(1.1) message.repeated_float.append(1.3) message.repeated_float.append(1.2) message.repeated_float.sort() self.assertAlmostEqual(message.repeated_float[0], 1.1) self.assertAlmostEqual(message.repeated_float[1], 1.2) self.assertAlmostEqual(message.repeated_float[2], 1.3) message.repeated_string.append('a') message.repeated_string.append('c') message.repeated_string.append('b') message.repeated_string.sort() self.assertEqual(message.repeated_string[0], 'a') self.assertEqual(message.repeated_string[1], 'b') self.assertEqual(message.repeated_string[2], 'c') message.repeated_bytes.append(b'a') message.repeated_bytes.append(b'c') message.repeated_bytes.append(b'b') message.repeated_bytes.sort() self.assertEqual(message.repeated_bytes[0], b'a') self.assertEqual(message.repeated_bytes[1], b'b') self.assertEqual(message.repeated_bytes[2], b'c')
def testPositiveInfinity(self): golden_data = ('\x5D\x00\x00\x80\x7F' '\x61\x00\x00\x00\x00\x00\x00\xF0\x7F' '\xCD\x02\x00\x00\x80\x7F' '\xD1\x02\x00\x00\x00\x00\x00\x00\xF0\x7F') golden_message = unittest_pb2.TestAllTypes() golden_message.ParseFromString(golden_data) self.assertTrue(IsPosInf(golden_message.optional_float)) self.assertTrue(IsPosInf(golden_message.optional_double)) self.assertTrue(IsPosInf(golden_message.repeated_float[0])) self.assertTrue(IsPosInf(golden_message.repeated_double[0])) self.assertEqual(golden_data, golden_message.SerializeToString())
def testNegativeInfinity(self): golden_data = ('\x5D\x00\x00\x80\xFF' '\x61\x00\x00\x00\x00\x00\x00\xF0\xFF' '\xCD\x02\x00\x00\x80\xFF' '\xD1\x02\x00\x00\x00\x00\x00\x00\xF0\xFF') golden_message = unittest_pb2.TestAllTypes() golden_message.ParseFromString(golden_data) self.assertTrue(IsNegInf(golden_message.optional_float)) self.assertTrue(IsNegInf(golden_message.optional_double)) self.assertTrue(IsNegInf(golden_message.repeated_float[0])) self.assertTrue(IsNegInf(golden_message.repeated_double[0])) self.assertEqual(golden_data, golden_message.SerializeToString())
def testSortingRepeatedScalarFieldsDefaultComparator(self): """Check some different types with the default comparator.""" message = unittest_pb2.TestAllTypes() # TODO(mattp): would testing more scalar types strengthen test? message.repeated_int32.append(1) message.repeated_int32.append(3) message.repeated_int32.append(2) message.repeated_int32.sort() self.assertEqual(message.repeated_int32[0], 1) self.assertEqual(message.repeated_int32[1], 2) self.assertEqual(message.repeated_int32[2], 3) message.repeated_float.append(1.1) message.repeated_float.append(1.3) message.repeated_float.append(1.2) message.repeated_float.sort() self.assertAlmostEqual(message.repeated_float[0], 1.1) self.assertAlmostEqual(message.repeated_float[1], 1.2) self.assertAlmostEqual(message.repeated_float[2], 1.3) message.repeated_string.append('a') message.repeated_string.append('c') message.repeated_string.append('b') message.repeated_string.sort() self.assertEqual(message.repeated_string[0], 'a') self.assertEqual(message.repeated_string[1], 'b') self.assertEqual(message.repeated_string[2], 'c') message.repeated_bytes.append('a') message.repeated_bytes.append('c') message.repeated_bytes.append('b') message.repeated_bytes.sort() self.assertEqual(message.repeated_bytes[0], 'a') self.assertEqual(message.repeated_bytes[1], 'b') self.assertEqual(message.repeated_bytes[2], 'c')
def testExtendFloatWithPythonList(self, message_module): """Test extending repeated float fields with python lists.""" m = message_module.TestAllTypes() self.assertSequenceEqual([], m.repeated_float) m.repeated_float.extend([0.0]) self.assertSequenceEqual([0.0], m.repeated_float) m.repeated_float.extend([1.0, 2.0]) self.assertSequenceEqual([0.0, 1.0, 2.0], m.repeated_float) m.repeated_float.extend([3.0, 4.0]) self.assertSequenceEqual([0.0, 1.0, 2.0, 3.0, 4.0], m.repeated_float)
def testPythonicInit(self): message = unittest_pb2.TestAllTypes( optional_int32=100, optional_fixed32=200, optional_float=300.5, optional_bytes=b'x', optionalgroup={'a': 400}, optional_nested_message={'bb': 500}, optional_nested_enum='BAZ', repeatedgroup=[{'a': 600}, {'a': 700}], repeated_nested_enum=['FOO', unittest_pb2.TestAllTypes.BAR], default_int32=800, oneof_string='y') self.assertIsInstance(message, unittest_pb2.TestAllTypes) self.assertEqual(100, message.optional_int32) self.assertEqual(200, message.optional_fixed32) self.assertEqual(300.5, message.optional_float) self.assertEqual(b'x', message.optional_bytes) self.assertEqual(400, message.optionalgroup.a) self.assertIsInstance(message.optional_nested_message, unittest_pb2.TestAllTypes.NestedMessage) self.assertEqual(500, message.optional_nested_message.bb) self.assertEqual(unittest_pb2.TestAllTypes.BAZ, message.optional_nested_enum) self.assertEqual(2, len(message.repeatedgroup)) self.assertEqual(600, message.repeatedgroup[0].a) self.assertEqual(700, message.repeatedgroup[1].a) self.assertEqual(2, len(message.repeated_nested_enum)) self.assertEqual(unittest_pb2.TestAllTypes.FOO, message.repeated_nested_enum[0]) self.assertEqual(unittest_pb2.TestAllTypes.BAR, message.repeated_nested_enum[1]) self.assertEqual(800, message.default_int32) self.assertEqual('y', message.oneof_string) self.assertFalse(message.HasField('optional_int64')) self.assertEqual(0, len(message.repeated_float)) self.assertEqual(42, message.default_int64) message = unittest_pb2.TestAllTypes(optional_nested_enum=u'BAZ') self.assertEqual(unittest_pb2.TestAllTypes.BAZ, message.optional_nested_enum) with self.assertRaises(ValueError): unittest_pb2.TestAllTypes( optional_nested_message={'INVALID_NESTED_FIELD': 17}) with self.assertRaises(TypeError): unittest_pb2.TestAllTypes( optional_nested_message={'bb': 'INVALID_VALUE_TYPE'}) with self.assertRaises(ValueError): unittest_pb2.TestAllTypes(optional_nested_enum='INVALID_LABEL') with self.assertRaises(ValueError): unittest_pb2.TestAllTypes(repeated_nested_enum='FOO') # Class to test proto3-only features/behavior (updated field presence & enums)