xoutil.decorator
- Several decorators¶
This module contains several useful decorators, for several purposed. Also it severs as a namespace for other well-defined types of decorators.
Warning
This modules will be progressively deprecated during the 1.6 series.
We feel that either xoutil.objects
or xoutil.functools are a
better match for some of these decorators. But since we need to make sure
about keeping dependencies, the deprecation won’t be final until 1.7.0.
After 1.8.0, this modules will be finally removed.
Top-level decorators¶
-
class
xoutil.decorator.
AttributeAlias
(attr_name)[source]¶ Descriptor to create aliases for object attributes.
This descriptor is mainly to be used internally by
aliases()
decorator.
-
xoutil.decorator.
settle
(**kwargs)[source]¶ Returns a decorator that sets different attribute values to the decorated target (function or class).
Usage:
>>> @settle(name='Name') ... class Person(object): ... pass >>> Person.name 'Name'
-
xoutil.decorator.
namer
(name, **kwargs)[source]¶ Like
settle()
, but name is a positional argument and is assigned to the attribute__name__
.Usage:
>>> @namer('Identity', custom=1) ... class I(object): ... pass >>> I.__name__ 'Identity' >>> I.custom 1
-
xoutil.decorator.
aliases
(*names, **kwargs)[source]¶ In a class, create an
AttributeAlias
descriptor for each definition as keyword argument (alias=existing_attribute).If “names” are given, then the definition context is looked and are assigned to it the same decorator target with all new names:
>>> @aliases('foo', 'bar') ... def foobar(*args): ... 'This function is added to its module with two new names.'
-
xoutil.decorator.
assignment_operator
(func, maybe_inline=False)[source]¶ Makes a function that receives a name, and other args to get its first argument (the name) from an assigment operation, meaning that it if its used in a single assignment statement the name will be taken from the left part of the
=
operator.Warning
This function is dependant of CPython’s implementation of the language and won’t probably work on other implementations. Use only you don’t care about portability, but use sparingly (in case you change your mind about portability).
-
xoutil.decorator.
instantiate
(target, *args, **kwargs)[source]¶ Some singleton classes must be instantiated as part of its declaration because they represents singleton objects.
Every argument, positional or keyword, is passed as such when invoking the target. The following two code samples show two cases:
>>> @instantiate ... class Foobar(object): ... def __init__(self): ... print('Init...') Init... >>> @instantiate('test', context={'x': 1}) ... class Foobar(object): ... def __init__(self, name, context): ... print('Initializing a Foobar instance with name={name!r} ' ... 'and context={context!r}'.format(**locals())) Initializing a Foobar instance with name='test' and context={'x': 1}
In all cases, Foobar remains the class, not the instance:
>>> Foobar <class '...Foobar'>
-
class
xoutil.decorator.
memoized_property
(fget, doc=None)[source]¶ A read-only property that is only evaluated once.
This is extracted from the SQLAlchemy project’s codebase, merit and copyright goes to SQLAlchemy authors:
Copyright (C) 2005-2011 the SQLAlchemy authors and contributors This module is part of SQLAlchemy and is released under the MIT License: http://www.opensource.org/licenses/mit-license.php
-
class
xoutil.decorator.
memoized_instancemethod
(fget, doc=None)[source]¶ Decorate a method memoize its return value.
Best applied to no-arg methods: memoization is not sensitive to argument values, and will always return the same value even when called with different arguments.
This is extracted from the SQLAlchemy project’s codebase, merit and copyright goes to SQLAlchemy authors:
Copyright (C) 2005-2011 the SQLAlchemy authors and contributors This module is part of SQLAlchemy and is released under the MIT License: http://www.opensource.org/licenses/mit-license.php