Skip to content

Commit 6f9f795

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 ea35d4c commit 6f9f795

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

10281028
if isinstance(raw_input, dict):
10291029
for key, val in raw_input.items():
1030-
str_val = str(val)
1030+
# convert all values to strings to align it on one type: dict[str, str]
1031+
# spaces have to be removed as setInput() could take list of tuples as input and spaces would
1032+
str_val = str(val).replace(' ', '')
10311033
if ' ' in key or ' ' in str_val:
10321034
raise ModelicaSystemError(f"Spaces not allowed in key/value pairs: {repr(key)} = {repr(val)}!")
10331035
input_data[key] = str_val
@@ -1184,9 +1186,11 @@ def setOptimizationOptions(self, optimizationOptions: str | list[str] | dict[str
11841186

11851187
def setInputs(self, name: str | list[str] | dict[str, Any]) -> bool:
11861188
"""
1187-
This method is used to set input values. It can be called:
1188-
with a sequence of input name and assigning corresponding values as arguments as show in the example below:
1189-
usage
1189+
This method is used to set input values. It can be called with a sequence of input name and assigning
1190+
corresponding values as arguments as show in the example below. Compared to other set*() methods this is a
1191+
special case as value could be a list of tuples - these are converted to a string in _prepare_input_data()
1192+
and restored here via ast.literal_eval().
1193+
11901194
>>> setInputs("Name=value") # depreciated
11911195
>>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
11921196
>>> setInputs(name={"Name1": "value1", "Name2": "value2"})
@@ -1195,7 +1199,11 @@ def setInputs(self, name: str | list[str] | dict[str, Any]) -> bool:
11951199

11961200
for key, val in inputdata.items():
11971201
if key in self.inputlist:
1202+
if not isinstance(val, str):
1203+
raise ModelicaSystemError(f"Invalid data in input for {repr(key)}: {repr(val)}")
1204+
11981205
val_evaluated = ast.literal_eval(val)
1206+
11991207
if isinstance(val_evaluated, (int, float)):
12001208
self.inputlist[key] = [(float(self.simulateOptions["startTime"]), float(val)),
12011209
(float(self.simulateOptions["stopTime"]), float(val))]

0 commit comments

Comments
 (0)