-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameters_menu.py
More file actions
56 lines (45 loc) · 2.26 KB
/
parameters_menu.py
File metadata and controls
56 lines (45 loc) · 2.26 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
from generation_models.general_model import GenModel
from helpers import helper, curses_helper
def __write_input_request(var_name, value, end=''):
curses_helper.write_formatted(" '{0, 2}' value ({1, 3}): " + end, var_name, value)
def complete(model: GenModel, error_message: str = ''):
helper.flush_input()
caption: str = f'Enter the parameters of the {model.name}:'
parameters: dict = model.parameters
# init curses_helper
curses_helper.init()
# write an error message if it exists
curses_helper.write_formatted("{0}", error_message)
# write a caption
curses_helper.write_formatted("{0, 2} {1, 1}\n", chr(9679), caption)
previous_value_dict = {}
# start parameters input loop
for k, v in parameters.items():
if not v['constant']:
# we should check whether the user's input is correct
while 1:
__write_input_request(k, v['value'])
value = curses_helper.read(allow_single_dot=isinstance(v['value'], float))
if value == '':
value = str(v['value'])
curses_helper.move_line_home(-1)
__write_input_request(k, value, end=value + '\n')
break
# notify a user that input value is not correct
if helper.safe_cast(value, int, None) is None and helper.safe_cast(value, float, None) is None:
curses_helper.move_line_home(-1)
curses_helper.write_formatted("{0,4} {1,1}", chr(9679),
"Input value must be a positive number! Press Enter to try again.")
curses_helper.read()
curses_helper.move_line_home(-1)
else:
break # if the input is correct then break loop
previous_value_dict[k] = str(v['value'])
v['value'] = type(v['value'])(value)
curses_helper.close()
for k, v in model.param_conditions.items():
condition = v.format('model.parameters')
if not eval(condition):
return complete(model, "An error occurred while checking the conditions of the model parameters: \n"
"\t{0}\n\n".format(k))
return True