Skip to content

Commit 5bf0be2

Browse files
committed
handling of errors
1 parent 8949b49 commit 5bf0be2

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

pyat/at/latticetools/observablelist.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,15 @@ def __init__(self, obsiter):
4949
self.base = obsiter
5050

5151
def __next__(self):
52-
val = next(self.base)
53-
if isinstance(val, Exception):
54-
raise val
55-
return val
52+
return Observable.check_value(next(self.base))
5653

5754

5855
class _ObsResults(tuple):
5956
def __getitem__(self, item):
6057
if isinstance(item, slice):
6158
return _ObsResults(super().__getitem__(item))
6259
else:
63-
val = super().__getitem__(item)
64-
if isinstance(val, Exception):
65-
raise type(val)(val.args[0]) from val
66-
return val
60+
return Observable.check_value(super().__getitem__(item))
6761

6862
def __iter__(self):
6963
return _ObsResIter(super().__iter__())

pyat/at/latticetools/observables.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,16 @@ def check(self) -> bool:
353353
"""
354354
return self.value is not None
355355

356+
@staticmethod
357+
def check_value(value):
358+
if isinstance(value, Exception):
359+
raise type(value)(value.args[0]) from value
360+
return value
361+
356362
@property
357363
def value(self):
358364
"""Value of the observable."""
359-
val = self._value
360-
if isinstance(val, Exception):
361-
raise type(val)(val.args[0]) from val
362-
return val
365+
return self.check_value(self._value)
363366

364367
@property
365368
def weight(self):
@@ -528,7 +531,9 @@ def check(self):
528531
ok = super().check()
529532
shp = self._shape
530533
if ok and shp and shp[0] <= 0:
531-
raise AtError(f"Observable {self.name!r}: No location selected in the lattice.")
534+
raise AtError(
535+
f"Observable {self.name!r}: No location selected in the lattice."
536+
)
532537
return ok
533538

534539
def _all_lines(self):

0 commit comments

Comments
 (0)