Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(2)  |  Call(0)  |  Derive(0)  |  Import(2)

        def proxify(cls, *complementors):
    '''A decorator to proxify classes with :class:`Proxy`.

    Usage::

        >>> class Foobar(object):
        ...    def hi(self):
        ...        print('Hello from %r' % self)

        >>> class HackedHi(object):
        ...    def hi(self):
        ...        print('Hacked %r' % self)
        ...        return self

        >>> class Addition(object):
        ...    def __add__(self, other):
        ...        print("I'm adding %s" % type(self))
        ...        return self
        ...    __radd__ = __add__

        >>> @proxify
        ... class Proxified(object):
        ...    behaves = [HackedHi, Addition]
        ...    def __init__(self, target):
        ...        self.target = target

        >>> x = Foobar()
        >>> y = Proxified(x)
        >>> r = y.hi()  # doctest: +ELLIPSIS
        Hacked <...>

        >>> r + 1 is 1 + r  # doctest: +ELLIPSIS
        I'm adding 
        I'm adding 
        True

    But notice that if neither the proxied object or it's behaviors implement a
    method, a exception is raised::

        >>> r < 1                                # doctest: +SKIP
        Traceback (most recent call last):
            ...
        AttributeError: ...

    .. note::

       In Python 3 it will be a TypeError.

    The only exceptions for the above rule are `__eq__` and `__ne__`
    operations, for which we provide a fallback implementation if none is
    provided.

    Notice that behaviors classes must not assume that `self` is the proxy
    object instead of the "bare" object itself. That's needed for the illusion
    of "added" behaviors to be consistent.  If we make `self` the bare object
    then all the added behavior we'll be lost inside the method call.

    If you need to access the bare object directly use the attribute `target`
    of the proxy object (i.e: ``self.target``); we treat that attribute
    specially.

    To the same accord, the fallback implementations of `__eq__` and `__ne__`
    also work at the proxy level. So if we do::

        >>> class UnproxifingAddition(object):
        ...    def __add__(self, other):
        ...        return self.target

        >>> @proxify
        ... class Proxified(object):
        ...    behaves = [UnproxifingAddition]
        ...    def __init__(self, target):
        ...        self.target = target

    Now the addition would remove the proxy and the equality test
    will fail::

        >>> x = Foobar()
        >>> y = Proxified(x)
        >>> y is (y + 1)
        False

    .. warning::

       But be warned! If the proxied object has an attribute `target` it will
       shadow the proxy::

            >>> x.target = 'oops'
            >>> y.target == 'oops'
            True

       If you need to access any attribute of the proxy and not the proxied
       object without fear of being shadowed, use the
       :class:`UNPROXIFING_CONTEXT` context like this::

            >>> from xoutil.context import context
            >>> with context(UNPROXIFING_CONTEXT):
            ...     y.target is x
            True

            >>> y.target is x
            False

    '''
    if not complementors:
        complementors = (SupportedOperations,)
    ComplementedProxy = complementor(*complementors)(Proxy)
    return complementor(ComplementedProxy)(cls)
        


src/x/o/xotl.ql-0.2.1dev/xotl/ql/expressions.py   xotl.ql(Download)
from xoutil.context import context
from xoutil.aop import complementor
from xoutil.proxy import proxify, UNPROXIFING_CONTEXT, unboxed
from xoutil.decorator.compat import metaclass
from xoutil.compat import py3k as _py3k

src/x/o/xoutil-1.5.4/tests/test_proxy.py   xoutil(Download)
 
from xoutil.context import context
from xoutil.proxy import proxify, UNPROXIFING_CONTEXT, unboxed
 
__docstring_format__ = 'rst'