decorator
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
- breaking Importing the 'decorator' function as 'import decorator' is incorrect and will lead to an ImportError.
- gotcha The 'decorator' module requires Python 3.8 or higher. Using it with earlier versions will result in a VersionError.
Install
-
pip install decorator
Imports
- decorator
from decorator import decorator
Quickstart
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')