Recursive Decorator
Decorator to apply a given decorator recursively on all function, inside a function/method, recursively.
What is recursive_decorator?
recursive_decorator is a decorator that allows us to decorate/trasform all functions along the stack call at runtime, motivated by the need to add/transform logics, to knownunknown functions, along the stack calls.
Notes:
Functions/Methods will not be replaced, new instances will be returned.
Function/Methods cannot be wrapped more then once with same transformer/decorator.
Installing
$ pip install recursive_decorator
Usage
import recursive_decorator
from recursive_decorator import recursive_decorator
define your decorator to apply recursively on all functions.
>>> def decorator(f):
...: def wrapper(*args, **kwargs):
...: print(f.__name__)
...: return f(*args, **kwargs)
...: return wrapper
Now using your decorator on function without using recursive_decorator will leads to the following output
>>> @decorator
...:def main_function():
...: sub_function()
>>> main_function()
main_function
Using recursive_decorator leads to
>>> @recursive_decorator(decorator)
...:def main_function():
...: sub_function()
>>> main_function()
main_function
sub_function
Furthermore, if sub_function has function calls, they will decorated to
>>> def sub_function():
...: another_function()
>>> @recursive_decorator(decorator)
...:def main_function():
...: sub_function()
>>> main_function()
main_function
sub_function
another_function
and so on…
Examples
Stop on Execption
We can wrap all functions with try except…
>>> import sys
>>> import ipdb
>>> from recursive_decorator import recursive_decorator
>>> def wrap_function_with_try_except(f):
...: def transformed_func(*args, **kwargs):
...: try:
...: return f(*args, **kwargs)
...: except:
...: ipdb.set_trace(sys._getframe().f_back)
...: return transformed_func
>>> def throws_exception():
...: raise Exception
>>> @recursive_decorator(wrap_function_with_try_except)
...:def function():
...: throws_exception()
...: pass
>>> function()
21 throws_exception()
---> 22 pass
23
If function will throw an error… ipdb session will start.
Profiler
We can set time profiler for all running functions.
>>> import time
>>> from recursive_decorator import recursive_decorator
>>> def duration_transformer(f):
...: def transformed_func(*args, **kwargs):
...: start_time = time.time()
...: value = f(*args, **kwargs)
...: end_time = time.time()
...: print("function {} duration is {} minutes"
...: .format(f.__name__, end_time - start_time))
...: return value
...: return transformed_func
>>> def waiting_function():
...: time.sleep(5)
>>> @recursive_decorator(duration_transformer)
...:def function():
...: waiting_function()
>>> function()
function waiting_function duration is 5.00511908531189 minutes
function function duration is 5.006134510040283 minutes
Metadata
Release files for recursive-decorator 1.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| recursive_decorator-1.0.7.tar.gz | 5.9 kB | Without provenance | Details for recursive_decorator-1.0.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| recursive_decorator-1.0.7-py3-none-any.whl | Python 3 | none | any | Details for recursive_decorator-1.0.7-py3-none-any.whl |
Total release size: 12.8 kB
Release files / recursive_decorator-1.0.7.tar.gz
| Download URL | recursive_decorator-1.0.7.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
41d88e3424c6ea14eee4dc56799193aa2ea4602bfb2f35edb93f524e73c61c91
|
|
BLAKE2b-256 checksum How to use checksums |
ef8b065d1e80ed4f3d4eb2d13a443b7f484f28ea09dfe1a19b8318cf71a02e78
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.4.6
|
Release files / recursive_decorator-1.0.7-py3-none-any.whl
| Download URL | recursive_decorator-1.0.7-py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5a6b840a67f01bd295e62e3995647de6a6d89fdb407a93d751c2537d887eefdf
|
|
BLAKE2b-256 checksum How to use checksums |
370c6580dbfb6a01d8e0e752d5994ed26780359f5d97ad15443888b424994418
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.4.6
|
Security for recursive-decorator 1.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.