Skip to content

Commit b5766f4

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 1b5917c commit b5766f4

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
@@ -1159,7 +1159,9 @@ def prepare_str(str_in: str) -> dict[str, str]:
11591159

11601160
if isinstance(raw_input, dict):
11611161
for key, val in raw_input.items():
1162-
str_val = str(val)
1162+
# convert all values to strings to align it on one type: dict[str, str]
1163+
# spaces have to be removed as setInput() could take list of tuples as input and spaces would
1164+
str_val = str(val).replace(' ', '')
11631165
if ' ' in key or ' ' in str_val:
11641166
raise ModelicaSystemError(f"Spaces not allowed in key/value pairs: {repr(key)} = {repr(val)}!")
11651167
input_data[key] = str_val
@@ -1334,9 +1336,11 @@ def setInputs(
13341336
name: str | list[str] | dict[str, Any],
13351337
) -> bool:
13361338
"""
1337-
This method is used to set input values. It can be called:
1338-
with a sequence of input name and assigning corresponding values as arguments as show in the example below:
1339-
usage
1339+
This method is used to set input values. It can be called with a sequence of input name and assigning
1340+
corresponding values as arguments as show in the example below. Compared to other set*() methods this is a
1341+
special case as value could be a list of tuples - these are converted to a string in _prepare_input_data()
1342+
and restored here via ast.literal_eval().
1343+
13401344
>>> setInputs("Name=value") # depreciated
13411345
>>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
13421346
>>> setInputs(name={"Name1": "value1", "Name2": "value2"})
@@ -1345,7 +1349,11 @@ def setInputs(
13451349

13461350
for key, val in inputdata.items():
13471351
if key in self._inputs:
1352+
if not isinstance(val, str):
1353+
raise ModelicaSystemError(f"Invalid data in input for {repr(key)}: {repr(val)}")
1354+
13481355
val_evaluated = ast.literal_eval(val)
1356+
13491357
if isinstance(val_evaluated, (int, float)):
13501358
self._inputs[key] = [(float(self._simulate_options["startTime"]), float(val)),
13511359
(float(self._simulate_options["stopTime"]), float(val))]

0 commit comments

Comments
 (0)