Python six 模块,add_metaclass() 实例源码

我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用six.add_metaclass()

项目:TCP-IP    作者:JackZ0    | 项目源码 | 文件源码
def setUp(self):
        from acme.jose.json_util import Field
        from acme.jose.json_util import JSONObjectWithFieldsMeta
        self.field = Field('Baz')
        self.field2 = Field('Baz2')
        # pylint: disable=invalid-name,missing-docstring,too-few-public-methods
        # pylint: disable=blacklisted-name

        @six.add_metaclass(JSONObjectWithFieldsMeta)
        class A(object):
            __slots__ = ('bar',)
            baz = self.field

        class B(A):
            pass

        class C(A):
            baz = self.field2

        self.a_cls = A
        self.b_cls = B
        self.c_cls = C
项目:certbot    作者:nikoloskii    | 项目源码 | 文件源码
def setUp(self):
        from acme.jose.json_util import Field
        from acme.jose.json_util import JSONObjectWithFieldsMeta
        self.field = Field('Baz')
        self.field2 = Field('Baz2')
        # pylint: disable=invalid-name,missing-docstring,too-few-public-methods
        # pylint: disable=blacklisted-name

        @six.add_metaclass(JSONObjectWithFieldsMeta)
        class A(object):
            __slots__ = ('bar',)
            baz = self.field

        class B(A):
            pass

        class C(A):
            baz = self.field2

        self.a_cls = A
        self.b_cls = B
        self.c_cls = C
项目:logfury    作者:ppolewicz    | 项目源码 | 文件源码
def test_disable_trace(self):
        @six.add_metaclass(TraceAllPublicCallsMeta)
        class Ala(object):
            @disable_trace
            def bar(self, a, b, c=None):
                return True

            def __repr__(self):
                return '<%s object>' % (self.__class__.__name__,)

        class Bela(Ala):
            def bar(self, a, b, c=None):
                return False

        with LogCapture() as l:
            a = Ala()
            a.bar(1, 2, 3)
            a.bar(1, b=2)
            b = Bela()
            b.bar(1, 2, 3)
            b.bar(1, b=2)
            l.check()
项目:immobilus    作者:pokidovea    | 项目源码 | 文件源码
def _decorate_class(self, cls):
        class _Meta(type):
            def __new__(cls, name, bases, attrs):
                for attr_name, attr in attrs.items():
                    if callable(attr):
                        attrs[attr_name] = self(attr)

                return super(_Meta, cls).__new__(cls, name, bases, attrs)

        return six.add_metaclass(_Meta)(cls)
项目:ion-python    作者:amzn    | 项目源码 | 文件源码
def record(*fields):
    """Constructs a type that can be extended to create immutable, value types.

    Examples:
        A typical declaration looks like::

            class MyRecord(record('a', ('b', 1))):
                pass

        The above would make a sub-class of ``collections.namedtuple`` that was named ``MyRecord`` with
        a constructor that had the ``b`` field set to 1 by default.

    Note:
        This uses meta-class machinery to rewrite the inheritance hierarchy.
        This is done in order to make sure that the underlying ``namedtuple`` instance is
        bound to the right type name and to make sure that the synthetic class that is generated
        to enable this machinery is not enabled for sub-classes of a user's record class.

    Args:
        fields (list[str | (str, any)]): A sequence of str or pairs that
    """
    @six.add_metaclass(_RecordMetaClass)
    class RecordType(object):
        _record_sentinel = True
        _record_fields = fields

    return RecordType
项目:logfury    作者:ppolewicz    | 项目源码 | 文件源码
def test_subclass(self):
        @six.add_metaclass(AbstractTracePublicCallsMeta)
        class Supp(object):
            @abstractmethod
            def a(self):
                pass

            def __repr__(self):
                return '<%s object>' % (self.__class__.__name__,)

        class Ala(Supp):
            def a(self):
                pass

            def bar(self, a, b, c=None):
                return True

        a = Ala()
        a.bar(1, 2, 3)
        a.bar(1, b=2)

        with LogCapture() as l:
            a = Ala()
            a.bar(1, 2, 3)
            a.bar(1, b=2)
            l.check(
                (__name__, 'DEBUG', 'calling %sbar(self=<Ala object>, a=1, b=2, c=3)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar(self=<Ala object>, a=1, b=2, c=None)' % (self._get_prefix(),)),
            )

        class Bela(Supp):
            # did not define a()
            def bar(self, a, b, c=None):
                return True

        with self.assertRaises(TypeError):
            Bela()
