Miscellaneous, useful decorators.
This decorator can be used to turn simple functions into well-behaved decorators, so long as the decorators are fairly simple. If a decorator expects a function and returns a function (no descriptors), and if it doesn’t modify function attributes or docstring, then it is eligible to use this. Simply apply @simple_decorator to your decorator and it will automatically preserve the docstring and function attributes of functions to which it is applied.
Augments a class’s method with new decorators or documentation.
This is useful when a class needs to add new decorators or new documentation to a parent class’s method, without changing the behavior or burying the existing decorators.
The methods using this decorator can provide code to run at the end of the parent function. Usually, though, it will just have an empty body of
- blocktag(*args, **kwargs)¶
Creates a block template tag with beginning/end tags.
This does all the hard work of creating a template tag that can parse the arguments passed in and then parse all nodes between a beginning and end tag (such as myblock/endmyblock).
By default, the end tag is prefixed with “end”, but that can be changed by passing
end_prefix="end_"or similar to @blocktag.
blocktag will call the wrapped function with context and nodelist parameters, as well as any parameters passed to the tag. It will also ensure that a proper error is raised if too many or too few parameters are passed.
unicode, optional) – The prefix for the end tag. This defaults to
'end', but template tags using underscores in the name might want to change this to
bool, optional) – Whether to automatically resolve all variables provided to the template tag. By default, variables are resolved. Template tags can turn this off if they want to handle variable parsing manually.
The resulting template tag function.
- Return type:
@register.tag @blocktag def divify(context, nodelist, div_id=None): s = "<div" if div_id: s += " id='%s'" % div_id return s + ">" + nodelist.render(context) + "</div>"
- class cached_property(func)¶
Decorator for creating a read-only property that caches a value.
This is a drop-in replacement for Django’s
cached_propertythat retains the docstring and attributes of the original method.
While Django 1.8+ does retain the docstring, it does not retain the attributes.
Initialize the property.
callable) – The function that will be called when this property is accessed. The property will have its name, documentation, and other attributes.
- optional_decorator(decorator, predicate)¶
Optionally apply a decorator given a predicate function.
callable) – The decorator to conditionally apply.
callable) – The predicate used to determine when
decoratorshould be used. The arguments provided to the decorated function will be passed to this function.
A decorator that, when applied to a function, will call the function decorated with
True. Otherwise it will call the original function.
- Return type:
from djblets.util.decorators import (optional_decorator, simple_decorator) def predicate(verbose) return verbose @simple_decorator def decorator(f): def decorated(verbose): print('Hello from the decorator') return f(verbose) @optional_decorator(decorator, predicate) def f(x): print('Hello from f()') # Prints "Hello from the decorator" and "Hello from f()" f(verbose=True) # Prints "Hello from f()" f(verbose=False)