当前位置: 首页>>代码示例>>Python>>正文


Python duration_pb2.Duration方法代码示例

本文整理汇总了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) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:23,代码来源:well_known_types_test.py

示例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') 
开发者ID:quantumlib,项目名称:Cirq,代码行数:18,代码来源:engine_processor_test.py

示例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') 
开发者ID:quantumlib,项目名称:Cirq,代码行数:18,代码来源:engine_processor_test.py

示例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 
开发者ID:luci,项目名称:luci-py,代码行数:22,代码来源:bqh.py

示例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() 
开发者ID:open-telemetry,项目名称:opentelemetry-python,代码行数:20,代码来源:test_otlp_trace_exporter.py

示例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) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:7,代码来源:well_known_types_test.py

示例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) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:29,代码来源:well_known_types_test.py

示例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) 
开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:31,代码来源:well_known_types_test.py

示例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') 
开发者ID:quantumlib,项目名称:Cirq,代码行数:9,代码来源:engine_processor_test.py

示例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()) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:50,代码来源:well_known_types_test.py


注:本文中的google.protobuf.duration_pb2.Duration方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。