Created
January 10, 2018 16:04
-
-
Save nicoknoll/f5d1769b43bb12cca818db644f233ca6 to your computer and use it in GitHub Desktop.
Easy python tracking of whole functions or blocks of code
This file contains 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
class Track: | |
def print_result(self, name, start_time, end_time): | |
time_delta = end_time - start_time | |
result = '%r took: %2.4f sec' % (name, time_delta) | |
print(result) | |
def time(self, func): | |
@wraps(func) | |
def func_wrapper(*args, **kwargs): | |
start_time = time.time() | |
func_result = func(*args, **kwargs) | |
self.print_result(func.__name__, start_time, time.time()) | |
return func_result | |
return func_wrapper | |
def partial(self, partial_name): | |
_self = self | |
class class_wrapper: | |
def __enter__(self): | |
self.start_time = time.time() | |
return self | |
def __exit__(self, type, value, traceback): | |
_self.print_result(partial_name, self.start_time, time.time()) | |
return class_wrapper() | |
track = Track() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment