本文整理汇总了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])
示例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
示例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)
示例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
示例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')
示例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
示例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)
示例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],)
示例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,)
示例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)
示例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)
示例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'))
示例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)
示例14: filter_class
# 需要导入模块: import six [as 别名]
# 或者: from six import with_metaclass [as 别名]
def filter_class(self):
return six.with_metaclass(self.filter_meta)
示例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