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

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

        def _weave_around_method(cls, aspect, method_name,
                          around_method_name='_around_{method_name}'):
    '''Simply put, replaces a method by another::

        >>> class OriginalClass(object):
        ...    def echo(self, what):
        ...        return what
        ...
        ...    def superecho(self, what):
        ...        return what * what

        >>> class Logger(object):
        ...    def _around_any(self, method, *args, **kwargs):
        ...        print('Calling {m} with {args}, {kwargs}'.
        ...              format(m=method, args=args, kwargs=kwargs))
        ...        result = method(*args, **kwargs)
        ...        print('... and {result!r} was returned'.
        ...              format(result=result))
        ...        return self.superecho(result)

        >>> obj = OriginalClass()
        >>> obj.echo(10)
        10

        >>> _weave_around_method(OriginalClass, Logger, 'echo', '_around_any')

        >>> obj.echo(10)            # doctest: +ELLIPSIS
        Calling ... with (10,), {}
        ... and 10 was returned
        100

    '''
    method = _mro_getattr(cls, method_name, None)
    around_method_name = around_method_name.format(method_name=method_name)
    around_method = get_staticattr(aspect, around_method_name, None)

    @wraps(method)
    def inner(*args, **kwargs):
        self, bound_method, args, kwargs = bind_method(method, *args, **kwargs)
        result = around_method(self, bound_method, *args, **kwargs)
        return result

    if method and around_method:
        wrapped = build_method(method, inner)
        setattr(cls, method_name, wrapped)
        


src/x/o/xotl.ql-0.2.1dev/tests/test_translate.py   xotl.ql(Download)
    import sys
    from xoutil.compat import iterkeys_
    from xoutil.aop.classical import weave, _weave_around_method
 
    from xotl.ql.tests import logging_aspect
    weave(aspect, QueryParticlesBubble)
    for attr in iterkeys_(_part_operations):
        _weave_around_method(QueryPart, aspect, attr, '_around_')
    _weave_around_method(QueryPart, aspect, '__getattribute__', '_around_')