-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
39 lines (26 loc) · 1.01 KB
/
utils.py
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
34
35
36
37
38
39
import time
import os
def loadInputFile(filename):
filepath = os.path.join("data", filename)
with open(filepath) as f:
data = f.read().splitlines()
return data
class TimerError(Exception):
"""A custom exception used to report errors in use of Timer class"""
class Timer:
# https://realpython.com/python-timer/#python-timer-functions
def __init__(self, name):
self.name = name
self._start_time = None
def start(self):
"""Start a new timer"""
if self._start_time is not None:
raise TimerError("Timer is running. Use .stop() to stop it")
self._start_time = time.perf_counter()
def stop(self):
"""Stop the timer, and report the elapsed time"""
if self._start_time is None:
raise TimerError("Timer is not running. Use .start() to start it")
elapsed_time = time.perf_counter() - self._start_time
self._start_time = None
print("Elapsed time ({}): {:.3f}s".format(self.name, elapsed_time))