Skip to main content

Decorator to apply given decorator recursively on functions

Project description

PyPI - Python Version https://badge.fury.io/py/recursive-decorator.svg https://travis-ci.org/yakobu/recursive_decorator.svg?branch=master https://coveralls.io/repos/github/yakobu/recursive_decorator/badge.svg?branch=master

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

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

recursive_decorator-1.0.6.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

recursive_decorator-1.0.6-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file recursive_decorator-1.0.6.tar.gz.

File metadata

  • Download URL: recursive_decorator-1.0.6.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for recursive_decorator-1.0.6.tar.gz
Algorithm Hash digest
SHA256 0537602acf81ddc7d6cb76970c60ee1516678a4aba172dc773c3e21c2469a3e9
MD5 9c3fd99f3b95ea2481bfe28b668ac20e
BLAKE2b-256 78c1043d88cc4d4fff36b065113bd28c5522ff585419099ea86aef447acd6d51

See more details on using hashes here.

Provenance

File details

Details for the file recursive_decorator-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: recursive_decorator-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for recursive_decorator-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1bbe98dc4c1d3550546b0b84affc80361d1e6b71e9ea9e28f8594c514cb7d0df
MD5 a5bcc96faa015e80dda451ef2d91c8cb
BLAKE2b-256 61f6b3904dbec7b2cc2d12aeac25117924988bf16e63524bba8e5aad71e89c37

See more details on using hashes here.

Provenance

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