项目:logfury    作者:ppolewicz    | 项目源码 | 文件源码
def test_subclass(self):
        @six.add_metaclass(DefaultTraceAbstractMeta)
        class Supp(object):
            @abstractmethod
            def a(self):
                pass

            def __repr__(self):
                return '<%s object>' % (self.__class__.__name__,)

        class Ala(Supp):
            def a(self):
                pass

            def bar(self, a, b, c=None):
                return True

        a = Ala()
        a.bar(1, 2, 3)
        a.bar(1, b=2)

        with LogCapture() as l:
            a = Ala()
            a.bar(1, 2, 3)
            a.bar(1, b=2)
            l.check(
                (__name__, 'DEBUG', 'calling %sbar(self=<Ala object>, a=1, b=2, c=3)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar(self=<Ala object>, a=1, b=2, c=None)' % (self._get_prefix(),)),
            )

        class Bela(Supp):
            # did not define a()
            def bar(self, a, b, c=None):
                return True

        with self.assertRaises(TypeError):
            Bela()
项目:logfury    作者:ppolewicz    | 项目源码 | 文件源码
def test_subclass(self):
        @six.add_metaclass(TraceAllPublicCallsMeta)
        class Ala(object):
            def bar(self, a, b, c=None):
                return True

            def __repr__(self):
                return '<%s object>' % (self.__class__.__name__,)

        class Bela(Ala):
            def bar(self, a, b, c=None):
                return False

        with LogCapture() as l:
            a = Ala()
            a.bar(1, 2, 3)
            a.bar(1, b=2)
            l.check(
                (__name__, 'DEBUG', 'calling %sbar(self=<Ala object>, a=1, b=2, c=3)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar(self=<Ala object>, a=1, b=2, c=None)' % (self._get_prefix(),)),
            )

        with LogCapture() as l:
            b = Bela()
            b.bar(1, 2, 3)
            b.bar(1, b=2)

            l.check(
                (__name__, 'DEBUG', 'calling %sbar(self=<Bela object>, a=1, b=2, c=3)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar(self=<Bela object>, a=1, b=2, c=None)' % (self._get_prefix(),)),
            )
项目:logfury    作者:ppolewicz    | 项目源码 | 文件源码
def test_empty_only(self):
        @six.add_metaclass(TraceAllPublicCallsMeta)
        class Ala(object):
            @limit_trace_arguments(only=tuple())
            def bar(self, a, b, c=None):
                return True

            def __repr__(self):
                return '<%s object>' % (self.__class__.__name__,)

        class Bela(Ala):
            def bar(self, a, b, c=None):
                return False

        with LogCapture() as l:
            a = Ala()
            a.bar(1, 2, 3)
            a.bar(1, b=2)
            b = Bela()
            b.bar(1, 2, 3)
            b.bar(1, b=2)
            l.check(
                (__name__, 'DEBUG', 'calling %sbar() (hidden args: self, a, b, c)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar() (hidden args: self, a, b, c)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar() (hidden args: self, a, b, c)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar() (hidden args: self, a, b, c)' % (self._get_prefix(),)),
            )
项目:logfury    作者:ppolewicz    | 项目源码 | 文件源码
def test_only(self):
        @six.add_metaclass(TraceAllPublicCallsMeta)
        class Ala(object):
            #@limit_trace_arguments(only=['self', 'a', 'b'], skip=['a'])
            @limit_trace_arguments(only=['a'])
            def bar(self, a, b, c=None):
                return True

            def __repr__(self):
                return '<%s object>' % (self.__class__.__name__,)

        class Bela(Ala):
            def bar(self, a, b, c=None):
                return False

        with LogCapture() as l:
            a = Ala()
            a.bar(1, 2, 3)
            a.bar(1, b=2)
            b = Bela()
            b.bar(1, 2, 3)
            b.bar(1, b=2)
            l.check(
                (__name__, 'DEBUG', 'calling %sbar(a=1) (hidden args: self, b, c)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar(a=1) (hidden args: self, b, c)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar(a=1) (hidden args: self, b, c)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar(a=1) (hidden args: self, b, c)' % (self._get_prefix(),)),
            )
