From 751d09836c5d0710552073f265694e14c8599a32 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Thu, 7 Apr 2022 13:20:56 -0500 Subject: [PATCH] Make 'a' and 'z' properties read-only --- openmc/data/kalbach_mann.py | 52 ++++------------------ tests/unit_tests/test_data_kalbach_mann.py | 8 ++-- 2 files changed, 13 insertions(+), 47 deletions(-) diff --git a/openmc/data/kalbach_mann.py b/openmc/data/kalbach_mann.py index 8d1a5c2e16..4c6b89e41f 100644 --- a/openmc/data/kalbach_mann.py +++ b/openmc/data/kalbach_mann.py @@ -43,11 +43,17 @@ class AtomicRepresentation(EqualityMixin): """ def __init__(self, z, a): - self._consistency_check(z, a) + # Sanity checks on values + cv.check_type('z', z, Integral) + cv.check_greater_than('z', z, 0, equality=True) + cv.check_type('a', a, Integral) + cv.check_greater_than('a', a, 0, equality=True) + if z > a: + raise ValueError(f"Number of protons ({z}) must be less than or " + f"equal to number of nucleons ({a}).") + self._z = z self._a = a - self.z = self._z - self.a = self._a def __add__(self, other): """Adds two AtomicRepresentations. @@ -67,12 +73,10 @@ class AtomicRepresentation(EqualityMixin): @property def a(self): - self._consistency_check(self._z, self._a) return self._a @property def z(self): - self._consistency_check(self._z, self._a) return self._z @property @@ -83,44 +87,6 @@ class AtomicRepresentation(EqualityMixin): def za(self): return self.z * 1000 + self.a - @a.setter - def a(self, an): - cv.check_type('a', an, Integral) - cv.check_greater_than('a', an, 0, equality=True) - self._consistency_check(self._z, an) - self._a = an - - @z.setter - def z(self, zn): - cv.check_type('z', zn, Integral) - cv.check_greater_than('z', zn, 0, equality=True) - self._consistency_check(zn, self._a) - self._z = zn - - @staticmethod - def _consistency_check(z, a): - """Simple consistency check. - - Parameters - ---------- - z : int - Number of protons (atomic number) - a : int - Number of nucleons (mass number) - - Raises - ------ - IOError: - When the number of protons (z) declared is higher than the number - of nucleons (a) - - """ - if z > a: - raise IOError( - "Number of protons (%i) incompatible with number of " - "nucleons (%i)" % (z, a) - ) - @classmethod def from_za(cls, za): """Instantiates an AtomicRepresentation from a ZA identifier. diff --git a/tests/unit_tests/test_data_kalbach_mann.py b/tests/unit_tests/test_data_kalbach_mann.py index 36282f7c59..bd540addb3 100644 --- a/tests/unit_tests/test_data_kalbach_mann.py +++ b/tests/unit_tests/test_data_kalbach_mann.py @@ -75,15 +75,15 @@ def test_atomic_representation(neutron, triton, b10, c12, c13, na23): assert triton.za == 1003 # Test instanciation errors - with pytest.raises(IOError): + with pytest.raises(ValueError): AtomicRepresentation(z=5, a=1) with pytest.raises(ValueError): AtomicRepresentation(z=-1, a=1) - with pytest.raises(IOError): + with pytest.raises(ValueError): AtomicRepresentation(z=5, a=0) - with pytest.raises(IOError): + with pytest.raises(ValueError): AtomicRepresentation(z=5, a=-2) - with pytest.raises(OSError): + with pytest.raises(ValueError): neutron - triton