Skip to content

Commit fbf34cd

Browse files
committed
[ModelicaSystem] remove old style input for set*() functions
see PR #314 and PR #345
1 parent ff02f2a commit fbf34cd

1 file changed

Lines changed: 14 additions & 85 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 14 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,49 +1300,13 @@ def getSolutions(
13001300

13011301
@staticmethod
13021302
def _prepare_input_data(
1303-
input_args: Any,
13041303
input_kwargs: dict[str, Any],
13051304
) -> dict[str, str]:
13061305
"""
13071306
Convert raw input to a structured dictionary {'key1': 'value1', 'key2': 'value2'}.
13081307
"""
13091308

1310-
def prepare_str(str_in: str) -> dict[str, str]:
1311-
str_in = str_in.replace(" ", "")
1312-
key_val_list: list[str] = str_in.split("=")
1313-
if len(key_val_list) != 2:
1314-
raise ModelicaSystemError(f"Invalid 'key=value' pair: {str_in}")
1315-
1316-
input_data_from_str: dict[str, str] = {key_val_list[0]: key_val_list[1]}
1317-
1318-
return input_data_from_str
1319-
13201309
input_data: dict[str, str] = {}
1321-
1322-
for input_arg in input_args:
1323-
if isinstance(input_arg, str):
1324-
warnings.warn(message="The definition of values to set should use a dictionary, "
1325-
"i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which "
1326-
"use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]",
1327-
category=DeprecationWarning,
1328-
stacklevel=3)
1329-
input_data = input_data | prepare_str(input_arg)
1330-
elif isinstance(input_arg, list):
1331-
warnings.warn(message="The definition of values to set should use a dictionary, "
1332-
"i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which "
1333-
"use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]",
1334-
category=DeprecationWarning,
1335-
stacklevel=3)
1336-
1337-
for item in input_arg:
1338-
if not isinstance(item, str):
1339-
raise ModelicaSystemError(f"Invalid input data type for set*() function: {type(item)}!")
1340-
input_data = input_data | prepare_str(item)
1341-
elif isinstance(input_arg, dict):
1342-
input_data = input_data | input_arg
1343-
else:
1344-
raise ModelicaSystemError(f"Invalid input data type for set*() function: {type(input_arg)}!")
1345-
13461310
if len(input_kwargs):
13471311
for key, val in input_kwargs.items():
13481312
# ensure all values are strings to align it on one type: dict[str, str]
@@ -1420,21 +1384,15 @@ def isParameterChangeable(
14201384

14211385
def setContinuous(
14221386
self,
1423-
*args: Any,
14241387
**kwargs: dict[str, Any],
14251388
) -> bool:
14261389
"""
1427-
This method is used to set continuous values. It can be called:
1428-
with a sequence of continuous name and assigning corresponding values as arguments as show in the example below:
1429-
usage
1430-
>>> setContinuous("Name=value") # depreciated
1431-
>>> setContinuous(["Name1=value1","Name2=value2"]) # depreciated
1432-
1390+
This method is used to set continuous values.
14331391
>>> setContinuous(Name1="value1", Name2="value2")
14341392
>>> param = {"Name1": "value1", "Name2": "value2"}
14351393
>>> setContinuous(**param)
14361394
"""
1437-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1395+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
14381396

14391397
return self._set_method_helper(
14401398
inputdata=inputdata,
@@ -1444,21 +1402,15 @@ def setContinuous(
14441402

14451403
def setParameters(
14461404
self,
1447-
*args: Any,
14481405
**kwargs: dict[str, Any],
14491406
) -> bool:
14501407
"""
1451-
This method is used to set parameter values. It can be called:
1452-
with a sequence of parameter name and assigning corresponding value as arguments as show in the example below:
1453-
usage
1454-
>>> setParameters("Name=value") # depreciated
1455-
>>> setParameters(["Name1=value1","Name2=value2"]) # depreciated
1456-
1408+
This method is used to set parameter values.
14571409
>>> setParameters(Name1="value1", Name2="value2")
14581410
>>> param = {"Name1": "value1", "Name2": "value2"}
14591411
>>> setParameters(**param)
14601412
"""
1461-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1413+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
14621414

14631415
return self._set_method_helper(
14641416
inputdata=inputdata,
@@ -1468,22 +1420,15 @@ def setParameters(
14681420

14691421
def setSimulationOptions(
14701422
self,
1471-
*args: Any,
14721423
**kwargs: dict[str, Any],
14731424
) -> bool:
14741425
"""
1475-
This method is used to set simulation options. It can be called:
1476-
with a sequence of simulation options name and assigning corresponding values as arguments as show in the
1477-
example below:
1478-
usage
1479-
>>> setSimulationOptions("Name=value") # depreciated
1480-
>>> setSimulationOptions(["Name1=value1","Name2=value2"]) # depreciated
1481-
1426+
This method is used to set simulation options.
14821427
>>> setSimulationOptions(Name1="value1", Name2="value2")
14831428
>>> param = {"Name1": "value1", "Name2": "value2"}
14841429
>>> setSimulationOptions(**param)
14851430
"""
1486-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1431+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
14871432

14881433
return self._set_method_helper(
14891434
inputdata=inputdata,
@@ -1493,22 +1438,15 @@ def setSimulationOptions(
14931438

14941439
def setLinearizationOptions(
14951440
self,
1496-
*args: Any,
14971441
**kwargs: dict[str, Any],
14981442
) -> bool:
14991443
"""
1500-
This method is used to set linearization options. It can be called:
1501-
with a sequence of linearization options name and assigning corresponding value as arguments as show in the
1502-
example below
1503-
usage
1504-
>>> setLinearizationOptions("Name=value") # depreciated
1505-
>>> setLinearizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1506-
1444+
This method is used to set linearization options.
15071445
>>> setLinearizationOptions(Name1="value1", Name2="value2")
15081446
>>> param = {"Name1": "value1", "Name2": "value2"}
15091447
>>> setLinearizationOptions(**param)
15101448
"""
1511-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1449+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
15121450

15131451
return self._set_method_helper(
15141452
inputdata=inputdata,
@@ -1518,22 +1456,18 @@ def setLinearizationOptions(
15181456

15191457
def setOptimizationOptions(
15201458
self,
1521-
*args: Any,
15221459
**kwargs: dict[str, Any],
15231460
) -> bool:
15241461
"""
15251462
This method is used to set optimization options. It can be called:
15261463
with a sequence of optimization options name and assigning corresponding values as arguments as show in the
15271464
example below:
15281465
usage
1529-
>>> setOptimizationOptions("Name=value") # depreciated
1530-
>>> setOptimizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1531-
15321466
>>> setOptimizationOptions(Name1="value1", Name2="value2")
15331467
>>> param = {"Name1": "value1", "Name2": "value2"}
15341468
>>> setOptimizationOptions(**param)
15351469
"""
1536-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1470+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
15371471

15381472
return self._set_method_helper(
15391473
inputdata=inputdata,
@@ -1543,23 +1477,18 @@ def setOptimizationOptions(
15431477

15441478
def setInputs(
15451479
self,
1546-
*args: Any,
15471480
**kwargs: dict[str, Any],
15481481
) -> bool:
15491482
"""
1550-
This method is used to set input values. It can be called with a sequence of input name and assigning
1551-
corresponding values as arguments as show in the example below. Compared to other set*() methods this is a
1552-
special case as value could be a list of tuples - these are converted to a string in _prepare_input_data()
1553-
and restored here via ast.literal_eval().
1554-
1555-
>>> setInputs("Name=value") # depreciated
1556-
>>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
1483+
This method is used to set input values.
15571484
1485+
Compared to other set*() methods this is a special case as value could be a list of tuples - these are
1486+
converted to a string in _prepare_input_data() and restored here via ast.literal_eval().
15581487
>>> setInputs(Name1="value1", Name2="value2")
15591488
>>> param = {"Name1": "value1", "Name2": "value2"}
15601489
>>> setInputs(**param)
15611490
"""
1562-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1491+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
15631492

15641493
for key, val in inputdata.items():
15651494
if key not in self._inputs:
@@ -2099,7 +2028,7 @@ def prepare(self) -> int:
20992028
}
21002029
)
21012030

2102-
self._mod.setParameters(sim_param_non_structural)
2031+
self._mod.setParameters(**sim_param_non_structural)
21032032
mscmd = self._mod.simulate_cmd(
21042033
result_file=resultfile,
21052034
)

0 commit comments

Comments
 (0)