@@ -472,12 +472,12 @@ def setTempDirectory(self, customBuildDirectory: Optional[str | os.PathLike | pa
472472 # create a unique temp directory for each session and build the model in that directory
473473 if customBuildDirectory is not None :
474474 if not os .path .exists (customBuildDirectory ):
475- raise IOError (customBuildDirectory , " does not exist" )
475+ raise IOError (f" { customBuildDirectory } does not exist" )
476476 tempdir = pathlib .Path (customBuildDirectory )
477477 else :
478478 tempdir = pathlib .Path (tempfile .mkdtemp ())
479479 if not tempdir .is_dir ():
480- raise IOError (tempdir , " cannot be created" )
480+ raise IOError (f" { tempdir } could not be created" )
481481
482482 logger .info ("Define tempdir as %s" , tempdir )
483483 exp = f'cd("{ tempdir .absolute ().as_posix ()} ")'
@@ -565,19 +565,57 @@ def xmlparse(self):
565565
566566 self .quantitiesList .append (scalar )
567567
568- def getQuantities (self , names = None ): # 3
568+ def getQuantities (self , names : Optional [ str | list [ str ]] = None ) -> list [ dict ]:
569569 """
570- This method returns list of dictionaries. It displays details of quantities such as name, value, changeable, and description, where changeable means if value for corresponding quantity name is changeable or not. It can be called :
571- usage:
572- >>> getQuantities()
573- >>> getQuantities("Name1")
574- >>> getQuantities(["Name1","Name2"])
570+ This method returns list of dictionaries. It displays details of
571+ quantities such as name, value, changeable, and description.
572+
573+ Examples:
574+ >>> mod.getQuantities()
575+ [
576+ {
577+ 'alias': 'noAlias',
578+ 'aliasvariable': None,
579+ 'causality': 'local',
580+ 'changeable': 'true',
581+ 'description': None,
582+ 'max': None,
583+ 'min': None,
584+ 'name': 'x',
585+ 'start': '1.0',
586+ 'unit': None,
587+ 'variability': 'continuous',
588+ },
589+ {
590+ 'name': 'der(x)',
591+ # ...
592+ },
593+ # ...
594+ ]
595+
596+ >>> getQuantities("y")
597+ [{
598+ 'name': 'y', # ...
599+ }]
600+
601+ >>> getQuantities(["y","x"])
602+ [
603+ {
604+ 'name': 'y', # ...
605+ },
606+ {
607+ 'name': 'x', # ...
608+ }
609+ ]
575610 """
576611 if names is None :
577612 return self .quantitiesList
578613
579614 if isinstance (names , str ):
580- return [x for x in self .quantitiesList if x ["name" ] == names ]
615+ r = [x for x in self .quantitiesList if x ["name" ] == names ]
616+ if r == []:
617+ raise KeyError (names )
618+ return r
581619
582620 if isinstance (names , list ):
583621 return [x for y in names for x in self .quantitiesList if x ["name" ] == y ]
@@ -597,10 +635,10 @@ def getContinuous(self, names=None): # 4
597635 return self .continuouslist
598636
599637 if isinstance (names , str ):
600- return [self .continuouslist . get ( names , "NotExist" ) ]
638+ return [self .continuouslist [ names ] ]
601639
602640 if isinstance (names , list ):
603- return [self .continuouslist . get ( x , "NotExist" ) for x in names ]
641+ return [self .continuouslist [ x ] for x in names ]
604642 else :
605643 if names is None :
606644 for i in self .continuouslist :
@@ -615,7 +653,7 @@ def getContinuous(self, names=None): # 4
615653 if names in self .continuouslist :
616654 value = self .getSolutions (names )
617655 self .continuouslist [names ] = value [0 ][- 1 ]
618- return [self .continuouslist . get ( names ) ]
656+ return [self .continuouslist [ names ] ]
619657 else :
620658 raise ModelicaSystemError (f"{ names } is not continuous" )
621659
@@ -657,9 +695,9 @@ def getParameters(self, names: Optional[str | list[str]] = None) -> dict[str, st
657695 if names is None :
658696 return self .paramlist
659697 elif isinstance (names , str ):
660- return [self .paramlist . get ( names , "NotExist" ) ]
698+ return [self .paramlist [ names ] ]
661699 elif isinstance (names , list ):
662- return [self .paramlist . get ( x , "NotExist" ) for x in names ]
700+ return [self .paramlist [ x ] for x in names ]
663701
664702 raise ModelicaSystemError ("Unhandled input for getParameters()" )
665703
@@ -687,15 +725,13 @@ def getInputs(self, names: Optional[str | list[str]] = None) -> dict | list: #
687725 [[(0.0, 0.0), (1.0, 1.0)]]
688726 >>> mod.getInputs(["Name1","Name2"])
689727 [[(0.0, 0.0), (1.0, 1.0)], None]
690- >>> mod.getInputs("ThisInputDoesNotExist")
691- ['NotExist']
692728 """
693729 if names is None :
694730 return self .inputlist
695731 elif isinstance (names , str ):
696- return [self .inputlist . get ( names , "NotExist" ) ]
732+ return [self .inputlist [ names ] ]
697733 elif isinstance (names , list ):
698- return [self .inputlist . get ( x , "NotExist" ) for x in names ]
734+ return [self .inputlist [ x ] for x in names ]
699735
700736 raise ModelicaSystemError ("Unhandled input for getInputs()" )
701737
@@ -725,8 +761,6 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
725761 ['-0.4']
726762 >>> mod.getOutputs(["out1","out2"])
727763 ['-0.4', '1.2']
728- >>> mod.getOutputs("ThisOutputDoesNotExist")
729- ['NotExist']
730764
731765 After simulate():
732766 >>> mod.getOutputs()
@@ -740,9 +774,9 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
740774 if names is None :
741775 return self .outputlist
742776 elif isinstance (names , str ):
743- return [self .outputlist . get ( names , "NotExist" ) ]
777+ return [self .outputlist [ names ] ]
744778 else :
745- return [self .outputlist . get ( x , "NotExist" ) for x in names ]
779+ return [self .outputlist [ x ] for x in names ]
746780 else :
747781 if names is None :
748782 for i in self .outputlist :
@@ -753,9 +787,9 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
753787 if names in self .outputlist :
754788 value = self .getSolutions (names )
755789 self .outputlist [names ] = value [0 ][- 1 ]
756- return [self .outputlist . get ( names ) ]
790+ return [self .outputlist [ names ] ]
757791 else :
758- return names , " is not Output"
792+ raise KeyError ( names )
759793 elif isinstance (names , list ):
760794 valuelist = []
761795 for i in names :
@@ -764,7 +798,7 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
764798 self .outputlist [i ] = value [0 ][- 1 ]
765799 valuelist .append (value [0 ][- 1 ])
766800 else :
767- return i , "is not Output"
801+ raise KeyError ( i )
768802 return valuelist
769803
770804 raise ModelicaSystemError ("Unhandled input for getOutputs()" )
@@ -781,9 +815,9 @@ def getSimulationOptions(self, names=None): # 8
781815 if names is None :
782816 return self .simulateOptions
783817 elif isinstance (names , str ):
784- return [self .simulateOptions . get ( names , "NotExist" ) ]
818+ return [self .simulateOptions [ names ] ]
785819 elif isinstance (names , list ):
786- return [self .simulateOptions . get ( x , "NotExist" ) for x in names ]
820+ return [self .simulateOptions [ x ] for x in names ]
787821
788822 raise ModelicaSystemError ("Unhandled input for getSimulationOptions()" )
789823
@@ -799,9 +833,9 @@ def getLinearizationOptions(self, names=None): # 9
799833 if names is None :
800834 return self .linearOptions
801835 elif isinstance (names , str ):
802- return [self .linearOptions . get ( names , "NotExist" ) ]
836+ return [self .linearOptions [ names ] ]
803837 elif isinstance (names , list ):
804- return [self .linearOptions . get ( x , "NotExist" ) for x in names ]
838+ return [self .linearOptions [ x ] for x in names ]
805839
806840 raise ModelicaSystemError ("Unhandled input for getLinearizationOptions()" )
807841
@@ -815,9 +849,9 @@ def getOptimizationOptions(self, names=None): # 10
815849 if names is None :
816850 return self .optimizeOptions
817851 elif isinstance (names , str ):
818- return [self .optimizeOptions . get ( names , "NotExist" ) ]
852+ return [self .optimizeOptions [ names ] ]
819853 elif isinstance (names , list ):
820- return [self .optimizeOptions . get ( x , "NotExist" ) for x in names ]
854+ return [self .optimizeOptions [ x ] for x in names ]
821855
822856 raise ModelicaSystemError ("Unhandled input for getOptimizationOptions()" )
823857
@@ -1236,8 +1270,10 @@ def load_module_from_path(module_name, file_path):
12361270 return module_def
12371271
12381272 if self .xmlFile is None :
1239- raise IOError ("Linearization cannot be performed as the model is not build, "
1240- "use ModelicaSystem() to build the model first" )
1273+ raise ModelicaSystemError (
1274+ "Linearization cannot be performed as the model is not build, "
1275+ "use ModelicaSystem() to build the model first"
1276+ )
12411277
12421278 om_cmd = ModelicaSystemCmd (runpath = self .tempdir , modelname = self .modelName , timeout = timeout )
12431279
0 commit comments