Skip to main content

decfunc

License: MIT

Creating decorators with arguments made easy.

Creating decorators in Python is easy, unless you want to use arguments. The aim of this library to abstract away some code that makes argument-ed decorators work.

Documentation

This library only contains one class: wrapper. The mutate method of this class is responsible for your business logic. You can simply use a class inheriting from wrapper as the decorator. For example:

from decfunc import wrapper


class square_root(wrapper):
    def mutate(self, wrapped, *args, **kwargs):
        return wrapped(*args, **kwargs) ** (1 / 2)


@square_root
def get_number(*args, **kwargs):
    return 4


get_number()  # 2.0

In this example, decorated function's return value is mutated so that its square root is returned instead. As you can see mutate method takes a wrapped, which is the decorated class or function, and *args, **kwargs, which correspond to given args and kwargs of the decorated function/class (you may also use the signature of the decorated callable).

Now lets change this a bit so that we can give pass an argument named n, which will denote to nth root, instead of square root.

from decfunc import wrapper


class nth_root(wrapper):
    def __init__(self, n=2):
        self.n = n

    def mutate(self, wrapped, *args, **kwargs):
        return wrapped(*args, **kwargs) ** (1 / self.n)


@nth_root(n=3)
def get_number(*args, **kwargs):
    return 27


@nth_root
def get_another_number(*args, **kwargs):
    return 16


get_number()  # 3.0
get_another_number()  # 4.0

As you can see, in order to add arguments to the decorator you can use __init__, whose signature will be the signature of the decorator.

Release files for decfunc 1.0.0

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

Source distribution (sdist)

Source distribution for decfunc 1.0.0
File Size Uploaded
decfunc-1.0.0.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for decfunc 1.0.0
File Interpreter ABI Platform
decfunc-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.3 kB

Release files / decfunc-1.0.0.tar.gz

Download URL decfunc-1.0.0.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2245d3c29cfa7756059dbfd2a20247a91eb9c5b1155eac715fe665ad63b3fa8d
BLAKE2b-256 checksum
How to use checksums
d3b06dee1b86f6c79aafc60a5a7842885b4ad2c3cae66aacb6eca0a406c20634
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.9

Release files / decfunc-1.0.0-py3-none-any.whl

Download URL decfunc-1.0.0-py3-none-any.whl
Size 3.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9cd39a4cac99b5409ee663ab6f4f1e0708980201483b0adb6c059ea615463f66
BLAKE2b-256 checksum
How to use checksums
c65eb78961bdfe0911ce45017f0b881491405831ad324bd34c7a290d7fab9352
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.9

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.2.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