當前位置: 首頁>>代碼示例>>Python>>正文


Python six.with_metaclass方法代碼示例

本文整理匯總了Python中six.with_metaclass方法的典型用法代碼示例。如果您正苦於以下問題:Python six.with_metaclass方法的具體用法?Python six.with_metaclass怎麽用?Python six.with_metaclass使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在six的用法示例。


在下文中一共展示了six.with_metaclass方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: testParsingFlatClassWithExplicitClassDeclaration

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def testParsingFlatClassWithExplicitClassDeclaration(self):
    """Test that the generated class can parse a flat message."""
    # TODO(xiaofeng): This test fails with cpp implemetnation in the call
    # of six.with_metaclass(). The other two callsites of with_metaclass
    # in this file are both excluded from cpp test, so it might be expected
    # to fail. Need someone more familiar with the python code to take a
    # look at this.
    if api_implementation.Type() != 'python':
      return
    file_descriptor = descriptor_pb2.FileDescriptorProto()
    file_descriptor.ParseFromString(self._GetSerializedFileDescriptor('A'))
    msg_descriptor = descriptor.MakeDescriptor(
        file_descriptor.message_type[0])

    class MessageClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
      DESCRIPTOR = msg_descriptor
    msg = MessageClass()
    msg_str = (
        'flat: 0 '
        'flat: 1 '
        'flat: 2 ')
    text_format.Merge(msg_str, msg)
    self.assertEqual(msg.flat, [0, 1, 2]) 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:25,代碼來源:reflection_test.py

示例2: test_decorate_all

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def test_decorate_all():
    def add_two(function):
        def wrap(*args, **kwargs):
            return function(*args, **kwargs) + 2
        return wrap

    class Test(six.with_metaclass(util.decorate_all(add_two))):

        def one(self):
            return 1

        def two(self):
            return 2

    assert Test().one() == 3
    assert Test().two() == 4 
開發者ID:IvanMalison,項目名稱:okcupyd,代碼行數:18,代碼來源:util_test.py

示例3: enable_final

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def enable_final(base=(), meta_base=()):
    """Returns a base class in which ``final`` decorator is made available.

    Inheriting from the returned value of this function enables
    :meth:``~chainer.utils.final`` decorator to be applied to the methods of
    the class.

    Args:
        base (type or tuple of types): Base classes of the returned class.
        meta_base (type or tuples of type): Base metaclasses. If any descendant
            classes can directly or indirectly have any metaclasses, these
            metaclasses should be specified here to avoid the metaclass
            conflict.
    """
    if not isinstance(base, (list, tuple)):
        base = (base,)
    if not isinstance(meta_base, (list, tuple)):
        meta_base = (meta_base,)

    base_metaclass = type('base_metaclass', (_EnableFinal,) + meta_base, {})
    return six.with_metaclass(base_metaclass, *base) 
開發者ID:chainer,項目名稱:chainer,代碼行數:23,代碼來源:meta.py

示例4: setUpClass

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def setUpClass(cls):
        FinalABCMeta = final_meta_factory(ABCMeta)

        class ABCWithFinal(with_metaclass(FinalABCMeta, object)):
            a = final('ABCWithFinal: a')
            b = 'ABCWithFinal: b'

            @final
            def f(self):
                return 'ABCWithFinal: f'

            def g(self):
                return 'ABCWithFinal: g'

            @abstractmethod
            def h(self):
                raise NotImplementedError('h')

        cls.class_ = ABCWithFinal 
開發者ID:zhanghan1990,項目名稱:zipline-chinese,代碼行數:21,代碼來源:test_final.py

示例5: test_subclass_setattr

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def test_subclass_setattr(self):
        """
        Tests that subclasses don't destroy the __setattr__.
        """
        class ClassWithFinal(with_metaclass(FinalMeta, object)):
            @final
            def f(self):
                return 'ClassWithFinal: f'

        class SubClass(ClassWithFinal):
            def __init__(self):
                self.a = 'a'

        SubClass()
        self.assertEqual(SubClass().a, 'a')
        self.assertEqual(SubClass().f(), 'ClassWithFinal: f') 
