Skip to content

Commit 30fa7a6

Browse files
committed
Merge branch 'remove_deprecated-ModelicaSystem_rewrite_set_functions' into v5.0.0-syntron
2 parents 8eb2dd8 + ba56397 commit 30fa7a6

2 files changed

Lines changed: 17 additions & 75 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 13 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,6 @@ def getSolutions(
11961196

11971197
@staticmethod
11981198
def _prepare_input_data(
1199-
input_args: Any,
12001199
input_kwargs: dict[str, Any],
12011200
) -> dict[str, str]:
12021201
"""
@@ -1215,28 +1214,6 @@ def prepare_str(str_in: str) -> dict[str, str]:
12151214

12161215
input_data: dict[str, str] = {}
12171216

1218-
for input_arg in input_args:
1219-
if isinstance(input_arg, str):
1220-
warnings.warn(message="The definition of values to set should use a dictionary, "
1221-
"i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which "
1222-
"use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]",
1223-
category=DeprecationWarning,
1224-
stacklevel=3)
1225-
input_data = input_data | prepare_str(input_arg)
1226-
elif isinstance(input_arg, list):
1227-
warnings.warn(message="The definition of values to set should use a dictionary, "
1228-
"i.e. {'key1': 'val1', 'key2': 'val2', ...}. Please convert all cases which "
1229-
"use a string ('key=val') or list ['key1=val1', 'key2=val2', ...]",
1230-
category=DeprecationWarning,
1231-
stacklevel=3)
1232-
1233-
for item in input_arg:
1234-
if not isinstance(item, str):
1235-
raise ModelicaSystemError(f"Invalid input data type for set*() function: {type(item)}!")
1236-
input_data = input_data | prepare_str(item)
1237-
else:
1238-
raise ModelicaSystemError(f"Invalid input data type for set*() function: {type(input_arg)}!")
1239-
12401217
if len(input_kwargs):
12411218
for key, val in input_kwargs.items():
12421219
# ensure all values are strings to align it on one type: dict[str, str]
@@ -1310,21 +1287,15 @@ def isParameterChangeable(
13101287

13111288
def setContinuous(
13121289
self,
1313-
*args: Any,
13141290
**kwargs: dict[str, Any],
13151291
) -> bool:
13161292
"""
1317-
This method is used to set continuous values. It can be called:
1318-
with a sequence of continuous name and assigning corresponding values as arguments as show in the example below:
1319-
usage
1320-
>>> setContinuous("Name=value") # depreciated
1321-
>>> setContinuous(["Name1=value1","Name2=value2"]) # depreciated
1322-
1293+
This method is used to set continuous values.
13231294
>>> setContinuous(Name1="value1", Name2="value2")
13241295
>>> param = {"Name1": "value1", "Name2": "value2"}
13251296
>>> setContinuous(**param)
13261297
"""
1327-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1298+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
13281299

13291300
return self._set_method_helper(
13301301
inputdata=inputdata,
@@ -1334,21 +1305,15 @@ def setContinuous(
13341305

13351306
def setParameters(
13361307
self,
1337-
*args: Any,
13381308
**kwargs: dict[str, Any],
13391309
) -> bool:
13401310
"""
1341-
This method is used to set parameter values. It can be called:
1342-
with a sequence of parameter name and assigning corresponding value as arguments as show in the example below:
1343-
usage
1344-
>>> setParameters("Name=value") # depreciated
1345-
>>> setParameters(["Name1=value1","Name2=value2"]) # depreciated
1346-
1311+
This method is used to set parameter values.
13471312
>>> setParameters(Name1="value1", Name2="value2")
13481313
>>> param = {"Name1": "value1", "Name2": "value2"}
13491314
>>> setParameters(**param)
13501315
"""
1351-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1316+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
13521317

13531318
return self._set_method_helper(
13541319
inputdata=inputdata,
@@ -1358,22 +1323,15 @@ def setParameters(
13581323

13591324
def setSimulationOptions(
13601325
self,
1361-
*args: Any,
13621326
**kwargs: dict[str, Any],
13631327
) -> bool:
13641328
"""
1365-
This method is used to set simulation options. It can be called:
1366-
with a sequence of simulation options name and assigning corresponding values as arguments as show in the
1367-
example below:
1368-
usage
1369-
>>> setSimulationOptions("Name=value") # depreciated
1370-
>>> setSimulationOptions(["Name1=value1","Name2=value2"]) # depreciated
1371-
1329+
This method is used to set simulation options.
13721330
>>> setSimulationOptions(Name1="value1", Name2="value2")
13731331
>>> param = {"Name1": "value1", "Name2": "value2"}
13741332
>>> setSimulationOptions(**param)
13751333
"""
1376-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1334+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
13771335

13781336
return self._set_method_helper(
13791337
inputdata=inputdata,
@@ -1383,22 +1341,15 @@ def setSimulationOptions(
13831341

13841342
def setLinearizationOptions(
13851343
self,
1386-
*args: Any,
13871344
**kwargs: dict[str, Any],
13881345
) -> bool:
13891346
"""
1390-
This method is used to set linearization options. It can be called:
1391-
with a sequence of linearization options name and assigning corresponding value as arguments as show in the
1392-
example below
1393-
usage
1394-
>>> setLinearizationOptions("Name=value") # depreciated
1395-
>>> setLinearizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1396-
1347+
This method is used to set linearization options.
13971348
>>> setLinearizationOptions(Name1="value1", Name2="value2")
13981349
>>> param = {"Name1": "value1", "Name2": "value2"}
13991350
>>> setLinearizationOptions(**param)
14001351
"""
1401-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1352+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
14021353

14031354
return self._set_method_helper(
14041355
inputdata=inputdata,
@@ -1408,22 +1359,18 @@ def setLinearizationOptions(
14081359

14091360
def setOptimizationOptions(
14101361
self,
1411-
*args: Any,
14121362
**kwargs: dict[str, Any],
14131363
) -> bool:
14141364
"""
14151365
This method is used to set optimization options. It can be called:
14161366
with a sequence of optimization options name and assigning corresponding values as arguments as show in the
14171367
example below:
14181368
usage
1419-
>>> setOptimizationOptions("Name=value") # depreciated
1420-
>>> setOptimizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1421-
14221369
>>> setOptimizationOptions(Name1="value1", Name2="value2")
14231370
>>> param = {"Name1": "value1", "Name2": "value2"}
14241371
>>> setOptimizationOptions(**param)
14251372
"""
1426-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1373+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
14271374

14281375
return self._set_method_helper(
14291376
inputdata=inputdata,
@@ -1433,23 +1380,18 @@ def setOptimizationOptions(
14331380

14341381
def setInputs(
14351382
self,
1436-
*args: Any,
14371383
**kwargs: dict[str, Any],
14381384
) -> bool:
14391385
"""
1440-
This method is used to set input values. It can be called with a sequence of input name and assigning
1441-
corresponding values as arguments as show in the example below. Compared to other set*() methods this is a
1442-
special case as value could be a list of tuples - these are converted to a string in _prepare_input_data()
1443-
and restored here via ast.literal_eval().
1444-
1445-
>>> setInputs("Name=value") # depreciated
1446-
>>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
1386+
This method is used to set input values.
14471387
1388+
Compared to other set*() methods this is a special case as value could be a list of tuples - these are
1389+
converted to a string in _prepare_input_data() and restored here via ast.literal_eval().
14481390
>>> setInputs(Name1="value1", Name2="value2")
14491391
>>> param = {"Name1": "value1", "Name2": "value2"}
14501392
>>> setInputs(**param)
14511393
"""
1452-
inputdata = self._prepare_input_data(input_args=args, input_kwargs=kwargs)
1394+
inputdata = self._prepare_input_data(input_kwargs=kwargs)
14531395

14541396
for key, val in inputdata.items():
14551397
if key not in self._inputs:

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
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)