本文整理匯總了Python中krpc.types.Types.as_type方法的典型用法代碼示例。如果您正苦於以下問題:Python Types.as_type方法的具體用法?Python Types.as_type怎麽用?Python Types.as_type使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類krpc.types.Types
的用法示例。
在下文中一共展示了Types.as_type方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_class_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
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)
示例2: test_coerce_to
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
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.assertEqual(['foo','bar'], types.coerce_to(['foo','bar'], types.as_type('List(string)')))
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)'))
示例3: test_value_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
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')
示例4: test_tuple_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
def test_tuple_types(self):
types = Types()
typ = types.as_type('Tuple(bool)')
self.assertTrue(isinstance(typ, TupleType))
self.assertEqual(typ.python_type, tuple)
self.assertEqual('Tuple(bool)', typ.protobuf_type)
self.assertEqual(1, len(typ.value_types))
self.assertTrue(isinstance(typ.value_types[0], ValueType))
self.assertEqual('bool', typ.value_types[0].protobuf_type)
self.assertEqual(bool, typ.value_types[0].python_type)
typ = types.as_type('Tuple(int32,string)')
self.assertTrue(isinstance(typ, TupleType))
self.assertEqual(typ.python_type, tuple)
self.assertEqual('Tuple(int32,string)', typ.protobuf_type)
self.assertEqual(2, len(typ.value_types))
self.assertTrue(isinstance(typ.value_types[0], ValueType))
self.assertTrue(isinstance(typ.value_types[1], ValueType))
self.assertEqual('int32', typ.value_types[0].protobuf_type)
self.assertEqual('string', typ.value_types[1].protobuf_type)
self.assertEqual(int, typ.value_types[0].python_type)
self.assertEqual(str, typ.value_types[1].python_type)
typ = types.as_type('Tuple(float,int64,string)')
self.assertTrue(isinstance(typ, TupleType))
self.assertEqual(typ.python_type, tuple)
self.assertEqual('Tuple(float,int64,string)', typ.protobuf_type)
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].protobuf_type)
self.assertEqual('int64', typ.value_types[1].protobuf_type)
self.assertEqual('string', typ.value_types[2].protobuf_type)
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.assertRaises(ValueError, types.as_type, 'Tuple')
self.assertRaises(ValueError, types.as_type, 'Tuple(')
self.assertRaises(ValueError, types.as_type, 'Tuple()')
self.assertRaises(ValueError, types.as_type, 'Tuple(foo')
self.assertRaises(ValueError, types.as_type, 'Tuple(string,)')
self.assertRaises(ValueError, types.as_type, 'Tuple(,)')
self.assertRaises(ValueError, types.as_type, 'Tuple(,string)')
self.assertRaises(ValueError, types.as_type, 'Tuple(int,string))')
示例5: test_message_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
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')
示例6: test_enum_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
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)
示例7: test_get_parameter_type
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
def test_get_parameter_type(self):
types = Types()
self.assertEqual(float, types.get_parameter_type(0, 'float', []).python_type)
self.assertEqual('int32', types.get_parameter_type(0, 'int32', []).protobuf_type)
self.assertEqual('KRPC.Response', types.get_parameter_type(1, 'KRPC.Response', []).protobuf_type)
class_parameter = types.get_parameter_type(0, 'uint64', ['ParameterType(0).Class(ServiceName.ClassName)'])
self.assertEqual(types.as_type('Class(ServiceName.ClassName)'), class_parameter)
self.assertTrue(isinstance(class_parameter, ClassType))
self.assertTrue(issubclass(class_parameter.python_type, ClassBase))
self.assertEqual('Class(ServiceName.ClassName)', class_parameter.protobuf_type)
self.assertEqual('uint64', types.get_parameter_type(0, 'uint64', ['ParameterType(1).Class(ServiceName.ClassName)']).protobuf_type)
示例8: test_protobuf_enum_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
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')
示例9: test_list_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
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)')
示例10: test_class_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
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)
示例11: test_set_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
def test_set_types(self):
types = Types()
typ = types.as_type('Set(string)')
self.assertTrue(isinstance(typ, SetType))
self.assertEqual(typ.python_type, set)
self.assertEqual('Set(string)', typ.protobuf_type)
self.assertTrue(isinstance(typ.value_type, ValueType))
self.assertEqual('string', typ.value_type.protobuf_type)
self.assertEqual(str, typ.value_type.python_type)
self.assertRaises(ValueError, types.as_type, 'Set')
self.assertRaises(ValueError, types.as_type, 'Set(')
self.assertRaises(ValueError, types.as_type, 'Set()')
self.assertRaises(ValueError, types.as_type, 'Set(string,)')
self.assertRaises(ValueError, types.as_type, 'Set(,)')
self.assertRaises(ValueError, types.as_type, 'Set(,string)')
self.assertRaises(ValueError, types.as_type, 'Set(int,string))')
示例12: test_coerce_to
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
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))
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.as_type('string')))
self.assertEqual(['foo', 'bar'], types.coerce_to(['foo', 'bar'], types.as_type('List(string)')))
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)'))
示例13: test_dictionary_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
def test_dictionary_types(self):
types = Types()
typ = types.as_type('Dictionary(string,int32)')
self.assertTrue(isinstance(typ, DictionaryType))
self.assertEqual(typ.python_type, dict)
self.assertEqual('Dictionary(string,int32)', typ.protobuf_type)
self.assertTrue(isinstance(typ.key_type, ValueType))
self.assertEqual('string', typ.key_type.protobuf_type)
self.assertEqual(str, typ.key_type.python_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, 'Dictionary')
self.assertRaises(ValueError, types.as_type, 'Dictionary(')
self.assertRaises(ValueError, types.as_type, 'Dictionary()')
self.assertRaises(ValueError, types.as_type, 'Dictionary(foo')
self.assertRaises(ValueError, types.as_type, 'Dictionary(string)')
self.assertRaises(ValueError, types.as_type, 'Dictionary(string,)')
self.assertRaises(ValueError, types.as_type, 'Dictionary(,)')
self.assertRaises(ValueError, types.as_type, 'Dictionary(,string)')
self.assertRaises(ValueError, types.as_type, 'Dictionary(int,string))')
示例14: test_enumeration_types
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
def test_enumeration_types(self):
types = Types()
typ = types.enumeration_type(
'ServiceName', 'EnumName', 'enum documentation')
self.assertTrue(isinstance(typ, EnumerationType))
self.assertIsNone(typ.python_type)
self.check_protobuf_type(
Type.ENUMERATION, 'ServiceName', 'EnumName', 0, typ.protobuf_type)
typ.set_values({
'a': {'value': 0, 'doc': 'doca'},
'b': {'value': 42, 'doc': 'docb'},
'c': {'value': 100, 'doc': 'docc'}
})
self.assertTrue(issubclass(typ.python_type, Enum))
self.assertEqual('enum documentation', typ.python_type.__doc__)
self.assertEquals(0, typ.python_type.a.value)
self.assertEquals(42, typ.python_type.b.value)
self.assertEquals(100, typ.python_type.c.value)
self.assertEquals('doca', typ.python_type.a.__doc__)
self.assertEquals('docb', typ.python_type.b.__doc__)
self.assertEquals('docc', typ.python_type.c.__doc__)
typ2 = types.as_type(typ.protobuf_type)
self.assertEqual(typ, typ2)
示例15: Client
# 需要導入模塊: from krpc.types import Types [as 別名]
# 或者: from krpc.types.Types import as_type [as 別名]
class Client(object):
"""
A kRPC client, through which all Remote Procedure Calls are made.
Services provided by the server that the client connects to are automatically added.
RPCs can be made using client.ServiceName.ProcedureName(parameter)
"""
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._request_type = self._types.as_type("KRPC.Request")
self._response_type = self._types.as_type("KRPC.Response")
# Set up the main KRPC service
self.krpc = KRPC(self)
services = self.krpc.get_services().services
# Create class types
# TODO: is this needed?!?
for service in services:
for procedure in service.procedures:
try:
name = Attributes.get_class_name(procedure.attributes)
self._types.as_type("Class(" + service.name + "." + name + ")")
except ValueError:
pass
# Set up services
for service in services:
if service.name != "KRPC":
setattr(self, _to_snake_case(service.name), create_service(self, service))
# Set up stream update thread
if stream_connection is not None:
self._stream_thread = threading.Thread(target=krpc.stream.update_thread, args=(stream_connection,))
self._stream_thread.daemon = True
self._stream_thread.start()
else:
self._stream_thread = None
def close(self):
self._rpc_connection.close()
if self._stream_connection is not None:
self._stream_connection.close()
def __enter__(self):
return self
def __exit__(self, typ, value, traceback):
self.close()
def add_stream(self, func, *args, **kwargs):
if self._stream_connection is None:
raise RuntimeError("Not connected to stream server")
return krpc.stream.add_stream(self, func, *args, **kwargs)
@contextmanager
def stream(self, func, *args, **kwargs):
""" 'with' support """
s = self.add_stream(func, *args, **kwargs)
try:
yield s
finally:
s.remove()
def _invoke(self, service, procedure, args=[], kwargs={}, param_names=[], param_types=[], return_type=None):
""" Execute an RPC """
# Build the request
request = self._build_request(service, procedure, args, kwargs, param_names, param_types, return_type)
# Send the request
with self._rpc_connection_lock:
self._send_request(request)
response = self._receive_response()
# Check for an error response
if response.HasField("error"):
raise RPCError(response.error)
# Decode the response and return the (optional) result
result = None
if return_type is not None:
result = Decoder.decode(response.return_value, return_type)
return result
def _build_request(self, service, procedure, args=[], kwargs={}, param_names=[], param_types=[], return_type=None):
""" Build a KRPC.Request object """
def encode_argument(i, value):
typ = param_types[i]
if type(value) != typ.python_type:
# Try coercing to the correct type
try:
value = self._types.coerce_to(value, typ)
except ValueError:
raise TypeError(
#.........這裏部分代碼省略.........