-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4ec202
commit 27abf8e
Showing
4 changed files
with
45 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import time | ||
|
||
class TimerError(Exception): | ||
"""A custom exception used to report errors in use of Timer class""" | ||
|
||
class Timer: | ||
def __init__( | ||
self, | ||
label="Elapsed time", | ||
logger=print | ||
): | ||
self._start_time = None | ||
self.label = label | ||
self.logger = logger | ||
|
||
def start(self): | ||
"""Start a new timer""" | ||
if self._start_time is not None: | ||
raise TimerError(f"Timer is running. Use .stop() to stop it") | ||
|
||
self._start_time = time.perf_counter() | ||
return self | ||
|
||
def stop(self): | ||
"""Stop the timer, and report the elapsed time""" | ||
if self._start_time is None: | ||
raise TimerError(f"Timer is not running. Use .start() to start it") | ||
|
||
elapsed_time = time.perf_counter() - self._start_time | ||
self._start_time = None | ||
|
||
if self.logger: | ||
self.logger((self.label + ": {:0.4f} seconds").format(elapsed_time)) | ||
|
||
return elapsed_time |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
version: "3.6" | ||
services: | ||
hasuragres: | ||
build: . | ||
|