Skip to content

Commit 459e28d

Browse files
committed
Merge branch 'remove_deprecated-ModelicaSystem_rewrite_set_functions' into v5.0.0-syntron4
2 parents 5247013 + 756f54c commit 459e28d

2 files changed

Lines changed: 18 additions & 89 deletions

File tree

OMPython/ModelicaSystem.py

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

12151215
@staticmethod
12161216
def _prepare_input_data(
1217-
input_args: Any,
12181217
input_kwargs: dict[str, Any],
12191218
) -> dict[str, str]:
12201219
"""
12211220
Convert raw input to a structured dictionary {'key1': 'value1', 'key2': 'value2'}.
12221221
"""
12231222

1224-
def prepare_str(str_in: str) -> dict[str, str]:
1225-
str_in = str_in.replace(" ", "")
1226-
key_val_list: list[str] = str_in.split("=")
1227-
if len(key_val_list) != 2:
1228-
raise ModelicaSystemError(f"Invalid 'key=value' pair: {str_in}")
1229-
1230-
input_data_from_str: dict[str, str] = {key_val_list[0]: key_val_list[1]}
1231-
1232-
return input_data_from_str
1233-
12341223
input_data: dict[str, str] = {}
1235-
1236-
for input_arg in input_args:
1237-
if isinstance(input_arg, str):
1238-
warnings.warn(message="The definition of values to set should use a dictionary, "
1239-
"i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which "
1240-
"use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]",
1241-
category=DeprecationWarning,
1242-
stacklevel=3)
1243-
input_data = input_data | prepare_str(input_arg)
1244-
elif isinstance(input_arg, list):
1245-
warnings.warn(message="The definition of values to set should use a dictionary, "
1246-
"i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which "
1247-
"use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]",
1248-
category=DeprecationWarning,
1249-
stacklevel=3)
1250-
1251-
for item in input_arg:
1252-
if not isinstance(item, str):
1253-
raise ModelicaSystemError(f"Invalid input data type for set*() function: {type(item)}!")
1254-
input_data = input_data | prepare_str(item)
1255-
elif isinstance(input_arg, dict):
1256-
input_data = input_data | input_arg
1257-
else:
1258-
raise ModelicaSystemError(f"Invalid input data type for set*() function: {type(input_arg)}!")
1259-
12601224
if len(input_kwargs):
12611225
for key, val in input_kwargs.items():
12621226
# ensure all values are strings to align it on one type: dict[str, str]
@@ -1330,21 +1294,15 @@ def isParameterChangeable(
13301294

13311295
def setContinuous(
13321296
self,
1333-
*args: Any,
13341297
**kwargs: dict[str, Any],
13351298
) -> bool:
13361299
"""
1337-
This method is used to set continuous values. It can be called:
1338-
with a sequence of continuous name and assigning corresponding values as arguments as show in the example below:
1339-
usage
1340-
>>> setContinuous("Name=value") # depreciated
1341-
>>> setContinuous(["Name1=value1","Name2=value2"]) # depreciated
1342-
1300+
This method is used to set continuous values.
13431301
>>> setContinuous(Name1="value1", Name2="value2")
13441302
>>> param = {"Name1": "value1", "Name2": "value2"}
13451303
>>> setContinuous(**param)
13461304
"""
1347-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1305+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
13481306

13491307
return self._set_method_helper(
13501308
inputdata=inputdata,
@@ -1354,21 +1312,15 @@ def setContinuous(
13541312

13551313
def setParameters(
13561314
self,
1357-
*args: Any,
13581315
**kwargs: dict[str, Any],
13591316
) -> bool:
13601317
"""
1361-
This method is used to set parameter values. It can be called:
1362-
with a sequence of parameter name and assigning corresponding value as arguments as show in the example below:
1363-
usage
1364-
>>> setParameters("Name=value") # depreciated
1365-
>>> setParameters(["Name1=value1","Name2=value2"]) # depreciated
1366-
1318+
This method is used to set parameter values.
13671319
>>> setParameters(Name1="value1", Name2="value2")
13681320
>>> param = {"Name1": "value1", "Name2": "value2"}
13691321
>>> setParameters(**param)
13701322
"""
1371-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1323+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
13721324

13731325
return self._set_method_helper(
13741326
inputdata=inputdata,
@@ -1378,22 +1330,15 @@ def setParameters(
13781330

13791331
def setSimulationOptions(
13801332
self,
1381-
*args: Any,
13821333
**kwargs: dict[str, Any],
13831334
) -> bool:
13841335
"""
1385-
This method is used to set simulation options. It can be called:
1386-
with a sequence of simulation options name and assigning corresponding values as arguments as show in the
1387-
example below:
1388-
usage
1389-
>>> setSimulationOptions("Name=value") # depreciated
1390-
>>> setSimulationOptions(["Name1=value1","Name2=value2"]) # depreciated
1391-
1336+
This method is used to set simulation options.
13921337
>>> setSimulationOptions(Name1="value1", Name2="value2")
13931338
>>> param = {"Name1": "value1", "Name2": "value2"}
13941339
>>> setSimulationOptions(**param)
13951340
"""
1396-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1341+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
13971342

13981343
return self._set_method_helper(
13991344
inputdata=inputdata,
@@ -1403,22 +1348,15 @@ def setSimulationOptions(
14031348

14041349
def setLinearizationOptions(
14051350
self,
1406-
*args: Any,
14071351
**kwargs: dict[str, Any],
14081352
) -> bool:
14091353
"""
1410-
This method is used to set linearization options. It can be called:
1411-
with a sequence of linearization options name and assigning corresponding value as arguments as show in the
1412-
example below
1413-
usage
1414-
>>> setLinearizationOptions("Name=value") # depreciated
1415-
>>> setLinearizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1416-
1354+
This method is used to set linearization options.
14171355
>>> setLinearizationOptions(Name1="value1", Name2="value2")
14181356
>>> param = {"Name1": "value1", "Name2": "value2"}
14191357
>>> setLinearizationOptions(**param)
14201358
"""
1421-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1359+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
14221360

14231361
return self._set_method_helper(
14241362
inputdata=inputdata,
@@ -1428,22 +1366,18 @@ def setLinearizationOptions(
14281366

14291367
def setOptimizationOptions(
14301368
self,
1431-
*args: Any,
14321369
**kwargs: dict[str, Any],
14331370
) -> bool:
14341371
"""
14351372
This method is used to set optimization options. It can be called:
14361373
with a sequence of optimization options name and assigning corresponding values as arguments as show in the
14371374
example below:
14381375
usage
1439-
>>> setOptimizationOptions("Name=value") # depreciated
1440-
>>> setOptimizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1441-
14421376
>>> setOptimizationOptions(Name1="value1", Name2="value2")
14431377
>>> param = {"Name1": "value1", "Name2": "value2"}
14441378
>>> setOptimizationOptions(**param)
14451379
"""
1446-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1380+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
14471381

14481382
return self._set_method_helper(
14491383
inputdata=inputdata,
@@ -1453,23 +1387,18 @@ def setOptimizationOptions(
14531387

14541388
def setInputs(
14551389
self,
1456-
*args: Any,
14571390
**kwargs: dict[str, Any],
14581391
) -> bool:
14591392
"""
1460-
This method is used to set input values. It can be called with a sequence of input name and assigning
1461-
corresponding values as arguments as show in the example below. Compared to other set*() methods this is a
1462-
special case as value could be a list of tuples - these are converted to a string in _prepare_input_data()
1463-
and restored here via ast.literal_eval().
1464-
1465-
>>> setInputs("Name=value") # depreciated
1466-
>>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
1393+
This method is used to set input values.
14671394
1395+
Compared to other set*() methods this is a special case as value could be a list of tuples - these are
1396+
converted to a string in _prepare_input_data() and restored here via ast.literal_eval().
14681397
>>> setInputs(Name1="value1", Name2="value2")
14691398
>>> param = {"Name1": "value1", "Name2": "value2"}
14701399
>>> setInputs(**param)
14711400
"""
1472-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1401+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
14731402

14741403
for key, val in inputdata.items():
14751404
if key not in self._inputs:
@@ -2005,7 +1934,7 @@ def prepare(self) -> int:
20051934
}
20061935
)
20071936

2008-
self._mod.setParameters(sim_param_non_structural)
1937+
self._mod.setParameters(**sim_param_non_structural)
20091938
mscmd = self._mod.simulate_cmd(
20101939
result_file=resultfile,
20111940
timeout=self._timeout,

tests/test_ModelicaSystem.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def test_setParameters():
5858
model_name="BouncingBall",
5959
)
6060

61-
# method 1 (test depreciated variants)
62-
mod.setParameters("e=1.234")
63-
mod.setParameters(["g=321.0"])
61+
# method 1 (as kwarg)
62+
mod.setParameters(e=1.234)
63+
mod.setParameters(g=321.0)
6464
assert mod.getParameters("e") == ["1.234"]
6565
assert mod.getParameters("g") == ["321.0"]
6666
assert mod.getParameters() == {
@@ -70,7 +70,7 @@ def test_setParameters():
7070
with pytest.raises(KeyError):
7171
mod.getParameters("thisParameterDoesNotExist")
7272

73-
# method 2 (new style)
73+
# method 2 (as **kwarg)
7474
pvals = {"e": 21.3, "g": 0.12}
7575
mod.setParameters(**pvals)
7676
assert mod.getParameters() == {

0 commit comments

Comments
 (0)