Last active
December 5, 2019 20:22
-
-
Save messiest/c6c86d60dd5544783d5eb2be448cfb8a 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 | |
from contextlib import ContextDecorator | |
class Timer(ContextDecorator): | |
def __enter__(self): | |
self.start = time.time() | |
return self | |
def __exit__(self, *exc): | |
print("RUNTIME: ", self.format_runtime(time.time() - self.start)) | |
return False | |
def format_runtime(self, runtime): | |
if runtime > 60: | |
mins = runtime // 60 | |
secs = runtime % 60 | |
return f"{mins} minutes, {secs} seconds" | |
else: | |
return f"{runtime:.1f} seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment