pt_decorators
Module Path: pt_dev_utility.pt_decorators
Description
Decorator utilities for function timing and performance monitoring. This module provides decorators for measuring function execution time for Python 3.8+.
Functions
measure_execution_time(func: Callable[..., Any]) -> Callable[..., Any]
Description: Decorator to measure and display function execution time.
This decorator calculates the total execution time of a function and prints it in HH:MM:SS format. Includes a 1-second delay after function execution for timing accuracy.
Parameters:
func(Callable[..., Any]) - The function to be decorated
Returns:
- Callable[..., Any] - Wrapped function that measures execution time
Features: - Measures precise execution time - Displays time in readable HH:MM:SS format - Preserves original function metadata - Includes 1-second delay for timing accuracy - Works with any function signature
Example Usage:
import pt_dev_utility as pdu
import time
# Example 1: Basic usage
@pdu.measure_execution_time
def slow_function():
time.sleep(2)
return "Task completed"
result = slow_function()
print(result)
# Output: Execution time for function slow_function is 00:00:03
# Output: Task completed
# Example 2: With parameters
@pdu.measure_execution_time
def process_data(data_size, iterations=1):
"""Process data with given size and iterations."""
total = 0
for i in range(iterations):
total += sum(range(data_size))
return total
result = process_data(1000, 5)
print(f"Result: {result}")
# Output: Execution time for function process_data is 00:00:01
# Output: Result: 2497500
# Example 3: With class methods
class DataProcessor:
@pdu.measure_execution_time
def analyze_data(self, dataset):
"""Analyze the given dataset."""
time.sleep(1.5)
return f"Analyzed {len(dataset)} records"
processor = DataProcessor()
result = processor.analyze_data([1, 2, 3, 4, 5])
print(result)
# Output: Execution time for function analyze_data is 00:00:02
# Output: Analyzed 5 records
# Example 4: Multiple decorated functions
@pdu.measure_execution_time
def fetch_data():
time.sleep(0.5)
return "Data fetched"
@pdu.measure_execution_time
def transform_data(data):
time.sleep(0.3)
return f"Transformed: {data}"
@pdu.measure_execution_time
def save_data(data):
time.sleep(0.2)
return f"Saved: {data}"
# Chain operations
data = fetch_data()
transformed = transform_data(data)
saved = save_data(transformed)
# Each function will print its execution time
Use Cases
- Performance Monitoring - Track function execution times in production
- Optimization - Identify slow functions that need optimization
- Debugging - Monitor timing during development and testing
- Profiling - Get quick timing information without complex profiling tools
- API Monitoring - Track response times for API endpoints
- Batch Processing - Monitor long-running batch operations
Notes
- The decorator adds a 1-second delay after function execution for timing accuracy
- Execution time includes the 1-second delay in the measurement
- Original function's
__name__,__doc__, and other attributes are preserved - Works with functions that have any number of positional and keyword arguments
- Thread-safe and can be used in multi-threaded applications
- Time is displayed in HH:MM:SS format for easy reading