-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
203 lines (184 loc) · 7.73 KB
/
exceptions.py
File metadata and controls
203 lines (184 loc) · 7.73 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from color import Colors as C
class Exceptions():
"""
Handles exceptions
:param origin: The name of the function that called this function
:type origin: str
:param param: The parameters that should be checked
:type param: list
:param check: The types that the parameters should have
:type check: list
:param strict: If True, the types have to match exactly, if False, the types have to be subclasses of the given types, defaults to False
:type strict: bool, optional
"""
def __init__(self) -> None:
pass
def type_checker(origin: str, param: list, check: list, strict=False):
"""
Checks if the types of the given parameters are correct
:param origin: The name of the function that called this function
:type origin: str
:param param: The parameters that should be checked
:type param: list
:param check: The types that the parameters should have
:type check: list
:param strict: If True, the types have to match exactly, if False, the types have to be subclasses of the given types, defaults to False
:type strict: bool, optional
"""
print(C.ok("checking" + str(param)))
print(C.ok("cmmp" + str(check)))
if (len(param) != len(check)):
print(C.error("[" +
origin +
"] ERROR:" +
(str(len(check))) +
" arguments were expected but " +
(str(len(param))) +
" were given"))
raise BaseException()
for i in range(len(param)):
if (((type(check[i])) == list)):
if ((type(param[i])) == list):
obj = Exceptions()
obj.type_checker_rek(origin, param[i], check[i], strict)
else:
print(C.error("[" +
origin +
"] ERROR: Type " +
(str(type(check[i]))) +
" was expected as " +
(str(i)) +
"-th argument but " +
(str(type(param[i]))) +
" was found"))
raise BaseException()
elif ((not isinstance((param[i]), check[i])) and not (issubclass(type(param[i]), check[i])) and not strict):
print(C.error("[" +
origin +
"] ERROR: Type " +
(str(check[i])) +
" was expected as " +
(str(i)) +
"-th argument but " +
(str(type(param[i]))) +
" was found"))
raise BaseException()
elif ((not isinstance((param[i]), check[i])) and strict):
print(C.error("[" +
origin +
"] ERROR: Type " +
(str(check[i])) +
" was expected as " +
(str(i)) +
"-th argument but " +
(str(type(param[i]))) +
" was found"))
raise BaseException()
print(
C.checkSuc(
"[" + origin + "] SUCCESS: Type check completed successfully"))
return
def type_checker_rek(
self,
origin: str,
param: list,
check: list,
strict: bool):
"""
Recursively checks if the types of the given parameters are correct
:param origin: The name of the function that called this function
:type origin: str
:param param: The parameters that should be checked
:type param: list
:param check: The types that the parameters should have
:type check: list
:param strict: If True, the types have to match exactly, if False, the types have to be subclasses of the given types, defaults to False
:type strict: bool, optional
"""
if (len(param) != len(check)):
print(C.error("[" +
origin +
"] ERROR:" +
(str(len(check))) +
" arguments were expected but " +
(str(len(param))) +
" were given"))
raise BaseException()
for i in range(len(param)):
if (((type(check[i])) == list)):
if ((type(param[i])) == list):
self.type_checker_rek(origin, param[i], check[i])
else:
print(C.error("[" +
origin +
"] ERROR: Type " +
(str(type(check[i]))) +
" was expected as " +
(str(i)) +
"-th argument but " +
(str(type(param[i]))) +
" was found"))
raise BaseException()
elif ((not isinstance((param[i]), check[i])) and not (issubclass(type(param[i]), check[i])) and not strict):
print(C.error("[" +
origin +
"] ERROR: Type " +
(str(check[i])) +
" was expected as " +
(str(i)) +
"-th argument but " +
(str(type(param[i]))) +
" was found"))
raise BaseException()
elif ((not isinstance((param[i]), check[i])) and strict):
print(C.error("[" +
origin +
"] ERROR: Type " +
(str(check[i])) +
" was expected as " +
(str(i)) +
"-th argument but " +
(str(type(param[i]))) +
" was found"))
raise BaseException()
return
def bound_checker(
origin: str,
varname: str,
val: float,
lower: float,
upper: float):
"""
Checks if the given value is in the given range
:param origin: The name of the function that called this function
:type origin: str
:param varname: The name of the variable that should be checked
:type varname: str
:param val: The value that should be checked
:type val: float
:param lower: The lower bound of the range
:type lower: float
:param upper: The upper bound of the range
:type upper: float
"""
if ((val > upper) or (val < lower)):
print(
C.warning(
"[" +
origin +
"] WARNING: The variable " +
varname +
" was expected to be in the range [" +
str(lower) +
"," +
str(upper) +
"] but was " +
str(val)))
else:
print(
C.checkSuc(
"[" +
origin +
"] SUCCESS: Range check of " +
varname +
" completed successfully"))