def start(self): if self._state == self._STARTED: return self self._started_at = misc.wallclock() self._stopped_at = None self._state = self._STARTED return self
def elapsed(self): if self._state == self._STOPPED: return float(self._stopped_at - self._started_at) elif self._state == self._STARTED: return float(misc.wallclock() - self._started_at) else: raise RuntimeError("Can not get the elapsed time of an invalid" " stopwatch")
def leftover(self): if self._duration is None: raise RuntimeError("Can not get the leftover time of a watch that" " has no duration") if self._state != self._STARTED: raise RuntimeError("Can not get the leftover time of a stopwatch" " that has not been started") end_time = self._started_at + self._duration return max(0.0, end_time - misc.wallclock())
def stop(self): if self._state == self._STOPPED: return self if self._state != self._STARTED: raise RuntimeError("Can not stop a stopwatch that has not been" " started") self._stopped_at = misc.wallclock() self._state = self._STOPPED return self