開發者ID:zhanghan1990,項目名稱:zipline-chinese,代碼行數:18,代碼來源:test_final.py

示例6: setUpClass

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def setUpClass(cls):
        FinalABCMeta = compose_types(FinalMeta, ABCMeta)

        class ABCWithFinal(with_metaclass(FinalABCMeta, object)):
            a = final('ABCWithFinal: a')
            b = 'ABCWithFinal: b'

            @final
            def f(self):
                return 'ABCWithFinal: f'

            def g(self):
                return 'ABCWithFinal: g'

            @abstractmethod
            def h(self):
                raise NotImplementedError('h')

        cls.class_ = ABCWithFinal 
開發者ID:enigmampc,項目名稱:catalyst,代碼行數:21,代碼來源:test_final.py

示例7: with_metaclasses

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def with_metaclasses(metaclasses, *bases):
    """Make a class inheriting from ``bases`` whose metaclass inherits from
    all of ``metaclasses``.

    Like :func:`six.with_metaclass`, but allows multiple metaclasses.

    Parameters
    ----------
    metaclasses : iterable[type]
        A tuple of types to use as metaclasses.
    *bases : tuple[type]
        A tuple of types to use as bases.

    Returns
    -------
    base : type
        A subtype of ``bases`` whose metaclass is a subtype of ``metaclasses``.

    Notes
    -----
    The metaclasses must be written to support cooperative multiple
    inheritance. This means that they must delegate all calls to ``super()``
    instead of inlining their super class by name.
    """
    return six.with_metaclass(compose_types(*metaclasses), *bases) 
開發者ID:enigmampc,項目名稱:catalyst,代碼行數:27,代碼來源:metautils.py

示例8: test_with_metaclass_typing

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def test_with_metaclass_typing():
    try:
        import typing
    except ImportError:
        pytest.skip("typing module required")
    class Meta(type):
        pass
    if sys.version_info[:2] < (3, 7):
        # Generics with custom metaclasses were broken on older versions.
        class Meta(Meta, typing.GenericMeta):
            pass
    T = typing.TypeVar('T')
    class G(six.with_metaclass(Meta, typing.Generic[T])):
        pass
    class GA(six.with_metaclass(abc.ABCMeta, typing.Generic[T])):
        pass
    assert isinstance(G, Meta)
    assert isinstance(GA, abc.ABCMeta)
    assert G[int] is not G[G[int]]
    assert GA[int] is not GA[GA[int]]
    assert G.__bases__ == (typing.Generic,)
    assert G.__orig_bases__ == (typing.Generic[T],) 
開發者ID:benjaminp,項目名稱:six,代碼行數:24,代碼來源:test_six.py

示例9: test_with_metaclass_pep_560

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def test_with_metaclass_pep_560():
    class Meta(type):
        pass
    class A:
        pass
    class B:
        pass
    class Fake:
        def __mro_entries__(self, bases):
            return (A, B)
    fake = Fake()
    class G(six.with_metaclass(Meta, fake)):
        pass
    class GA(six.with_metaclass(abc.ABCMeta, fake)):
        pass
    assert isinstance(G, Meta)
    assert isinstance(GA, abc.ABCMeta)
    assert G.__bases__ == (A, B)
    assert G.__orig_bases__ == (fake,) 
開發者ID:benjaminp,項目名稱:six,代碼行數:21,代碼來源:test_six.py

示例10: test_with_metaclass_prepare

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def test_with_metaclass_prepare():
    """Test that with_metaclass causes Meta.__prepare__ to be called with the correct arguments."""

    class MyDict(dict):
        pass

    class Meta(type):

        @classmethod
        def __prepare__(cls, name, bases):
            namespace = MyDict(super().__prepare__(name, bases), cls=cls, bases=bases)
            namespace['namespace'] = namespace
            return namespace

    class Base(object):
        pass

    bases = (Base,)

    class X(six.with_metaclass(Meta, *bases)):
        pass

    assert getattr(X, 'cls', type) is Meta
    assert getattr(X, 'bases', ()) == bases
    assert isinstance(getattr(X, 'namespace', {}), MyDict) 
開發者ID:benjaminp,項目名稱:six,代碼行數:27,代碼來源:test_six.py

示例11: test_with_metaclass

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def test_with_metaclass():
    class Meta(type):
        pass
    class X(six.with_metaclass(Meta)):
        pass
    assert type(X) is Meta
    assert issubclass(X, object)
    class Base(object):
        pass
    class X(six.with_metaclass(Meta, Base)):
        pass
    assert type(X) is Meta
    assert issubclass(X, Base)
    class Base2(object):
        pass
    class X(six.with_metaclass(Meta, Base, Base2)):
        pass
    assert type(X) is Meta
    assert issubclass(X, Base)
    assert issubclass(X, Base2)
    assert X.__mro__ == (X, Base, Base2, object) 
開發者ID:jmankoff,項目名稱:data,代碼行數:23,代碼來源:test_six.py

示例12: testHandWrittenReflection

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def testHandWrittenReflection(self):
    # Hand written extensions are only supported by the pure-Python
    # implementation of the API.
    if api_implementation.Type() != 'python':
      return

    FieldDescriptor = descriptor.FieldDescriptor
    foo_field_descriptor = FieldDescriptor(
        name='foo_field', full_name='MyProto.foo_field',
        index=0, number=1, type=FieldDescriptor.TYPE_INT64,
        cpp_type=FieldDescriptor.CPPTYPE_INT64,
        label=FieldDescriptor.LABEL_OPTIONAL, default_value=0,
        containing_type=None, message_type=None, enum_type=None,
        is_extension=False, extension_scope=None,
        options=descriptor_pb2.FieldOptions())
    mydescriptor = descriptor.Descriptor(
        name='MyProto', full_name='MyProto', filename='ignored',
        containing_type=None, nested_types=[], enum_types=[],
        fields=[foo_field_descriptor], extensions=[],
        options=descriptor_pb2.MessageOptions())
    class MyProtoClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
      DESCRIPTOR = mydescriptor
    myproto_instance = MyProtoClass()
    self.assertEqual(0, myproto_instance.foo_field)
    self.assertTrue(not myproto_instance.HasField('foo_field'))
    myproto_instance.foo_field = 23
    self.assertEqual(23, myproto_instance.foo_field)
    self.assertTrue(myproto_instance.HasField('foo_field')) 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:30,代碼來源:reflection_test.py

示例13: test_item_meta_classcell_regression

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def test_item_meta_classcell_regression(self):
        class MyItem(six.with_metaclass(ItemMeta, Item)):
            def __init__(self, *args, **kwargs):
                # This call to super() trigger the __classcell__ propagation
                # requirement. When not done properly raises an error:
                # TypeError: __class__ set to <class '__main__.MyItem'>
                # defining 'MyItem' as <class '__main__.MyItem'>
                super(MyItem, self).__init__(*args, **kwargs) 
開發者ID:bomquote,項目名稱:transistor,代碼行數:10,代碼來源:test_item.py

示例14: filter_class

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def filter_class(self):
        return six.with_metaclass(self.filter_meta) 
開發者ID:IvanMalison,項目名稱:okcupyd,代碼行數:4,代碼來源:filter.py

示例15: test_staticmethod_decorate_all

# 需要導入模塊: import six [as 別名]
# 或者: from six import with_metaclass [as 別名]
def test_staticmethod_decorate_all():
    class Test(six.with_metaclass(util.decorate_all(staticmethod))):
        def test():
            return 1

        def test2(a, b):
            return a + b

    assert Test.test() == 1
    assert Test.test2(1, 1) == 2 
開發者ID:IvanMalison,項目名稱:okcupyd,代碼行數:12,代碼來源:util_test.py


注:本文中的six.with_metaclass方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。