Skip to main content

🎴 The decorator-decorator 🎴

Project description

🗃 dek - the decorator-decorator 🗃

dek decorates 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, like unittest.mock.patch does.

dek is a decorator for decorators that does this deftly with a single tiny function.

Example 1: a simple decorator with dek

TASK: write a decorator before that prints a function's name and its arguments before it executes.

With dek, 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 9

pfunc is a functools.partial, which represents the function call that dek intercepted. Your code can call pfunc as often as you like, or add or change parameters.

Example 2: same, without dek

import functools

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

    return wrapped

With dek it'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 parameter

Write a decorator before that prints a function's name, arguments and a label before it executes.

With dek, 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 8

Example 4: same, without dek

Without dek it'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 mode

For finer control over function signatures there is deferred mode, which lets you select what sort of signature you want to expose with a wrapped function that you create.

@dek(defer=True)
def before(func, label='debug'):
    def wrapped(foo, bar):
        print(label, foo, bar)
        return func(foo, bar)

    return wrapped

Example 6: Decorating a class

If you need to decorate methods on a class, there's a methods parameter 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 article talks more about decorators that take parameters and about dek in general.

For your advanced decorator desires, the PyPi module decorator does not duplicate duties that dek does, but does pretty anything else you could conceive of in a decorator library.

API Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dek-1.4.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

dek-1.4.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file dek-1.4.2.tar.gz.

File metadata

  • Download URL: dek-1.4.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.11 Darwin/21.6.0

File hashes

Hashes for dek-1.4.2.tar.gz
Algorithm Hash digest
SHA256 9c1fe968538d773e2f13bd27241941b6af73b70a898afb0e06d24054b9f4fad9
MD5 995d3eb046fcc819953fcb453526c317
BLAKE2b-256 67a2185c123cc86bbe2f70c15e7624bc4e3a89b1c1ceff2836316b3aab89fbb9

See more details on using hashes here.

File details

Details for the file dek-1.4.2-py3-none-any.whl.

File metadata

  • Download URL: dek-1.4.2-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.11 Darwin/21.6.0

File hashes

Hashes for dek-1.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4324b9fc90d9d5f6e8957dd208c09f95d23cbfc7d488db0d1eb86d830e055f50
MD5 2fdc3ea7992fc69b969cf404b595fb0e
BLAKE2b-256 65e761891051cf9abdbdcdb89aaeb910372f646136643537e4a211bf74a4ec01

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page