Skip to content

Commit

Permalink
add time_track.py
Browse files Browse the repository at this point in the history
  • Loading branch information
metaphysicser committed Apr 10, 2023
1 parent 4c274f8 commit 847a46b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions utils/time_track.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 847a46b

Please sign in to comment.