Package logilab :: Package common :: Module decorators

[frames] | no frames]

# Module decorators

source code

    A few useful function/method decorators.

Classes
| cached_decorator
| cachedproperty Provides a cached property equivalent to the stacking of @cached and @property, but more efficient.
| wproperty Simple descriptor expecting to take a modifier function as first argument and looking for a _<function name> to retrieve the attribute.
| classproperty this is a simple property-like class but for class attributes.
| iclassmethod Descriptor for method which should be available as class method if called on the class or instance method if called on an instance.

Functions

cached(callableobj=None, keyarg=None, **kwargs) Simple decorator to cache result of method call. | source code

get_cache_impl(obj, funcname) | source code

clear_cache(obj, funcname) Clear a cache handled by the :func:`cached` decorator. | source code

copy_cache(obj, funcname, cacheobj) Copy cache for <funcname> from cacheobj to obj. | source code

timed(f) | source code

locked(acquire, release) Decorator taking two methods to acquire/release a lock as argument, returning a decorator function which will call the inner method after having called acquire(self) et will call release(self) afterwards. | source code

monkeypatch(klass, methodname=None) Decorator extending class with the decorated callable. | source code

Function Details

### clear_cache(obj, funcname)

source code

    Clear a cache handled by the :func:`cached` decorator. If 'x' class has
    @cached on its method `foo`, type

    >>> clear_cache(x, 'foo')

    to purge this method's cache on the instance.

### monkeypatch(klass, methodname=None)

source code

    Decorator extending class with the decorated callable. This is basically
    a syntactic sugar vs class assignment.

    >>> class A:
    ...     pass
    >>> @monkeypatch(A)
    ... def meth(self):
    ...     return 12
    ...
    >>> a = A()
    >>> a.meth()
    12
    >>> @monkeypatch(A, 'foo')
    ... def meth(self):
    ...     return 12
    ...
    >>> a.foo()
    12