Skip to content

Commit 2c545b8

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 92fc97e commit 2c545b8

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

OMPython/ModelicaSystem.py

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

10191019
if isinstance(raw_input, dict):
10201020
for key, val in raw_input.items():
1021-
str_val = str(val)
1021+
# convert all values to strings to align it on one type: dict[str, str]
1022+
# spaces have to be removed as setInput() could take list of tuples as input and spaces would
1023+
str_val = str(val).replace(' ', '')
10221024
if ' ' in key or ' ' in str_val:
10231025
raise ModelicaSystemError(f"Spaces not allowed in key/value pairs: {repr(key)} = {repr(val)}!")
10241026
input_data[key] = str_val
@@ -1175,9 +1177,11 @@ def setOptimizationOptions(self, optimizationOptions: str | list[str] | dict[str
11751177

11761178
def setInputs(self, name: str | list[str] | dict[str, Any]) -> bool:
11771179
"""
1178-
This method is used to set input values. It can be called:
1179-
with a sequence of input name and assigning corresponding values as arguments as show in the example below:
1180-
usage
1180+
This method is used to set input values. It can be called with a sequence of input name and assigning
1181+
corresponding values as arguments as show in the example below. Compared to other set*() methods this is a
1182+
special case as value could be a list of tuples - these are converted to a string in _prepare_input_data()
1183+
and restored here via ast.literal_eval().
1184+
11811185
>>> setInputs("Name=value") # depreciated
11821186
>>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
11831187
>>> setInputs(name={"Name1": "value1", "Name2": "value2"})
@@ -1186,7 +1190,11 @@ def setInputs(self, name: str | list[str] | dict[str, Any]) -> bool:
11861190

11871191
for key, val in inputdata.items():
11881192
if key in self.inputlist:
1193+
if not isinstance(val, str):
1194+
raise ModelicaSystemError(f"Invalid data in input for {repr(key)}: {repr(val)}")
1195+
11891196
val_evaluated = ast.literal_eval(val)
1197+
11901198
if isinstance(val_evaluated, (int, float)):
11911199
self.inputlist[key] = [(float(self.simulateOptions["startTime"]), float(val)),
11921200
(float(self.simulateOptions["stopTime"]), float(val))]

0 commit comments

Comments
 (0)