Metadata-Version: 2.1
Name: dek
Version: 0.10.2
Summary: dek: the decorator-decorator
Home-page: https://github.com/rec/dek
Author: Tom Ritchford
Author-email: tom@swirly.com
License: MIT
Keywords: testing,modules
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Utilities

🗃 dek - the decorator-decorator 🗃
======================================================

``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

.. code-block:: python

    # 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, label='debug'):
        print(label, func.args, func.keywords)
        return func()


    # 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:

1. ``@print_before``
2. ``@print_before()``
3. ``@print_before('debug')``
4. ``@print_before(label='debug')``
5. ``@print_before('debug', verbose=True)``

`This article <https://medium.com/better-programming/how-to-write-python-decorators-that-take-parameters-b5a07d7fe393>`_ talks more about
decorators that take parameters.

For advanced problems, the PyPi library
`decorator <https://github.com/micheles/decorator/blob/master/docs/documentation.md>`_ does not do what ``dek`` does, but does pretty anything
else you could conceive of in a decorator library.

API
---

``dek.dek(decorator)``
~~~~~~~~~~~~~~~~~~~~~~

(`dek.py, 82-100 <https://github.com/rec/dek/blob/master/dek.py#L82-L100>`_)

Implement a decorator with parameters, from a simple function

The function ``decorator`` has signature ``decorator(func, ...)``
where ``func`` is a ``functools.partial`` of the call that is
being handled, and the remaining

EXAMPLE:

.. code-block:: python

    @dek
    def print_before(func, label='debug'):
        print(label, func.args, func.keywords)
        return func()

``dek.dek2(decorator)``
~~~~~~~~~~~~~~~~~~~~~~~

(`dek.py, 102-125 <https://github.com/rec/dek/blob/master/dek.py#L102-L125>`_)

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. The wrapper function
that's returned can have any signature needed.

EXAMPLE:

.. code-block:: python

    @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 <https://github.com/rec/doks/>`_ on 2020-07-05T22:12:44.467941)


