Last active
July 7, 2016 08:34
-
-
Save dmitryhd/b5eb8ee50f3bc27b9f952d827d7e2e09 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Timer: | |
| def __init__(self, precision=1): | |
| self.precision = precision | |
| self.creation_time = time.time() | |
| self.start() | |
| def start(self): | |
| self.btime = time.time() | |
| @property | |
| def time(self) -> float: | |
| return round(time.time() - self.btime, self.precision) | |
| @property | |
| def time_total(self) -> float: | |
| return round(time.time() - self.creation_time, self.precision) | |
| def round(self) -> float: | |
| t = self.time | |
| self.start() | |
| return t | |
| # usage | |
| import time | |
| timer = Timer(precision=3) | |
| print(timer.time) | |
| time.sleep(0.1) | |
| print(timer.round()) | |
| time.sleep(0.1) | |
| print(timer.round()) | |
| print(timer.time_total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment