Created
December 4, 2023 17:05
-
-
Save pytlv/cac0afeae671091b6abd71536200ebc2 to your computer and use it in GitHub Desktop.
Python wrapper function to monitor execution time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # source: Sadrach Pierre https://builtin.com/data-science/python-wrapper | |
| from functools import wraps | |
| def runtime_monitor(input_function): | |
| @functools.wraps(input_function) | |
| def runtime_wrapper(*args, **kwargs): | |
| start_value = time.perf_counter() | |
| return_value = input_function(*args, **kwargs) | |
| end_value = time.perf_counter() | |
| runtime_value = end_value - start_value | |
| print(f"Finished executing {input_function.__name__} in {runtime_value} seconds") | |
| return return_value | |
| return runtime_wrapper | |
| def debugging_method(input_function): | |
| @functools.wraps(input_function) | |
| def debugging_wrapper(*args, **kwargs): | |
| arguments = [] | |
| keyword_arguments = [] | |
| for a in args: | |
| arguments.append(repr(a)) | |
| for key, value in kwargs.items(): | |
| keyword_arguments.append(f"{key}={value}") | |
| function_signature = arguments + keyword_arguments | |
| function_signature = "; ".join(function_signature) | |
| print(f"{input_function.__name__} has the following signature: {function_signature}") | |
| return_value = input_function(*args, **kwargs) | |
| print(f"{input_function.__name__} has the following return: {return_value}") | |
| return return_value | |
| return debugging_wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment