Skip to content

Commit 010cabf

Browse files
committed
[ModelicaSystem] setInput() - handly input data as list of tuples
This method is used to set input values. It can be called with a sequence of input name and assigning corresponding values as arguments as show in the example below. Compared to other set*() methods this is a special case as value could be a list of tuples - these are converted to a string in _prepare_input_data() and restored here via ast.literal_eval().
1 parent c0498b1 commit 010cabf

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,9 @@ def prepare_str(str_in: str) -> dict[str, str]:
11141114

11151115
if isinstance(raw_input, dict):
11161116
for key, val in raw_input.items():
1117-
str_val = str(val)
1117+
# convert all values to strings to align it on one type: dict[str, str]
1118+
# spaces have to be removed as setInput() could take list of tuples as input and spaces would
1119+
str_val = str(val).replace(' ', '')
11181120
if ' ' in key or ' ' in str_val:
11191121
raise ModelicaSystemError(f"Spaces not allowed in key/value pairs: {repr(key)} = {repr(val)}!")
11201122
input_data[key] = str_val
@@ -1289,9 +1291,11 @@ def setInputs(
12891291
name: str | list[str] | dict[str, Any],
12901292
) -> bool:
12911293
"""
1292-
This method is used to set input values. It can be called:
1293-
with a sequence of input name and assigning corresponding values as arguments as show in the example below:
1294-
usage
1294+
This method is used to set input values. It can be called with a sequence of input name and assigning
1295+
corresponding values as arguments as show in the example below. Compared to other set*() methods this is a
1296+
special case as value could be a list of tuples - these are converted to a string in _prepare_input_data()
1297+
and restored here via ast.literal_eval().
1298+
12951299
>>> setInputs("Name=value") # depreciated
12961300
>>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
12971301
>>> setInputs(name={"Name1": "value1", "Name2": "value2"})
@@ -1300,7 +1304,11 @@ def setInputs(
13001304

13011305
for key, val in inputdata.items():
13021306
if key in self._inputs:
1307+
if not isinstance(val, str):
1308+
raise ModelicaSystemError(f"Invalid data in input for {repr(key)}: {repr(val)}")
1309+
13031310
val_evaluated = ast.literal_eval(val)
1311+
13041312
if isinstance(val_evaluated, (int, float)):
13051313
self._inputs[key] = [(float(self._simulate_options["startTime"]), float(val)),
13061314
(float(self._simulate_options["stopTime"]), float(val))]

0 commit comments

Comments
 (0)