wrapper_function
unknown
python
3 years ago
1.2 kB
9
Indexable
import time
def wrapper_time(a_function:callable) ->callable:
"""A wrapper function that measures time it takes for function to complete. It accepts other functions as input."""
def wrapped_function(*args, **kwargs):
"""A wrapper function content, designed to accept all possible arguments."""
time_start = time.time()
result = a_function(*args, **kwargs) #defines wrapper result -> a function accepting arguments.
time_end = time.time()
print(f'Function {a_function.__name__} took {time_end - time_start} seconds to complete.')
return result #wrapped function returns same output as input function.
return wrapped_function #Wrapper returns function with time log.
def get_sequence(n):
"""Example function to get wrapped. High arguments take long time to complete (hard stopped at n>23 to prevent excessive calculation time)."""
if n <= 0:
return 1
elif n > 23:
return 1
else:
v = 0
for i in range(n):
v += 1 + (get_sequence(i - 1) + get_sequence(i))/2
return v
wrapper_get_sequence = wrapper_time(get_sequence)
print(wrapper_get_sequence(22))
Editor is loading...