Package: dek
🗃dek- the decorator-decorator 🗃dekdecorates your decorators to diminish defects and drudgery.Writing a Python decorator which takes no parameters isn't hard.But writing a decorator with parameters is less easy - and more work
if you want to decorate classes, likeunittest.mock.patchdoes.dekis a decorator for decorators that does this deftly with a
single tiny function.Example 1: a simple decorator with dekTASK: write a decoratorbeforethat prints a function's name and its
arguments before it executes.Withdek, it's a few lines:import dek

@dek
def before(pfunc):
    print(pfunc)
    return pfunc()Done! To use your new decorator:@before
def phone(two, four=4):
    print('Calling', two + two, four * four)

one(32, four=3)

# That prints something like:
#
# functools.partial(<function phone at 0x7fafa8072b00>, 32, four=3)
# Calling 64 9pfuncis afunctools.partial,
which represents the function call thatdekintercepted.  Your code
can callpfuncas often as you like, or add or change parameters.Example 2: same, withoutdekimport functools

def before(func):
    @functools.wraps(func)
    def wrapped(*args, **kwargs):
        print(func, args, kwargs)
        return func(*args, **kwargs)

    return wrappedWithdekit's a bit less work, but the real advantage comes when you have
a decorator with a parameter.Example 3: a decorator with a single optional parameterWrite a decoratorbeforethat prints a function's name, argumentsand a labelbefore it executes.Withdek, it's a trivial change from the previous solution.import dek

@dek
def before(pfunc, label='dull'):
    print(label, pfunc.func, *pfunc.args)
    return pfunc()

@before
def add(x, y):
    return x + y

@before(label='Exciting!')
def times(x, y):
    return x * y

print('Result', add(2, times(2, 3)))

# Prints:
#   Exciting! times 2 3
#   dull add 2 6
#   Result 8Example 4: same, withoutdekWithoutdekit's actual work that's easy to get wrong.import functools

def before(func=None, label='dull'):
    if func is not None:
        @functools.wraps(func)
        def wrapped(*args, **kwargs):
            print(label, func.__name, *args)
            return func(*args, **kwargs)

        return wrapped

    return functools.partial(before, label=label)Example 5: Deferred modeFor finer control over function signatures there is deferred mode, which
lets you select what sort of signature you want to expose with awrappedfunction that you create.@dek(defer=True)
def before(func, label='debug'):
    def wrapped(foo, bar):
        print(label, foo, bar)
        return func(foo, bar)

    return wrappedExample 6: Decorating a classIf you need to decorate methods on a class, there's amethodsparameter to
select which methods get decorated.import dek

@dek(methods='test')
def before(pfunc):
    print('HERE', *pfunc.args)
    return pfunc()

@before
class Class:
    def test_one(self):
        return 1

    def test_two(self):
        return 2

    def three(self):  # This won't get decorated
        return 1


# Test at the command line:
>>> cl = Class()
>>> cl.test_one(), cl.test_two(), cl.three()
HERE 1
HERE 2
(1, 2, 3)NOTES:This articletalks more about
decorators that take parameters and aboutdekin general.For your advanced decorator desires, the PyPi moduledecoratordoes not duplicate duties thatdekdoes, but does
pretty anything else you could conceive of in a decorator library.API Documentation