From 847a46b1849d8f87a2052b727f7c753716edfc69 Mon Sep 17 00:00:00 2001 From: zpl010720 Date: Mon, 10 Apr 2023 13:13:12 +0800 Subject: [PATCH] add time_track.py --- utils/time_track.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 utils/time_track.py diff --git a/utils/time_track.py b/utils/time_track.py new file mode 100644 index 0000000..1efd89f --- /dev/null +++ b/utils/time_track.py @@ -0,0 +1,50 @@ +import time +from functools import partial + + +def base_time_desc_decorator(method, desc='test_description'): + def timed(*args, **kwargs): + + # Print Description + # print('#' * 50) + print(desc) + # print('#' * 50 + '\n') + + # Calculation Runtime + start = time.time() + + # Run Method + try: + result = method(*args, **kwargs) + except TypeError: + result = method(**kwargs) + + # Print Runtime + print('Done! It took {:.2} secs\n'.format(time.time() - start)) + + if result is not None: + return result + + return timed + + +def time_desc_decorator(desc): return partial(base_time_desc_decorator, desc=desc) + + +@time_desc_decorator('this is description') +def time_test(arg, kwarg='this is kwarg'): + time.sleep(3) + print('Inside of time_test') + print('printing arg: ', arg) + print('printing kwarg: ', kwarg) + + +@time_desc_decorator('this is second description') +def no_arg_method(): + print('this method has no argument') + + +if __name__ == '__main__': + time_test('hello', kwarg=3) + time_test(3) + no_arg_method()