Skip to content

Commit 83d5386

Browse files
committed
Make getters raise KeyError
... instead of returning weird values like "NotExist"
1 parent 2656786 commit 83d5386

2 files changed

Lines changed: 43 additions & 25 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,10 @@ def getQuantities(self, names=None): # 3
577577
return self.quantitiesList
578578

579579
if isinstance(names, str):
580-
return [x for x in self.quantitiesList if x["name"] == names]
580+
r = [x for x in self.quantitiesList if x["name"] == names]
581+
if r == []:
582+
raise KeyError(names)
583+
return r
581584

582585
if isinstance(names, list):
583586
return [x for y in names for x in self.quantitiesList if x["name"] == y]
@@ -597,10 +600,10 @@ def getContinuous(self, names=None): # 4
597600
return self.continuouslist
598601

599602
if isinstance(names, str):
600-
return [self.continuouslist.get(names, "NotExist")]
603+
return [self.continuouslist[names]]
601604

602605
if isinstance(names, list):
603-
return [self.continuouslist.get(x, "NotExist") for x in names]
606+
return [self.continuouslist[x] for x in names]
604607
else:
605608
if names is None:
606609
for i in self.continuouslist:
@@ -615,7 +618,7 @@ def getContinuous(self, names=None): # 4
615618
if names in self.continuouslist:
616619
value = self.getSolutions(names)
617620
self.continuouslist[names] = value[0][-1]
618-
return [self.continuouslist.get(names)]
621+
return [self.continuouslist[names]]
619622
else:
620623
raise ModelicaSystemError(f"{names} is not continuous")
621624

@@ -657,9 +660,9 @@ def getParameters(self, names: Optional[str | list[str]] = None) -> dict[str, st
657660
if names is None:
658661
return self.paramlist
659662
elif isinstance(names, str):
660-
return [self.paramlist.get(names, "NotExist")]
663+
return [self.paramlist[names]]
661664
elif isinstance(names, list):
662-
return [self.paramlist.get(x, "NotExist") for x in names]
665+
return [self.paramlist[x] for x in names]
663666

664667
raise ModelicaSystemError("Unhandled input for getParameters()")
665668

@@ -687,15 +690,13 @@ def getInputs(self, names: Optional[str | list[str]] = None) -> dict | list: #
687690
[[(0.0, 0.0), (1.0, 1.0)]]
688691
>>> mod.getInputs(["Name1","Name2"])
689692
[[(0.0, 0.0), (1.0, 1.0)], None]
690-
>>> mod.getInputs("ThisInputDoesNotExist")
691-
['NotExist']
692693
"""
693694
if names is None:
694695
return self.inputlist
695696
elif isinstance(names, str):
696-
return [self.inputlist.get(names, "NotExist")]
697+
return [self.inputlist[names]]
697698
elif isinstance(names, list):
698-
return [self.inputlist.get(x, "NotExist") for x in names]
699+
return [self.inputlist[x] for x in names]
699700

700701
raise ModelicaSystemError("Unhandled input for getInputs()")
701702

@@ -725,8 +726,6 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
725726
['-0.4']
726727
>>> mod.getOutputs(["out1","out2"])
727728
['-0.4', '1.2']
728-
>>> mod.getOutputs("ThisOutputDoesNotExist")
729-
['NotExist']
730729
731730
After simulate():
732731
>>> mod.getOutputs()
@@ -740,9 +739,9 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
740739
if names is None:
741740
return self.outputlist
742741
elif isinstance(names, str):
743-
return [self.outputlist.get(names, "NotExist")]
742+
return [self.outputlist[names]]
744743
else:
745-
return [self.outputlist.get(x, "NotExist") for x in names]
744+
return [self.outputlist[x] for x in names]
746745
else:
747746
if names is None:
748747
for i in self.outputlist:
@@ -753,9 +752,9 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
753752
if names in self.outputlist:
754753
value = self.getSolutions(names)
755754
self.outputlist[names] = value[0][-1]
756-
return [self.outputlist.get(names)]
755+
return [self.outputlist[names]]
757756
else:
758-
return names, " is not Output"
757+
raise KeyError(names)
759758
elif isinstance(names, list):
760759
valuelist = []
761760
for i in names:
@@ -764,7 +763,7 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
764763
self.outputlist[i] = value[0][-1]
765764
valuelist.append(value[0][-1])
766765
else:
767-
return i, "is not Output"
766+
raise KeyError(i)
768767
return valuelist
769768

770769
raise ModelicaSystemError("Unhandled input for getOutputs()")
@@ -781,9 +780,9 @@ def getSimulationOptions(self, names=None): # 8
781780
if names is None:
782781
return self.simulateOptions
783782
elif isinstance(names, str):
784-
return [self.simulateOptions.get(names, "NotExist")]
783+
return [self.simulateOptions[names]]
785784
elif isinstance(names, list):
786-
return [self.simulateOptions.get(x, "NotExist") for x in names]
785+
return [self.simulateOptions[x] for x in names]
787786

788787
raise ModelicaSystemError("Unhandled input for getSimulationOptions()")
789788

@@ -799,9 +798,9 @@ def getLinearizationOptions(self, names=None): # 9
799798
if names is None:
800799
return self.linearOptions
801800
elif isinstance(names, str):
802-
return [self.linearOptions.get(names, "NotExist")]
801+
return [self.linearOptions[names]]
803802
elif isinstance(names, list):
804-
return [self.linearOptions.get(x, "NotExist") for x in names]
803+
return [self.linearOptions[x] for x in names]
805804

806805
raise ModelicaSystemError("Unhandled input for getLinearizationOptions()")
807806

@@ -815,9 +814,9 @@ def getOptimizationOptions(self, names=None): # 10
815814
if names is None:
816815
return self.optimizeOptions
817816
elif isinstance(names, str):
818-
return [self.optimizeOptions.get(names, "NotExist")]
817+
return [self.optimizeOptions[names]]
819818
elif isinstance(names, list):
820-
return [self.optimizeOptions.get(x, "NotExist") for x in names]
819+
return [self.optimizeOptions[x] for x in names]
821820

822821
raise ModelicaSystemError("Unhandled input for getOptimizationOptions()")
823822

tests/test_ModelicaSystem.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def test_setParameters():
4343
"e": "1.234",
4444
"g": "321.0",
4545
}
46+
with pytest.raises(KeyError):
47+
mod.getParameters("thisParameterDoesNotExist")
4648

4749
# method 2
4850
mod.setParameters(["e=21.3", "g=0.12"])
@@ -52,6 +54,8 @@ def test_setParameters():
5254
}
5355
assert mod.getParameters(["e", "g"]) == ["21.3", "0.12"]
5456
assert mod.getParameters(["g", "e"]) == ["0.12", "21.3"]
57+
with pytest.raises(KeyError):
58+
mod.getParameters(["g", "thisParameterDoesNotExist"])
5559

5660

5761
def test_setSimulationOptions():
@@ -69,6 +73,8 @@ def test_setSimulationOptions():
6973
assert isinstance(d, dict)
7074
assert d["stopTime"] == "1.234"
7175
assert d["tolerance"] == "1.1e-08"
76+
with pytest.raises(KeyError):
77+
mod.getSimulationOptions("thisOptionDoesNotExist")
7278

7379
# method 2
7480
mod.setSimulationOptions(["stopTime=2.1", "tolerance=1.2e-08"])
@@ -125,7 +131,7 @@ def test_getSolutions(model_firstorder):
125131
assert "x" in sol_names
126132
assert "der(x)" in sol_names
127133
with pytest.raises(OMPython.ModelicaSystemError):
128-
mod.getSolutions("t") # variable 't' does not exist
134+
mod.getSolutions("thisVariableDoesNotExist")
129135
assert np.isclose(t[0], 0), "time does not start at 0"
130136
assert np.isclose(t[-1], stopTime), "time does not end at stopTime"
131137
x_analytical = x0 * np.exp(a*t)
@@ -262,11 +268,18 @@ def test_getters(tmp_path):
262268
},
263269
]
264270

271+
with pytest.raises(KeyError):
272+
mod.getQuantities("thisQuantityDoesNotExist")
273+
265274
assert mod.getInputs() == {}
275+
with pytest.raises(KeyError):
276+
mod.getInputs("thisInputDoesNotExist")
266277
# getOutputs before simulate()
267278
assert mod.getOutputs() == {'y': '-0.4'}
268279
assert mod.getOutputs("y") == ["-0.4"]
269280
assert mod.getOutputs(["y", "y"]) == ["-0.4", "-0.4"]
281+
with pytest.raises(KeyError):
282+
mod.getOutputs("thisOutputDoesNotExist")
270283

271284
# getContinuous before simulate():
272285
assert mod.getContinuous() == {
@@ -276,7 +289,8 @@ def test_getters(tmp_path):
276289
}
277290
assert mod.getContinuous("y") == ['-0.4']
278291
assert mod.getContinuous(["y", "x"]) == ['-0.4', '1.0']
279-
assert mod.getContinuous("a") == ["NotExist"] # a is a parameter
292+
with pytest.raises(KeyError):
293+
mod.getContinuous("a") # a is a parameter
280294

281295
stopTime = 1.0
282296
a = -0.5
@@ -293,6 +307,8 @@ def test_getters(tmp_path):
293307
assert np.isclose(d["y"], dx_analytical, 1e-4)
294308
assert mod.getOutputs("y") == [d["y"]]
295309
assert mod.getOutputs(["y", "y"]) == [d["y"], d["y"]]
310+
with pytest.raises(KeyError):
311+
mod.getOutputs("thisOutputDoesNotExist")
296312

297313
# getContinuous after simulate() should return values at end of simulation:
298314
with pytest.raises(OMPython.ModelicaSystemError):
@@ -307,6 +323,9 @@ def test_getters(tmp_path):
307323
assert mod.getContinuous("x") == [d["x"]]
308324
assert mod.getContinuous(["y", "x"]) == [d["y"], d["x"]]
309325

326+
with pytest.raises(OMPython.ModelicaSystemError):
327+
mod.getContinuous("a") # a is a parameter
328+
310329
with pytest.raises(OMPython.ModelicaSystemError):
311330
mod.setSimulationOptions("thisOptionDoesNotExist=3")
312331

0 commit comments

Comments
 (0)