dek: the decorator-decorator
Project description
dek decorates your decorators to diminish defects and drudgery.
Writing a decorator with parameters needs three levels of function and several opportunities for error, which dek deletes.
EXAMPLE:
Write a decorator print_before that prints a function’s arguments with a label when it executes
# Without dek all is sadness
import functools
def print_before(label='label'):
def deferred(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
print(label, args, kwargs)
return func(*args, **kwargs)
return wrapped
if callable(label):
return deferred(label)
return deferred
# Things go better with dek
from dek import dek
@dek
def print_before(func, args, kwargs, label='debug'):
print(label, args, kwargs)
return func(*args, **kargs)
# For finer control, enjoy ``dek.dek2``
from dek import dek2
@dek2
def print_before(func, label='debug'):
def wrapped(foo, bar):
print(label, foo, bar)
return func(foo, bar)
return wrapped
NOTES:
All these forms are supported:
@print_before
@print_before()
@print_before('debug')
@print_before(label='debug')
@print_before('debug', verbose=True)
This article talks more about decorators that take parameters.
For advanced problems, the PyPi library decorator does not do what dek does, but does pretty anything else you could conceive of in a decorator library.
API
dek.dek(decorator)
Implement a decorator with parameters, from a simple function
The function decorator has signature decorator(func, args, kwargs, ...) where:
func is the function being wrapped
args are the positional arguments to func
kwargs are the keyword arguments to func
and the remaining arguments (…), positional or keyword, can be anything you need to implement the decorator.
EXAMPLE:
@dek
def print_before(func, args, kwargs, my_label='debug'):
print(my_label, args, kwargs)
return func(*args, **kargs)
dek.dek2(decorator)
Implement a decorator with parameters, from a function that returns a function.
The top-level function decorator has signature decorator(func, ...) where func is the function being wrapped, and it returns the wrapper function, that has any signature needed.
EXAMPLE:
@dek2
def print_before(func, label='label'):
def wrapper(foo, bar):
if verbose:
print(label, foo, bar)
return func(foo, bar)
return wrapper
(automatically generated by doks on 2020-07-05T21:40:20.301083)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.