-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
78 lines (64 loc) · 1.8 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import time
import pickle
from pathlib import Path
import os
def myassert(condition, msg):
if not condition:
raise Exception(msg)
def myassertList(my_var, my_list):
if my_var not in my_list:
msg = 'Expected one of ' + ','.join(map(str,my_list)) + \
' but got {0}'.format(my_var)
raise Exception(msg)
def unpickle(file):
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
def doesFileExist(path):
my_file = Path(path)
if my_file.is_file():
return True
return False
def doesDirExist(path):
my_file = Path(path)
if my_file.is_dir():
return True
return False
def makeDir(path):
os.makedirs(path)
def checkMakeDir(path):
if not doesDirExist(path):
makeDir(path)
def wipeDir(path):
for the_file in os.listdir(path):
file_path = os.path.join(path, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
raise e
class StopWatch:
def __init__(self):
self.start_time = None
def start(self):
"""Starts the timer"""
self.start_time = time.time()
self.last_lap = self.start_time
return self.start_time
def stop(self):
"""Stops the timer. Returns the time elapsed"""
self.stop_time = time.time()
ret_val = self.stop_time - self.start_time
self.start_time = None
return ret_val
def lap(self):
if self.start_time == None:
self.start()
return 0
new_lap = time.time()
lap_time = new_lap - self.last_lap
self.last_lap = new_lap
return lap_time
def getElapsed(self):
return time.time() - self.start_time