Skip to content

Commit 241615d

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 3b0c9d9 commit 241615d

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

10351035
if isinstance(raw_input, dict):
10361036
for key, val in raw_input.items():
1037-
str_val = str(val)
1037+
# convert all values to strings to align it on one type: dict[str, str]
1038+
# spaces have to be removed as setInput() could take list of tuples as input and spaces would
1039+
str_val = str(val).replace(' ', '')
10381040
if ' ' in key or ' ' in str_val:
10391041
raise ModelicaSystemError(f"Spaces not allowed in key/value pairs: {repr(key)} = {repr(val)}!")
10401042
input_data[key] = str_val
@@ -1191,9 +1193,11 @@ def setOptimizationOptions(self, optimizationOptions: str | list[str] | dict[str
11911193

11921194
def setInputs(self, name: str | list[str] | dict[str, Any]) -> bool:
11931195
"""
1194-
This method is used to set input values. It can be called:
1195-
with a sequence of input name and assigning corresponding values as arguments as show in the example below:
1196-
usage
1196+
This method is used to set input values. It can be called with a sequence of input name and assigning
1197+
corresponding values as arguments as show in the example below. Compared to other set*() methods this is a
1198+
special case as value could be a list of tuples - these are converted to a string in _prepare_input_data()
1199+
and restored here via ast.literal_eval().
1200+
11971201
>>> setInputs("Name=value") # depreciated
11981202
>>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
11991203
>>> setInputs(name={"Name1": "value1", "Name2": "value2"})
@@ -1202,7 +1206,11 @@ def setInputs(self, name: str | list[str] | dict[str, Any]) -> bool:
12021206

12031207
for key, val in inputdata.items():
12041208
if key in self.inputlist:
1209+
if not isinstance(val, str):
1210+
raise ModelicaSystemError(f"Invalid data in input for {repr(key)}: {repr(val)}")
1211+
12051212
val_evaluated = ast.literal_eval(val)
1213+
12061214
if isinstance(val_evaluated, (int, float)):
12071215
self.inputlist[key] = [(float(self.simulateOptions["startTime"]), float(val)),
12081216
(float(self.simulateOptions["stopTime"]), float(val))]

0 commit comments

Comments
 (0)