@@ -1117,17 +1117,7 @@ def getSolutions(self, varList: Optional[str | list[str]] = None, resultfile: Op
11171117 return np_res
11181118
11191119 @staticmethod
1120- def _strip_space (name ):
1121- if isinstance (name , str ):
1122- return name .replace (" " , "" )
1123-
1124- if isinstance (name , list ):
1125- return [x .replace (" " , "" ) for x in name ]
1126-
1127- raise ModelicaSystemError ("Unhandled input for strip_space()" )
1128-
11291120 def _prepare_inputdata (
1130- self ,
11311121 rawinput : str | list [str ] | dict [str , str | int | float ],
11321122 ) -> dict [str , str ]:
11331123 """
@@ -1166,72 +1156,65 @@ def prepare_str(str_in: str) -> dict[str, str]:
11661156 return inputdata
11671157
11681158 if isinstance (rawinput , dict ):
1169- inputdata = {key : str (val ) for key , val in rawinput .items ()}
1159+ for key , val in rawinput .items ():
1160+ str_val = str (val )
1161+ if ' ' in key or ' ' in str_val :
1162+ raise ModelicaSystemError (f"Spaces not allowed in key/value pairs: { repr (key )} = { repr (val )} !" )
1163+ inputdata [key ] = str_val
11701164
11711165 return inputdata
11721166
1173- def setMethodHelper (
1167+ def _set_method_helper (
11741168 self ,
1175- inputdata : str | list [ str ] | dict [str , str | int | float ],
1169+ inputdata : dict [str , str ],
11761170 classdata : dict [str , Any ],
11771171 datatype : str ,
11781172 overwritedata : Optional [dict [str , str ]] = None ,
11791173 ) -> bool :
11801174 """
1181- Helper function for setters.
1175+ Helper function for:
1176+ * setParameter()
1177+ * setContinuous()
1178+ * setSimulationOptions()
1179+ * setLinearizationOption()
1180+ * setOptimizationOption()
1181+ * setInputs()
11821182
1183- args1 - string or list of string given by user
1184- args2 - dict() containing the values of different variables(eg:, parameter,continuous,simulation parameters)
1185- args3 - function name (eg; continuous, parameter, simulation, linearization,optimization)
1186- args4 - dict() which stores the new override variables list,
1183+ Parameters
1184+ ----------
1185+ inputdata
1186+ string or list of string given by user
1187+ classdata
1188+ dict() containing the values of different variables (eg: parameter, continuous, simulation parameters)
1189+ datatype
1190+ type identifier (eg; continuous, parameter, simulation, linearization, optimization)
1191+ overwritedata
1192+ dict() which stores the new override variables list,
11871193 """
11881194
1189- # TODO: cleanup / data handling / ...
1190- # (1) args1: Optional[str | list[str]] -> convert to dict
1191- # (2) work on dict! inputs: dict[str, str | int | float | ?numbers.number?]
1192- # (3) handle function
1193- # (4) use it also for other functions with such an input, i.e. 'key=value' | ['key=value']
1194- # (5) include setInputs()
1195-
1196- def apply_single (key_val : str ):
1197- key_val_list = key_val .split ("=" )
1198- if len (key_val_list ) != 2 :
1199- raise ModelicaSystemError (f"Invalid key = value pair: { key_val } " )
1200-
1201- name = self ._strip_space (key_val_list [0 ])
1202- value = self ._strip_space (key_val_list [1 ])
1203-
1204- if name in classdata :
1205- if datatype == "parameter" and not self .isParameterChangeable (name ):
1206- logger .debug (f"It is not possible to set the parameter { repr (name )} . It seems to be "
1195+ inputdata_status : dict [str , bool ] = {}
1196+ for key , val in inputdata .items ():
1197+ status = False
1198+ if key in classdata :
1199+ if datatype == "parameter" and not self .isParameterChangeable (key ):
1200+ logger .debug (f"It is not possible to set the parameter { repr (key )} . It seems to be "
12071201 "structural, final, protected, evaluated or has a non-constant binding. "
12081202 "Use sendExpression(...) and rebuild the model using buildModel() API; example: "
12091203 "sendExpression(\" setParameterValue("
1210- f"{ self .modelName } , { name } , { value if value is not None else '<?value?>' } "
1204+ f"{ self .modelName } , { key } , { val if val is not None else '<?value?>' } "
12111205 ")\" ) " )
1212- return False
1213-
1214- classdata [name ] = value
1215- if overwritedata is not None :
1216- overwritedata [name ] = value
1217-
1218- return True
1219-
1206+ else :
1207+ classdata [key ] = val
1208+ if overwritedata is not None :
1209+ overwritedata [key ] = val
1210+ status = True
12201211 else :
12211212 raise ModelicaSystemError ("Unhandled case in setMethodHelper.apply_single() - "
1222- f"{ repr (name )} is not a { repr (datatype )} variable" )
1223-
1224- result = []
1225- if isinstance (inputdata , str ):
1226- result = [apply_single (inputdata )]
1213+ f"{ repr (key )} is not a { repr (datatype )} variable" )
12271214
1228- elif isinstance (inputdata , list ):
1229- result = []
1230- inputdata = self ._strip_space (inputdata )
1231- for var in inputdata :
1232- result .append (apply_single (var ))
1215+ inputdata_status [key ] = status
12331216
1234- return all (result )
1217+ return all (inputdata_status . values () )
12351218
12361219 def isParameterChangeable (
12371220 self ,
@@ -1250,11 +1233,14 @@ def setContinuous(
12501233 This method is used to set continuous values. It can be called:
12511234 with a sequence of continuous name and assigning corresponding values as arguments as show in the example below:
12521235 usage
1253- >>> setContinuous("Name=value")
1254- >>> setContinuous(["Name1=value1","Name2=value2"])
1236+ >>> setContinuous("Name=value") # depreciated
1237+ >>> setContinuous(["Name1=value1","Name2=value2"]) # depreciated
1238+ >>> setContinuous(cvals={"Name1": "value1", "Name2": "value2"})
12551239 """
1256- return self ._setMethodHelper (
1257- inputdata = cvals ,
1240+ inputdata = self ._prepare_inputdata (rawinput = cvals )
1241+
1242+ return self ._set_method_helper (
1243+ inputdata = inputdata ,
12581244 classdata = self .continuouslist ,
12591245 datatype = "continuous" ,
12601246 overwritedata = self .overridevariables )
@@ -1267,11 +1253,14 @@ def setParameters(
12671253 This method is used to set parameter values. It can be called:
12681254 with a sequence of parameter name and assigning corresponding value as arguments as show in the example below:
12691255 usage
1270- >>> setParameters("Name=value")
1271- >>> setParameters(["Name1=value1","Name2=value2"])
1256+ >>> setParameters("Name=value") # depreciated
1257+ >>> setParameters(["Name1=value1","Name2=value2"]) # depreciated
1258+ >>> setParameters(pvals={"Name1": "value1", "Name2": "value2"})
12721259 """
1273- return self ._setMethodHelper (
1274- inputdata = pvals ,
1260+ inputdata = self ._prepare_inputdata (rawinput = pvals )
1261+
1262+ return self ._set_method_helper (
1263+ inputdata = inputdata ,
12751264 classdata = self .paramlist ,
12761265 datatype = "parameter" ,
12771266 overwritedata = self .overridevariables )
@@ -1284,11 +1273,14 @@ def setSimulationOptions(
12841273 This method is used to set simulation options. It can be called:
12851274 with a sequence of simulation options name and assigning corresponding values as arguments as show in the example below:
12861275 usage
1287- >>> setSimulationOptions("Name=value")
1288- >>> setSimulationOptions(["Name1=value1","Name2=value2"])
1276+ >>> setSimulationOptions("Name=value") # depreciated
1277+ >>> setSimulationOptions(["Name1=value1","Name2=value2"]) # depreciated
1278+ >>> setSimulationOptions(simOptions={"Name1": "value1", "Name2": "value2"})
12891279 """
1290- return _self .setMethodHelper (
1291- inputdata = simOptions ,
1280+ inputdata = self ._prepare_inputdata (rawinput = simOptions )
1281+
1282+ return self ._set_method_helper (
1283+ inputdata = inputdata ,
12921284 classdata = self .simulateOptions ,
12931285 datatype = "simulation-option" ,
12941286 overwritedata = self .simoptionsoverride )
@@ -1301,11 +1293,14 @@ def setLinearizationOptions(
13011293 This method is used to set linearization options. It can be called:
13021294 with a sequence of linearization options name and assigning corresponding value as arguments as show in the example below
13031295 usage
1304- >>> setLinearizationOptions("Name=value")
1305- >>> setLinearizationOptions(["Name1=value1","Name2=value2"])
1296+ >>> setLinearizationOptions("Name=value") # depreciated
1297+ >>> setLinearizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1298+ >>> setLinearizationOptions(linearizationOtions={"Name1": "value1", "Name2": "value2"})
13061299 """
1307- return self ._setMethodHelper (
1308- inputdata = linearizationOptions ,
1300+ inputdata = self ._prepare_inputdata (rawinput = linearizationOptions )
1301+
1302+ return self ._set_method_helper (
1303+ inputdata = inputdata ,
13091304 classdata = self .linearOptions ,
13101305 datatype = "Linearization-option" ,
13111306 overwritedata = None )
@@ -1315,11 +1310,14 @@ def setOptimizationOptions(self, optimizationOptions: str | list[str] | dict[str
13151310 This method is used to set optimization options. It can be called:
13161311 with a sequence of optimization options name and assigning corresponding values as arguments as show in the example below:
13171312 usage
1318- >>> setOptimizationOptions("Name=value")
1319- >>> setOptimizationOptions(["Name1=value1","Name2=value2"])
1313+ >>> setOptimizationOptions("Name=value") # depreciated
1314+ >>> setOptimizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1315+ >>> setOptimizationOptions(optimizationOptions={"Name1": "value1", "Name2": "value2"})
13201316 """
1321- return self ._setMethodHelper (
1322- inputdata = optimizationOptions ,
1317+ inputdata = self ._prepare_inputdata (rawinput = optimizationOptions )
1318+
1319+ return self ._set_method_helper (
1320+ inputdata = inputdata ,
13231321 classdata = self .optimizeOptions ,
13241322 datatype = "optimization-option" ,
13251323 overwritedata = None )
@@ -1332,9 +1330,12 @@ def setInputs(
13321330 This method is used to set input values. It can be called:
13331331 with a sequence of input name and assigning corresponding values as arguments as show in the example below:
13341332 usage
1335- >>> setInputs("Name=value")
1336- >>> setInputs(["Name1=value1","Name2=value2"])
1333+ >>> setInputs("Name=value") # depreciated
1334+ >>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
1335+ >>> setInputs(name={"Name1": "value1", "Name2": "value2"})
13371336 """
1337+ # inputdata = self._prepare_inputdata(rawinput=name)
1338+
13381339 if isinstance (name , str ):
13391340 name1 : str = name
13401341 name1 = name1 .replace (" " , "" )
0 commit comments