Created
September 30, 2010 03:27
-
-
Save clayg/603968 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
from time import time | |
class StopWatch(): | |
""" | |
Like using a stopwatch to time things, use start and stop methods, | |
restart will reset the timer | |
""" | |
def __init__(self, start=False): | |
"""New timers are not started by default | |
""" | |
if not start: | |
now = time() | |
self._counter = 0.0 | |
self._start = now | |
self._stop = now | |
else: | |
self.restart() | |
def restart(self): | |
"""Reset timer to zero and start | |
""" | |
self._counter = 0.0 | |
self._start = time() | |
self._stop = None | |
def _get_end(self): | |
return self._stop if self._stop else time() | |
@property | |
def time(self): | |
"""Current elapsed time on the clock in seconds as a float | |
""" | |
end = self._get_end() | |
return end - self._start + self._counter | |
def start(self): | |
"""Continue elapsed time | |
""" | |
self._counter = self.time | |
self._start = time() | |
self._stop = None | |
def stop(self): | |
"""Multiple calls to stop won't do anything | |
""" | |
self._stop = self._get_end() | |
def __str__(self): | |
return str(self.time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment