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


Python Types.as_type方法代码示例

本文整理汇总了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)
开发者ID:paperclip,项目名称:krpc,代码行数:14,代码来源:test_types.py

示例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)'))
开发者ID:Kerbal007,项目名称:krpc,代码行数:33,代码来源:test_types.py

示例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')
开发者ID:paperclip,项目名称:krpc,代码行数:11,代码来源:test_types.py

示例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))')
开发者ID:paperclip,项目名称:krpc,代码行数:45,代码来源:test_types.py

示例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')
开发者ID:paperclip,项目名称:krpc,代码行数:13,代码来源:test_types.py

示例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)
开发者ID:Kerbal007,项目名称:krpc,代码行数:13,代码来源:test_types.py

示例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)
开发者ID:Kerbal007,项目名称:krpc,代码行数:13,代码来源:test_types.py

示例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')
开发者ID:rosalesr,项目名称:krpc,代码行数:13,代码来源:test_types.py

示例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)')
开发者ID:paperclip,项目名称:krpc,代码行数:16,代码来源:test_types.py

示例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)
开发者ID:Loran425,项目名称:krpc,代码行数:17,代码来源:test_types.py

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

示例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)'))
开发者ID:paperclip,项目名称:krpc,代码行数:41,代码来源:test_types.py

示例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))')
开发者ID:paperclip,项目名称:krpc,代码行数:23,代码来源:test_types.py

示例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)
开发者ID:Loran425,项目名称:krpc,代码行数:25,代码来源:test_types.py

示例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(
#.........这里部分代码省略.........
开发者ID:602p,项目名称:krpc,代码行数:103,代码来源:client.py


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