本文整理汇总了Python中krpc.types.Types类的典型用法代码示例。如果您正苦于以下问题:Python Types类的具体用法?Python Types怎么用?Python Types使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Types类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_return_type
def test_get_return_type(self):
types = Types()
self.assertEqual('float', types.get_return_type('float', []).protobuf_type)
self.assertEqual('int32', types.get_return_type('int32', []).protobuf_type)
self.assertEqual('KRPC.Response', types.get_return_type('KRPC.Response', []).protobuf_type)
self.assertEqual('Class(ServiceName.ClassName)',
types.get_return_type('uint64', ['ReturnType.Class(ServiceName.ClassName)']).protobuf_type)
示例2: test_tuple_3_types
def test_tuple_3_types(self):
types = Types()
typ = types.tuple_type(
types.float_type, types.uint64_type, types.string_type)
self.assertTrue(isinstance(typ, TupleType))
self.assertEqual(typ.python_type, tuple)
self.check_protobuf_type(
Type.TUPLE, '', '', 3, typ.protobuf_type)
self.check_protobuf_type(
Type.FLOAT, '', '', 0, typ.protobuf_type.types[0])
self.check_protobuf_type(
Type.UINT64, '', '', 0, typ.protobuf_type.types[1])
self.check_protobuf_type(
Type.STRING, '', '', 0, typ.protobuf_type.types[2])
self.assertEqual(3, len(typ.value_types))
self.assertTrue(isinstance(typ.value_types[0], ValueType))
self.assertTrue(isinstance(typ.value_types[1], ValueType))
self.assertTrue(isinstance(typ.value_types[2], ValueType))
self.assertEqual(float, typ.value_types[0].python_type)
self.assertEqual(long, typ.value_types[1].python_type)
self.assertEqual(str, typ.value_types[2].python_type)
self.check_protobuf_type(
Type.FLOAT, '', '', 0, typ.value_types[0].protobuf_type)
self.check_protobuf_type(
Type.UINT64, '', '', 0, typ.value_types[1].protobuf_type)
self.check_protobuf_type(
Type.STRING, '', '', 0, typ.value_types[2].protobuf_type)
示例3: test_coerce_to
def test_coerce_to(self):
types = Types()
cases = [
(42.0, 42, 'double'),
(42.0, 42, 'float'),
(42, 42.0, 'int32'),
(42, 42L, 'int32'),
(42L, 42.0, 'int64'),
(42L, 42, 'int64'),
(42, 42.0, 'uint32'),
(42, 42L, 'uint32'),
(42L, 42.0, 'uint64'),
(42L, 42, 'uint64'),
(list(), tuple(), 'List(string)'),
((0,1,2), [0,1,2], 'Tuple(int32,int32,int32)'),
([0,1,2], (0,1,2), 'List(int32)'),
]
for expected, value, typ in cases:
coerced_value = types.coerce_to(value, types.as_type(typ))
self.assertEqual(expected, coerced_value)
self.assertEqual(type(expected), type(coerced_value))
self.assertRaises(ValueError, types.coerce_to, None, types.as_type('float'))
self.assertRaises(ValueError, types.coerce_to, '', types.as_type('float'))
self.assertRaises(ValueError, types.coerce_to, True, types.as_type('float'))
self.assertRaises(ValueError, types.coerce_to, list(), types.as_type('Tuple(int32)'))
self.assertRaises(ValueError, types.coerce_to, ["foo",2], types.as_type('Tuple(string)'))
self.assertRaises(ValueError, types.coerce_to, [1], types.as_type('Tuple(string)'))
self.assertRaises(ValueError, types.coerce_to, [1,"a","b"], types.as_type('List(string)'))
示例4: test_value_types
def test_value_types(self):
types = Types()
for protobuf_typ in PROTOBUF_VALUE_TYPES:
python_typ = PROTOBUF_TO_PYTHON_VALUE_TYPE[protobuf_typ]
typ = types.as_type(protobuf_typ)
self.assertTrue(isinstance(typ, ValueType))
self.assertEqual(protobuf_typ, typ.protobuf_type)
self.assertEqual(python_typ, typ.python_type)
self.assertRaises(ValueError, ValueType, 'invalid')
示例5: test_enum_types
def test_enum_types(self):
types = Types()
typ = types.as_type('Enum(ServiceName.EnumName)')
self.assertTrue(isinstance(typ, EnumType))
self.assertEqual(None, typ.python_type)
self.assertTrue('Enum(ServiceName.EnumName)', typ.protobuf_type)
typ.set_values({'a': 0, 'b': 42, 'c': 100})
self.assertTrue(issubclass(typ.python_type, Enum))
self.assertEquals(0, typ.python_type.a.value)
self.assertEquals(42, typ.python_type.b.value)
self.assertEquals(100, typ.python_type.c.value)
示例6: test_protobuf_enum_types
def test_protobuf_enum_types(self):
types = Types()
typ = types.as_type('Test.TestEnum')
self.assertTrue(isinstance(typ, ProtobufEnumType))
self.assertEqual(int, typ.python_type)
self.assertEqual('Test.TestEnum', typ.protobuf_type)
self.assertRaises(ValueError, types.as_type, 'Test.DoesntExist')
self.assertRaises(ValueError, ProtobufEnumType, '')
self.assertRaises(ValueError, ProtobufEnumType, 'invalid')
self.assertRaises(ValueError, ProtobufEnumType, '.')
self.assertRaises(ValueError, ProtobufEnumType, 'foo.bar')
示例7: test_message_types
def test_message_types(self):
types = Types()
typ = types.as_type('KRPC.Request')
self.assertTrue(isinstance(typ, MessageType))
self.assertEqual(krpc.schema.KRPC.Request, typ.python_type)
self.assertEqual('KRPC.Request', typ.protobuf_type)
self.assertRaises(ValueError, types.as_type, 'KRPC.DoesntExist')
self.assertRaises(ValueError, MessageType, '')
self.assertRaises(ValueError, MessageType, 'invalid')
self.assertRaises(ValueError, MessageType, '.')
self.assertRaises(ValueError, MessageType, 'foo.bar')
示例8: test_class_types
def test_class_types(self):
types = Types()
typ = types.as_type('Class(ServiceName.ClassName)')
self.assertTrue(isinstance(typ, ClassType))
self.assertTrue(issubclass(typ.python_type, ClassBase))
self.assertEqual('Class(ServiceName.ClassName)', typ.protobuf_type)
instance = typ.python_type(42)
self.assertEqual(42, instance._object_id)
self.assertEqual('ServiceName', instance._service_name)
self.assertEqual('ClassName', instance._class_name)
typ2 = types.as_type('Class(ServiceName.ClassName)')
self.assertEqual(typ, typ2)
示例9: test_set_types
def test_set_types(self):
types = Types()
typ = types.set_type(types.string_type)
self.assertTrue(isinstance(typ, SetType))
self.assertEqual(typ.python_type, set)
self.check_protobuf_type(
Type.SET, '', '', 1, typ.protobuf_type)
self.check_protobuf_type(
Type.STRING, '', '', 0, typ.protobuf_type.types[0])
self.assertTrue(isinstance(typ.value_type, ValueType))
self.assertEqual(str, typ.value_type.python_type)
self.check_protobuf_type(
Type.STRING, '', '', 0, typ.value_type.protobuf_type)
示例10: test_list_types
def test_list_types(self):
types = Types()
typ = types.list_type(types.uint32_type)
self.assertTrue(isinstance(typ, ListType))
self.assertEqual(typ.python_type, list)
self.check_protobuf_type(
Type.LIST, '', '', 1, typ.protobuf_type)
self.check_protobuf_type(
Type.UINT32, '', '', 0, typ.protobuf_type.types[0])
self.assertTrue(isinstance(typ.value_type, ValueType))
self.assertEqual(int, typ.value_type.python_type)
self.check_protobuf_type(
Type.UINT32, '', '', 0, typ.value_type.protobuf_type)
示例11: test_tuple_1_types
def test_tuple_1_types(self):
types = Types()
typ = types.tuple_type(types.bool_type)
self.assertTrue(isinstance(typ, TupleType))
self.assertEqual(typ.python_type, tuple)
self.check_protobuf_type(
Type.TUPLE, '', '', 1, typ.protobuf_type)
self.check_protobuf_type(
Type.BOOL, '', '', 0, typ.protobuf_type.types[0])
self.assertEqual(1, len(typ.value_types))
self.assertTrue(isinstance(typ.value_types[0], ValueType))
self.assertEqual(bool, typ.value_types[0].python_type)
self.check_protobuf_type(
Type.BOOL, '', '', 0, typ.value_types[0].protobuf_type)
示例12: test_coerce_to
def test_coerce_to(self):
types = Types()
cases = [
(42.0, 42, types.double_type),
(42.0, 42, types.float_type),
(42, 42.0, types.sint32_type),
(42, 42L, types.sint32_type),
(42L, 42.0, types.sint64_type),
(42L, 42, types.sint64_type),
(42, 42.0, types.uint32_type),
(42, 42L, types.uint32_type),
(42L, 42.0, types.uint64_type),
(42L, 42, types.uint64_type),
(list(), tuple(), types.list_type(types.string_type)),
((0, 1, 2), [0, 1, 2],
types.tuple_type(types.sint32_type,
types.sint32_type,
types.sint32_type)),
([0, 1, 2], (0, 1, 2),
types.list_type(types.sint32_type)),
(['foo', 'bar'], ['foo', 'bar'],
types.list_type(types.string_type))
]
for expected, value, typ in cases:
coerced_value = types.coerce_to(value, typ)
self.assertEqual(expected, coerced_value)
self.assertEqual(type(expected), type(coerced_value))
strings = [
u'foo',
u'\xe2\x84\xa2',
u'Mystery Goo\xe2\x84\xa2 Containment Unit'
]
for string in strings:
self.assertEqual(
string, types.coerce_to(string, types.string_type))
self.assertRaises(ValueError, types.coerce_to,
None, types.float_type)
self.assertRaises(ValueError, types.coerce_to,
'', types.float_type)
self.assertRaises(ValueError, types.coerce_to,
True, types.float_type)
self.assertRaises(ValueError, types.coerce_to,
list(), types.tuple_type(types.uint32_type))
self.assertRaises(ValueError, types.coerce_to,
['foo', 2], types.tuple_type(types.string_type))
self.assertRaises(ValueError, types.coerce_to,
[1], types.tuple_type(types.string_type))
示例13: test_list_types
def test_list_types(self):
types = Types()
typ = types.as_type('List(int32)')
self.assertTrue(isinstance(typ, ListType))
self.assertEqual(typ.python_type, list)
self.assertEqual('List(int32)', typ.protobuf_type)
self.assertTrue(isinstance(typ.value_type, ValueType))
self.assertEqual('int32', typ.value_type.protobuf_type)
self.assertEqual(int, typ.value_type.python_type)
self.assertRaises(ValueError, types.as_type, 'List')
self.assertRaises(ValueError, types.as_type, 'List(')
self.assertRaises(ValueError, types.as_type, 'List()')
self.assertRaises(ValueError, types.as_type, 'List(foo')
self.assertRaises(ValueError, types.as_type, 'List(int32,string)')
示例14: test_class_types
def test_class_types(self):
types = Types()
typ = types.class_type(
'ServiceName', 'ClassName', 'class documentation')
self.assertTrue(isinstance(typ, ClassType))
self.assertTrue(issubclass(typ.python_type, ClassBase))
self.assertEqual('class documentation', typ.python_type.__doc__)
self.check_protobuf_type(
Type.CLASS, 'ServiceName', 'ClassName', 0, typ.protobuf_type)
instance = typ.python_type(42)
self.assertEqual(42, instance._object_id)
self.assertEqual('ServiceName', instance._service_name)
self.assertEqual('ClassName', instance._class_name)
typ2 = types.as_type(typ.protobuf_type)
self.assertEqual(typ, typ2)
示例15: __init__
def __init__(self, rpc_connection, stream_connection):
self._types = Types()
self._rpc_connection = rpc_connection
self._rpc_connection_lock = threading.Lock()
self._stream_connection = stream_connection
self._stream_cache = {}
self._stream_cache_lock = threading.Lock()
self._request_type = self._types.as_type('KRPC.Request')
self._response_type = self._types.as_type('KRPC.Response')
# Get the services
services = self._invoke('KRPC', 'GetServices', [], [], [], self._types.as_type('KRPC.Services')).services
# Set up services
for service in services:
setattr(self, snake_case(service.name), create_service(self, service))
# Set up stream update thread
if stream_connection is not None:
self._stream_thread_stop = threading.Event()
self._stream_thread = threading.Thread(target=krpc.stream.update_thread,
args=(stream_connection, self._stream_thread_stop,
self._stream_cache, self._stream_cache_lock))
self._stream_thread.daemon = True
self._stream_thread.start()
else:
self._stream_thread = None