项目:logfury    作者:ppolewicz    | 项目源码 | 文件源码
def test_skip_and_only(self):
        @six.add_metaclass(TraceAllPublicCallsMeta)
        class Ala(object):
            @limit_trace_arguments(only=['self', 'a', 'b'], skip=['a'])
            def bar(self, a, b, c=None):
                return True

            def __repr__(self):
                return '<%s object>' % (self.__class__.__name__,)

        class Bela(Ala):
            def bar(self, a, b, c=None):
                return False

        with LogCapture() as l:
            a = Ala()
            a.bar(1, 2, 3)
            a.bar(1, b=2)
            b = Bela()
            b.bar(1, 2, 3)
            b.bar(1, b=2)
            l.check(
                (__name__, 'DEBUG', 'calling %sbar(self=<Ala object>, b=2) (hidden args: a, c)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar(self=<Ala object>, b=2) (hidden args: a, c)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar(self=<Bela object>, b=2) (hidden args: a, c)' % (self._get_prefix(),)),
                (__name__, 'DEBUG', 'calling %sbar(self=<Bela object>, b=2) (hidden args: a, c)' % (self._get_prefix(),)),
            )
项目:pure_interface    作者:aranzgeo    | 项目源码 | 文件源码
def test_concrete_abc_detection(self):
        @six.add_metaclass(abc.ABCMeta)
        class B(object):
            def __init__(self):
                self.foo = 'bar'

        class Concrete(B, pure_interface.PureInterface):
            pass

        self.assertFalse(Concrete._pi.type_is_pure_interface)
        try:
            c = Concrete()
        except Exception as exc:
            self.fail('Instantiation failed {}'.format(exc))
项目:pure_interface    作者:aranzgeo    | 项目源码 | 文件源码
def test_interface_abc_detection(self):
        @six.add_metaclass(abc.ABCMeta)
        class IABC(object):

            @abc.abstractmethod
            def foo(self):
                pass

            @abc.abstractproperty
            def bar(self):
                return None

        class EmptyABCPI(IABC, pure_interface.PureInterface):
            pass

        class PIEmptyABC(pure_interface.PureInterface, IABC):
            pass

        self.assertTrue(EmptyABCPI._pi.type_is_pure_interface)
        self.assertTrue(PIEmptyABC._pi.type_is_pure_interface)
        if six.PY3:
            self.assertTrue('foo' in EmptyABCPI._pi.interface_method_names)
            self.assertTrue('bar' in EmptyABCPI._pi.interface_property_names)
        self.assertTrue('foo' in PIEmptyABC._pi.interface_method_names)
        self.assertTrue('bar' in PIEmptyABC._pi.interface_property_names)
        with self.assertRaises(TypeError):
            EmptyABCPI()
        with self.assertRaises(TypeError):
            PIEmptyABC()
项目:gelato    作者:ferrine    | 项目源码 | 文件源码
def bayes(layercls, stack=1):
    try:
        issubcls = issubclass(layercls, lasagne.layers.base.Layer)
    except TypeError:
        raise TypeError('{} needs to be a Layer subclass'
                        .format(layercls))
    if issubcls:
        if type(layercls) is LayerModelMeta:
            raise TypeError('{} is already bayesian'
                            .format(layercls))
        else:
            @six.add_metaclass(LayerModelMeta)
            class BayesianAnalog(layercls, pm.Model):
                pass
            frm = inspect.stack()[stack]
            mod = inspect.getmodule(frm[0])
            if mod is None:
                modname = '__main__'
            else:
                modname = mod.__name__
            BayesianAnalog.__module__ = modname
            BayesianAnalog.__doc__ = layercls.__doc__
            BayesianAnalog.__name__ = layercls.__name__
            return BayesianAnalog
    else:
        raise TypeError('{} needs to be a Layer subclass'
                        .format(layercls))
项目:oanda-api-v20    作者:hootnot    | 项目源码 | 文件源码
def test__abstractclass(self):

        @six.add_metaclass(ABCMeta)
        class Something(object):

            @abstractmethod
            def __init__(self, x=10):
                self.x = x

        @abstractclass
        class SomethingElse(Something):
            # derived classes from this class make use
            # of this class __init__
            # since this __init__ overrides the parent's
            # @abstractmethod instances could be created,
            # by making the class abstract with @abstractclass
            # this can't be done, derived classes can
            # ... that is the goal

            def __init__(self, x=10, y=20):
                super(SomethingElse, self).__init__(x)
                self.y = y

        class ABCDerived(SomethingElse):
            pass

        with self.assertRaises(TypeError):
            Something(x=20)
        with self.assertRaises(TypeError):
            SomethingElse(x=20, y=30)

        x = 20
        y = 30
        abcDerived = ABCDerived(x, y)
        self.assertEqual(abcDerived.x + abcDerived.y, x+y)