-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.py
More file actions
33 lines (27 loc) · 992 Bytes
/
timer.py
File metadata and controls
33 lines (27 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import time
class TimerError(Exception):
"""A custom exception used to report errors in use of Timer class"""
class Timer:
def __init__(self):
self._start_time = None
self._elapsed_time = 0
def start(self):
if self._start_time is not None:
raise TimerError("timer is already running")
self._start_time = time.monotonic()
def stop(self):
if self._start_time is None:
raise TimerError("timer is not running")
elapsed_time = time.monotonic() - self._start_time
self._elapsed_time += elapsed_time
self._start_time = None
def reset(self):
# self._start_time = None
# self._elapsed_time = 0
self._elapsed_time = 0
def total(self):
if self._start_time is not None:
current_elapsed_time = time.monotonic() - self._start_time
return self._elapsed_time + current_elapsed_time
else:
return self._elapsed_time