-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate_component_table_cell_values.py
More file actions
262 lines (208 loc) · 8.68 KB
/
Copy pathvalidate_component_table_cell_values.py
File metadata and controls
262 lines (208 loc) · 8.68 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import math
def validate_data(value, column_name):
validation_message = ''
if column_name in validation_types.keys():
validation_message = validation_types[column_name](value)
return validation_message
def _time_intervals(given_string):
given_string = given_string.replace(' ', '')
for check in top_level_time_window_checks:
check_answer = check(given_string)
if check_answer != '':
return check_answer
check_answer = _elements_comma_separated(given_string)
if check_answer != '':
return check_answer
given_string = given_string[1:-1].replace('],', ']],')
for element in given_string.split('],'):
for check in element_level_checks:
check_answer = check(element)
if check_answer != '':
return check_answer
element = element.replace(':[', ':[[')
for time_string in element.split(':[')[1][1:-1].split(','):
for time_check in time_string_checks:
check_answer = time_check(time_string)
if check_answer != '':
return check_answer
return ''
def _real_value(given_string):
try:
float(given_string)
if math.isinf(float(given_string)) | math.isnan(float(given_string)):
return 'Value should be a real number.'
else:
return ''
except:
return 'Could not convert the given value to a float.'
def _boolean(given_string):
if given_string in ['True', 'False']:
return ''
else:
return 'Value should be True or False.'
def _non_negative_int(given_string):
try:
int(given_string)
if int(given_string) < 0:
return 'Value should be an integer greater than or equal to zero.'
else:
return ''
except:
return 'Could not convert the given value to an integer.'
def _real_non_negative_value(given_string):
try:
float(given_string)
if math.isinf(float(given_string)) | math.isnan(float(given_string)):
return 'Value should be a real number.'
else:
if float(given_string) >= 0:
return ''
else:
return 'Value should be greater than or equal to zero.'
except:
return 'Could not convert the given value to a float.'
def _real_non_negative_value_allow_inf(given_string):
if given_string == 'inf':
return ''
else:
try:
float(given_string)
if math.isnan(float(given_string)):
return 'Value should not be nan.'
elif float(given_string) < 0:
return 'Value should be greater than or equal to zero.'
elif math.isinf(float(given_string)):
return 'Inf should be defined as \'inf\' (no quotes).'
else:
return ''
except:
return 'Could not convert the given value to a float.'
def _list_of_ints(given_string):
if given_string[0] != '[' or given_string[-1] != ']':
return 'Inputs should be a list of integers enclosed in square brackets.'
if len(given_string) > 4 and ',' not in given_string:
return 'Integers should be comma separated.'
given_string = given_string.replace(' ', '')
for month in given_string[1:-1].split(','):
try:
int(month)
if '\'' in month or '\"' in month:
return 'Integers should not be quotes'
if int(month) < 1 or int(month) > 12:
return 'Each month should be an integer between 1 and 12.'
except:
return 'Each month should be an integer between 1 and 12.'
return ''
validation_types = {"TimeIntervals": _time_intervals, 'Value': _real_value, "Weekend": _boolean, "Weekday": _boolean,
"Based on Network Peak": _boolean, "Day Average": _boolean,
"Demand Window Length": _non_negative_int, "Number of Peaks": _non_negative_int,
"Min Demand (kW)": _real_non_negative_value, "Min Demand Charge ($)": _real_non_negative_value,
"HighBound": _real_non_negative_value_allow_inf, "Month": _list_of_ints}
def _elements_comma_separated(given_string):
if ']' not in given_string:
return 'Time window not enclosed in brackets.'
if '[' not in given_string:
return 'Time window not enclosed in brackets.'
if given_string.count('[') != given_string.count(']'):
return 'Time window not enclosed in brackets.'
if '],}' in given_string:
return 'Incorrect trailing comma.'
if given_string.count(']') - 1 != given_string.count('],'):
return 'Time windows not comma separated.'
return ''
def _not_empty_string(test_string):
if test_string != '':
return ''
else:
return 'Blank input provided.'
def _closed_curly_braces(test_string):
if test_string[0] == '{' and test_string[-1] == '}':
return ''
else:
return 'Input not enclosed in curly braces.'
def _non_blank_inside_curly_braces(test_string):
string_without_curly_braces = test_string[1:-1]
if string_without_curly_braces != '':
return ''
else:
return 'Blank input provided inside curly braces.'
top_level_time_window_checks = [_not_empty_string, _closed_curly_braces,
_non_blank_inside_curly_braces]
def _colon_separated(test_string):
if ':[' in test_string:
return ''
else:
return 'Time window names and values should be separated by a colon (:).'
def _time_window_inside_square_brackets(element_string):
if '[' not in element_string:
return 'Time window not inside square brackets.'
element_string = element_string.replace(':[', ':[[')
time_window_string = element_string.split(':[')[1]
if time_window_string[0] == '[' and time_window_string[-1] == ']':
return ''
else:
return 'Time window not inside square brackets.'
def _time_window_has_name(element_string):
element_string = element_string.replace(':[', ':[[')
element_string = element_string.split(':[')[0]
if element_string.split(':')[0] != '':
return ''
else:
return 'Time window without name provided.'
def _window_name_quoted(element_string):
element_string = element_string.replace(':[', ':[[')
name_with_quotes = element_string.split(':[')[0]
if name_with_quotes[0] == '\'' and name_with_quotes[-1] == '\'':
return ''
else:
return 'Time window name not quoted.'
def _window_name_not_blank_inside_quotes(element_string):
element_string = element_string.replace(':[', ':[[')
name_without_quotes = element_string.split(':[')[0][1:-1]
if name_without_quotes != '':
return ''
else:
return 'Blank window name provided inside quotes.'
def _time_window_has_two_elements(element_string):
element_string = element_string.replace(':[', ':[[')
time_window_string_no_brackets = element_string.split(':[')[1][1:-1]
if len(time_window_string_no_brackets.split(',')) == 2:
return ''
else:
return 'Time window should have two times, separated by a comma.'
element_level_checks = [_colon_separated, _time_window_inside_square_brackets,
_time_window_has_name, _window_name_quoted,
_window_name_not_blank_inside_quotes,
_time_window_has_two_elements]
def _time_is_quoted(time_string):
time_string = time_string.strip()
if time_string[0] == '\'' and time_string[-1] == '\'':
return ''
else:
return 'Time not quoted.'
def _hours_and_minutes_colon_separated(time_string):
time_string = time_string.strip()[1:-1]
if ':' in time_string:
return ''
else:
return 'Hours and minutes should be colon separated.'
def _hours_int_less_than_or_equal_to_24(time_string):
hour_string = time_string.strip()[1:-1].split(':')[0]
minute_string = time_string.strip()[1:-1].split(':')[1]
if hour_string.isdigit() and 0 <= int(hour_string) <= 23:
return ''
elif hour_string.isdigit() and int(hour_string) == 24:
if minute_string in ['0', '00']:
return ''
else:
return 'The max input time should be 24:00.'
else:
return 'The hour provided is not an integer between 0 and 24.'
def _minutes_int_0_or_30(time_string):
hour_string = time_string.strip()[1:-1].split(':')[1]
if hour_string.isdigit() and int(hour_string) in [0, 30]:
return ''
else:
return 'The minute provided is not 0 or 30 min.'
time_string_checks = [_time_is_quoted, _hours_and_minutes_colon_separated,
_minutes_int_0_or_30, _hours_int_less_than_or_equal_to_24]