API Guide
Model Class
- decoratorsutility.decorators.exception_dec(exc_mod, exc_path)
Decorator that handles exceptions by logging errors to a specified file path.
Args: - exc_mod (str): The module being executed. - exc_path (str): The path where the output file will be stored.
- Usage:
@exception_dec('module_name', '/path/to/logfile.log') def function_name(*args, **kwargs): try: # Function execution return func(*args, **kwargs) except Exception as err: # Logs the error to the specified file path log_error(module=exc_mod, file_path=exc_path, error=err) logging.error(err)
- decoratorsutility.decorators.repeat_on_error(max_try: int = 3, error_log: str = None, show_error: bool = True)
Decorator that retries function execution a set number of times in case of exceptions.
- Parameters:
max_try – Maximum number of retries.
error_log – File path to log errors (txt or csv format).
show_error – Flag to display errors (default is True).
- Returns:
Decorated function.
- Usage:
@repeat_on_error(max_try=3, error_log='log/logfile.log', show_error=True) def test_dummy(arg1:int): return arg1/0 if __name__ == '__main__': test_dummy(1)
- decoratorsutility.decorators.timeout_decorator(timeout)
decorrator to set a timeout to a function
- Parameters:
timeout (int) – time in seconts
- Usage:
import time from svh_utils.decorators import timeout_decorator @timeout_decorator(timeout=5) def my_function(): time.sleep(6) # Simulate a long-running function return "Success!" if __name__ == '__main__': a=my_function()
- class decoratorsutility.decorators._Timer
class to estimate the time it takes to run a function and number of executions
- Usage:
from decoratorsutility import timer_decorator @timer_decorator def dummy(x): return x+1 for i in range(10): x=dummy(i) #get the information of the function timer_decorator.data {'dummy': {'executions': 10, 'total_time': 0.0}}