Skip to main content

decoutils: Utilities for writing decorators

Build Status Documentation Status Coverage Status

Rationale

Python decorators are very useful. However, writing decorators with arguments is not straightforward. Take the following function as an example:

register_plugin(plugin, arg1=1):
    print('registering ', plugin.__name__, ' with arg1=', arg1)

This function registers a input function somewhere in the system. If this function could work as a decorator, that will be convinient. In order to make it a decorator, we just need it to return the input plugin trivially:

register_plugin(plugin, arg1=1):
    print('registering ', plugin.__name__, ' with arg1=', arg1)
    return plugin

@register_plugin
def plugin1(): pass

It is pretty easy so far. What if we want register_plugin works as a decorator with arguments? That's to say, we want to use it as:

@register_plugin(arg1=2)
def plugin2(): pass

In order to accomplish this goal, we need to wrap the function register_plugin so that it return a decorated plugin if the first input is a callable object, otherwise return a decorator with arguments. The decoutils.decorator_with_args is intend to abstract that wrapping, so that we can reuse it.

Installation

Install from PyPI

pip install decoutils

Install from Anaconda

conda install -c liyugong decoutils

Simple example

Basically, decorator_with_args enables a ordinary decorator function to be a decorator with arguments.

@decorator_with_args
def register_plugin(plugin, arg1=1):
    print('registering ', plugin.__name__, ' with arg1=', arg1)
    return plugin

@register_plugin(arg1=10)
def plugin1(): pass

Moreover, decorator_with_args itself is also a decorator with arguments: one argument return_original which can be used to convert a non-decorator function to be a decorator

@decorator_with_args
def register_plugin(plugin, arg1=1):
    print('registering ', plugin.__name__, ' with arg1=', arg1)
    # Note here the function does not return the plugin, so it cannot work as a decorator originally

@register_plugin(arg1=10)
def plugin1(): pass

decorator_with_args can also convert a function to decorator whose decorating target is not its first argument, e.g.

decorator_with_args(target_pos=1)
def register_plugin(arg1, plugin):
    return plugin

@register_plugin(100) # plugin2 will be registered with arg1=100
def plugin2(): pass

return_original control whether the resultant decorator return the original plugin, or the result of function register_plugin.

License

The decoutils package is released under the MIT License

Documentation

https://decoutils.readthedocs.io

Metadata

Release files for decoutils 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for decoutils 0.0.3
File Size Uploaded
decoutils-0.0.3.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for decoutils 0.0.3
File Interpreter ABI Platform
decoutils-0.0.3-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 7.7 kB

Release files / decoutils-0.0.3.tar.gz

Download URL decoutils-0.0.3.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
680d2f21832e79f64044e84e28732dd96b3fafe8727ca248ab27a776786dd7d5
BLAKE2b-256 checksum
How to use checksums
fbca757c4de1ed8f37abacbc59c0c66c07d9aed0bf70a8b67edaefce7fc50b66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Release files / decoutils-0.0.3-py2.py3-none-any.whl

Download URL decoutils-0.0.3-py2.py3-none-any.whl
Size 3.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
bf2d52a1bf2753870c668370c7c96d790323c51b3e526fd638dfb80e0fd7cffd
BLAKE2b-256 checksum
How to use checksums
565333601de73b75f1f9d72df460c0d9b75ad92604a61afaf66ad89efe6430b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page