Last active
November 2, 2018 08:31
-
-
Save cypreess/1bd7f5a2328c755c31a33974e4491e92 to your computer and use it in GitHub Desktop.
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
import time | |
import datetime | |
class MeasureDuration: | |
def __init__(self, title: str = "Total time taken %s"): | |
self.start = None | |
self.end = None | |
self.title = title | |
def __enter__(self): | |
self.start = time.time() | |
return self | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
self.end = time.time() | |
print(self.title % self.duration()) | |
def duration(self): | |
return str(datetime.timedelta(seconds=(self.end - self.start))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment