decorator

5.2.1 · active · verified Sat Mar 28

A Python module that simplifies the creation of decorators, ensuring they preserve the original function's signature. Current version: 5.2.1, released on February 24, 2025. Maintained by Michele Simionato. Requires Python 3.8 or higher.

Warnings

Install

Imports

Quickstart

An example demonstrating how to define and use a decorator with the 'decorator' module.

import time
import logging
from decorator import decorator

@decorator
def warn_slow(func, timelimit=60, *args, **kw):
    t0 = time.time()
    result = func(*args, **kw)
    dt = time.time() - t0
    if dt > timelimit:
        logging.warning('%s took %d seconds', func.__name__, dt)
    else:
        logging.info('%s took %d seconds', func.__name__, dt)
    return result

@warn_slow  # warn if it takes more than 1 minute
def preprocess_input_files(inputdir, tempdir):
    # Function implementation here
    pass

@warn_slow(timelimit=600)  # warn if it takes more than 10 minutes
def run_calculation(tempdir, outdir):
    # Function implementation here
    pass

# Usage
preprocess_input_files('/path/to/input', '/path/to/temp')
run_calculation('/path/to/temp', '/path/to/output')

view raw JSON →