本文整理汇总了Python中google.protobuf.duration_pb2.Duration方法的典型用法代码示例。如果您正苦于以下问题:Python duration_pb2.Duration方法的具体用法?Python duration_pb2.Duration怎么用?Python duration_pb2.Duration使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.protobuf.duration_pb2
的用法示例。
在下文中一共展示了duration_pb2.Duration方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testTimedeltaConversion
# 需要导入模块: from google.protobuf import duration_pb2 [as 别名]
# 或者: from google.protobuf.duration_pb2 import Duration [as 别名]
def testTimedeltaConversion(self):
message = duration_pb2.Duration()
message.FromNanoseconds(1999999999)
td = message.ToTimedelta()
self.assertEqual(1, td.seconds)
self.assertEqual(999999, td.microseconds)
message.FromNanoseconds(-1999999999)
td = message.ToTimedelta()
self.assertEqual(-1, td.days)
self.assertEqual(86398, td.seconds)
self.assertEqual(1, td.microseconds)
message.FromMicroseconds(-1)
td = message.ToTimedelta()
self.assertEqual(-1, td.days)
self.assertEqual(86399, td.seconds)
self.assertEqual(999999, td.microseconds)
converted_message = duration_pb2.Duration()
converted_message.FromTimedelta(td)
self.assertEqual(message, converted_message)
示例2: test_remove_reservation_delete
# 需要导入模块: from google.protobuf import duration_pb2 [as 别名]
# 或者: from google.protobuf.duration_pb2 import Duration [as 别名]
def test_remove_reservation_delete(delete_reservation, get_reservation):
name = 'projects/proj/processors/p0/reservations/rid'
now = int(datetime.datetime.now().timestamp())
result = qtypes.QuantumReservation(
name=name,
start_time=Timestamp(seconds=now + 20000),
end_time=Timestamp(seconds=now + 23610),
whitelisted_users=['dstrain@google.com'],
)
get_reservation.return_value = result
delete_reservation.return_value = result
processor = cg.EngineProcessor(
'proj', 'p0', EngineContext(),
qtypes.QuantumProcessor(schedule_frozen_period=Duration(seconds=10000)))
assert processor.remove_reservation('rid') == result
delete_reservation.assert_called_once_with('proj', 'p0', 'rid')
示例3: test_remove_reservation_cancel
# 需要导入模块: from google.protobuf import duration_pb2 [as 别名]
# 或者: from google.protobuf.duration_pb2 import Duration [as 别名]
def test_remove_reservation_cancel(cancel_reservation, get_reservation):
name = 'projects/proj/processors/p0/reservations/rid'
now = int(datetime.datetime.now().timestamp())
result = qtypes.QuantumReservation(
name=name,
start_time=Timestamp(seconds=now + 10),
end_time=Timestamp(seconds=now + 3610),
whitelisted_users=['dstrain@google.com'],
)
get_reservation.return_value = result
cancel_reservation.return_value = result
processor = cg.EngineProcessor(
'proj', 'p0', EngineContext(),
qtypes.QuantumProcessor(schedule_frozen_period=Duration(seconds=10000)))
assert processor.remove_reservation('rid') == result
cancel_reservation.assert_called_once_with('proj', 'p0', 'rid')
示例4: _to_bq_value
# 需要导入模块: from google.protobuf import duration_pb2 [as 别名]
# 或者: from google.protobuf.duration_pb2 import Duration [as 别名]
def _to_bq_value(value, field_desc):
if field_desc.enum_type:
# Enums are stored as strings.
enum_val = field_desc.enum_type.values_by_number.get(value)
if not enum_val:
raise ValueError('Invalid value %r for enum type %s' % (
value, field_desc.enum_type.full_name))
return enum_val.name
elif isinstance(value, duration_pb2.Duration):
return value.ToTimedelta().total_seconds()
elif isinstance(value, struct_pb2.Struct):
# Structs are stored as JSONPB strings,
# see https://bit.ly/chromium-bq-struct
return json_format.MessageToJson(value)
elif isinstance(value, timestamp_pb2.Timestamp):
return value.ToDatetime().isoformat()
elif isinstance(value, message_pb.Message):
return message_to_dict(value)
else:
return value
示例5: Export
# 需要导入模块: from google.protobuf import duration_pb2 [as 别名]
# 或者: from google.protobuf.duration_pb2 import Duration [as 别名]
def Export(self, request, context):
context.set_code(StatusCode.UNAVAILABLE)
context.send_initial_metadata(
(("google.rpc.retryinfo-bin", RetryInfo().SerializeToString()),)
)
context.set_trailing_metadata(
(
(
"google.rpc.retryinfo-bin",
RetryInfo(
retry_delay=Duration(seconds=4)
).SerializeToString(),
),
)
)
return ExportTraceServiceResponse()
示例6: CheckDurationConversion
# 需要导入模块: from google.protobuf import duration_pb2 [as 别名]
# 或者: from google.protobuf.duration_pb2 import Duration [as 别名]
def CheckDurationConversion(self, message, text):
self.assertEqual(text, message.ToJsonString())
parsed_message = duration_pb2.Duration()
parsed_message.FromJsonString(text)
self.assertEqual(message, parsed_message)
示例7: testDurationSerializeAndParse
# 需要导入模块: from google.protobuf import duration_pb2 [as 别名]
# 或者: from google.protobuf.duration_pb2 import Duration [as 别名]
def testDurationSerializeAndParse(self):
message = duration_pb2.Duration()
# Generated output should contain 3, 6, or 9 fractional digits.
message.seconds = 0
message.nanos = 0
self.CheckDurationConversion(message, '0s')
message.nanos = 10000000
self.CheckDurationConversion(message, '0.010s')
message.nanos = 10000
self.CheckDurationConversion(message, '0.000010s')
message.nanos = 10
self.CheckDurationConversion(message, '0.000000010s')
# Test min and max
message.seconds = 315576000000
message.nanos = 999999999
self.CheckDurationConversion(message, '315576000000.999999999s')
message.seconds = -315576000000
message.nanos = -999999999
self.CheckDurationConversion(message, '-315576000000.999999999s')
# Parsing accepts an fractional digits as long as they fit into nano
# precision.
message.FromJsonString('0.1s')
self.assertEqual(100000000, message.nanos)
message.FromJsonString('0.0000001s')
self.assertEqual(100, message.nanos)
示例8: testInvalidDuration
# 需要导入模块: from google.protobuf import duration_pb2 [as 别名]
# 或者: from google.protobuf.duration_pb2 import Duration [as 别名]
def testInvalidDuration(self):
message = duration_pb2.Duration()
self.assertRaisesRegexp(
well_known_types.ParseError,
'Duration must end with letter "s": 1.',
message.FromJsonString, '1')
self.assertRaisesRegexp(
well_known_types.ParseError,
'Couldn\'t parse duration: 1...2s.',
message.FromJsonString, '1...2s')
text = '-315576000001.000000000s'
self.assertRaisesRegexp(
well_known_types.Error,
r'Duration is not valid\: Seconds -315576000001 must be in range'
r' \[-315576000000\, 315576000000\].',
message.FromJsonString, text)
text = '315576000001.000000000s'
self.assertRaisesRegexp(
well_known_types.Error,
r'Duration is not valid\: Seconds 315576000001 must be in range'
r' \[-315576000000\, 315576000000\].',
message.FromJsonString, text)
message.seconds = -315576000001
message.nanos = 0
self.assertRaisesRegexp(
well_known_types.Error,
r'Duration is not valid\: Seconds -315576000001 must be in range'
r' \[-315576000000\, 315576000000\].',
message.ToJsonString)
示例9: test_remove_reservation_not_found
# 需要导入模块: from google.protobuf import duration_pb2 [as 别名]
# 或者: from google.protobuf.duration_pb2 import Duration [as 别名]
def test_remove_reservation_not_found(get_reservation):
get_reservation.return_value = None
processor = cg.EngineProcessor(
'proj', 'p0', EngineContext(),
qtypes.QuantumProcessor(schedule_frozen_period=Duration(seconds=10000)))
with pytest.raises(ValueError):
processor.remove_reservation('rid')
示例10: testDurationIntegerConversion
# 需要导入模块: from google.protobuf import duration_pb2 [as 别名]
# 或者: from google.protobuf.duration_pb2 import Duration [as 别名]
def testDurationIntegerConversion(self):
message = duration_pb2.Duration()
message.FromNanoseconds(1)
self.assertEqual('0.000000001s',
message.ToJsonString())
self.assertEqual(1, message.ToNanoseconds())
message.FromNanoseconds(-1)
self.assertEqual('-0.000000001s',
message.ToJsonString())
self.assertEqual(-1, message.ToNanoseconds())
message.FromMicroseconds(1)
self.assertEqual('0.000001s',
message.ToJsonString())
self.assertEqual(1, message.ToMicroseconds())
message.FromMicroseconds(-1)
self.assertEqual('-0.000001s',
message.ToJsonString())
self.assertEqual(-1, message.ToMicroseconds())
message.FromMilliseconds(1)
self.assertEqual('0.001s',
message.ToJsonString())
self.assertEqual(1, message.ToMilliseconds())
message.FromMilliseconds(-1)
self.assertEqual('-0.001s',
message.ToJsonString())
self.assertEqual(-1, message.ToMilliseconds())
message.FromSeconds(1)
self.assertEqual('1s', message.ToJsonString())
self.assertEqual(1, message.ToSeconds())
message.FromSeconds(-1)
self.assertEqual('-1s',
message.ToJsonString())
self.assertEqual(-1, message.ToSeconds())
# Test truncation behavior.
message.FromNanoseconds(1999)
self.assertEqual(1, message.ToMicroseconds())
# For negative values, Duration will be rounded towards 0.
message.FromNanoseconds(-1999)
self.assertEqual(-1, message.ToMicroseconds())