Skip to content

Commit c912116

Browse files
syntronadeas31
andauthored
[ModelicaSystem] Cleanup & mypy (#292)
* [ModelicaSystem] fix exception handling * define specific exceptions * [ModelicaSystem] remove log message, the content is printed by self.requestedApi() * [ModelicaSystem] check for file using is_file() instead of exists() * [ModelicaSystem] do not promote 'parse=False' * [ModelicaSystem] fix mypy warnings --------- Co-authored-by: Adeel Asghar <adeel.asghar@liu.se>
1 parent efcb00b commit c912116

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

OMPython/ModelicaSystem.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ def __init__(
322322
customBuildDirectory: Optional[str | os.PathLike | pathlib.Path] = None,
323323
omhome: Optional[str] = None,
324324
session: Optional[OMCSessionZMQ] = None,
325-
build: Optional[bool] = True
326-
):
325+
build: Optional[bool] = True,
326+
) -> None:
327327
"""Initialize, load and build a model.
328328
329329
The constructor loads the model file and builds it, generating exe and
@@ -401,8 +401,8 @@ def __init__(
401401
self.inputFlag = False # for model with input quantity
402402
self.simulationFlag = False # if the model is simulated?
403403
self.outputFlag = False
404-
self.csvFile = '' # for storing inputs condition
405-
self.resultfile = None # for storing result file
404+
self.csvFile: Optional[pathlib.Path] = None # for storing inputs condition
405+
self.resultfile: Optional[pathlib.Path] = None # for storing result file
406406
self.variableFilter = variableFilter
407407

408408
if self.fileName is not None and not self.fileName.is_file(): # if file does not exist
@@ -427,7 +427,7 @@ def __init__(
427427
if build:
428428
self.buildModel(variableFilter)
429429

430-
def setCommandLineOptions(self, commandLineOptions: str):
430+
def setCommandLineOptions(self, commandLineOptions: Optional[str] = None):
431431
# set commandLineOptions if provided by users
432432
if commandLineOptions is None:
433433
return
@@ -462,7 +462,7 @@ def loadLibrary(self, lmodel: list):
462462
'1)["Modelica"]\n'
463463
'2)[("Modelica","3.2.3"), "PowerSystems"]\n')
464464

465-
def setTempDirectory(self, customBuildDirectory) -> pathlib.Path:
465+
def setTempDirectory(self, customBuildDirectory: Optional[str | os.PathLike | pathlib.Path] = None) -> pathlib.Path:
466466
# create a unique temp directory for each session and build the model in that directory
467467
if customBuildDirectory is not None:
468468
if not os.path.exists(customBuildDirectory):
@@ -482,22 +482,22 @@ def setTempDirectory(self, customBuildDirectory) -> pathlib.Path:
482482
def getWorkDirectory(self) -> pathlib.Path:
483483
return self.tempdir
484484

485-
def buildModel(self, variableFilter=None):
485+
def buildModel(self, variableFilter: Optional[str] = None):
486486
if variableFilter is not None:
487487
self.variableFilter = variableFilter
488488

489489
if self.variableFilter is not None:
490490
varFilter = f'variableFilter="{self.variableFilter}"'
491491
else:
492492
varFilter = 'variableFilter=".*"'
493-
logger.debug("varFilter=%s", varFilter)
493+
494494
buildModelResult = self.requestApi("buildModel", self.modelName, properties=varFilter)
495495
logger.debug("OM model build result: %s", buildModelResult)
496496

497497
self.xmlFile = pathlib.Path(buildModelResult[0]).parent / buildModelResult[1]
498498
self.xmlparse()
499499

500-
def sendExpression(self, expr, parsed=True):
500+
def sendExpression(self, expr: str, parsed: bool = True):
501501
try:
502502
retval = self.getconn.sendExpression(expr, parsed)
503503
except OMCSessionException as ex:
@@ -522,7 +522,7 @@ def requestApi(self, apiName, entity=None, properties=None): # 2
522522
return self.sendExpression(exp)
523523

524524
def xmlparse(self):
525-
if not self.xmlFile.exists():
525+
if not self.xmlFile.is_file():
526526
raise ModelicaSystemError(f"XML file not generated: {self.xmlFile}")
527527

528528
tree = ET.parse(self.xmlFile)
@@ -597,7 +597,7 @@ def getContinuous(self, names=None): # 4
597597
try:
598598
value = self.getSolutions(i)
599599
self.continuouslist[i] = value[0][-1]
600-
except OMCSessionException as ex:
600+
except (OMCSessionException, ModelicaSystemError) as ex:
601601
raise ModelicaSystemError(f"{i} could not be computed") from ex
602602
return self.continuouslist
603603

@@ -999,8 +999,8 @@ def isParameterChangeable(self, name, value):
999999
if q[0]["changeable"] == "false":
10001000
logger.verbose(f"setParameters() failed : It is not possible to set the following signal {repr(name)}. "
10011001
"It seems to be structural, final, protected or evaluated or has a non-constant binding, "
1002-
f"use sendExpression(\"setParameterValue({self.modelName}, {name}, {value})\", "
1003-
"parsed=False) and rebuild the model using buildModel() API")
1002+
f"use sendExpression(\"setParameterValue({self.modelName}, {name}, {value})\") "
1003+
"and rebuild the model using buildModel() API")
10041004
return False
10051005
return True
10061006

0 commit comments

Comments
 (0)