diff --git a/openmc/element.py b/openmc/element.py index 8543500e9..386f9ac58 100644 --- a/openmc/element.py +++ b/openmc/element.py @@ -10,7 +10,7 @@ import openmc.checkvalue as cv from openmc.data import NATURAL_ABUNDANCE, atomic_mass -class Element(object): +class Element(str): """A natural element that auto-expands to add the isotopes of an element to a material in their natural abundance. Internally, the OpenMC Python API expands the natural element into isotopes only when the materials.xml file @@ -28,25 +28,14 @@ class Element(object): """ - def __init__(self, name=''): - # Initialize class attributes - self._name = '' - - # Set class attributes - self.name = name - - def __repr__(self): - return 'Element - {0}\n'.format(self._name) + def __new__(cls, name): + cv.check_type('element name', name, string_types) + cv.check_length('element name', name, 1, 2) + return super(Element, cls).__new__(cls, name) @property def name(self): - return self._name - - @name.setter - def name(self, name): - cv.check_type('element name', name, string_types) - cv.check_length('element name', name, 1, 2) - self._name = name + return self def expand(self, percent, percent_type, enrichment=None, cross_sections=None): @@ -93,7 +82,7 @@ class Element(object): # Get the nuclides present in nature natural_nuclides = set() for nuclide in sorted(NATURAL_ABUNDANCE.keys()): - if re.match(r'{}\d+'.format(self.name), nuclide): + if re.match(r'{}\d+'.format(self), nuclide): natural_nuclides.add(nuclide) # Create dict to store the expanded nuclides and abundances @@ -113,7 +102,7 @@ class Element(object): root = tree.getroot() for child in root: nuclide = child.attrib['materials'] - if re.match(r'{}\d+'.format(self.name), nuclide) and \ + if re.match(r'{}\d+'.format(self), nuclide) and \ '_m' not in nuclide: library_nuclides.add(nuclide) @@ -134,14 +123,14 @@ class Element(object): # 0 nuclide is present. If so, set the abundance to 1 for this # nuclide. Else, raise an error. elif len(mutual_nuclides) == 0: - nuclide_0 = self.name + '0' + nuclide_0 = self + '0' if nuclide_0 in library_nuclides: abundances[nuclide_0] = 1.0 else: msg = 'Unable to expand element {0} because the cross '\ 'section library provided does not contain any of '\ 'the natural isotopes for that element.'\ - .format(self.name) + .format(self) raise ValueError(msg) # If some, but not all, natural nuclides are in the library, add diff --git a/openmc/macroscopic.py b/openmc/macroscopic.py index 3a46b5225..cdb5cbb39 100644 --- a/openmc/macroscopic.py +++ b/openmc/macroscopic.py @@ -3,7 +3,7 @@ from six import string_types from openmc.checkvalue import check_type -class Macroscopic(object): +class Macroscopic(str): """A Macroscopic object that can be used in a material. Parameters @@ -18,22 +18,10 @@ class Macroscopic(object): """ - def __init__(self, name=''): - # Initialize class attributes - self._name = '' - - # Set the Macroscopic class attributes - self.name = name - - def __repr__(self): - string = 'Macroscopic - {0}\n'.format(self._name) - return string + def __new__(cls, name): + check_type('name', name, string_types) + return super(Macroscopic, cls).__new__(cls, name) @property def name(self): - return self._name - - @name.setter - def name(self, name): - check_type('name', name, string_types) - self._name = name + return self diff --git a/openmc/nuclide.py b/openmc/nuclide.py index d1f0c1f29..2e5a8e119 100644 --- a/openmc/nuclide.py +++ b/openmc/nuclide.py @@ -5,7 +5,7 @@ from six import string_types import openmc.checkvalue as cv -class Nuclide(object): +class Nuclide(str): """A nuclide that can be used in a material. Parameters @@ -20,54 +20,22 @@ class Nuclide(object): """ - def __init__(self, name=''): + def __new__(cls, name): # Initialize class attributes - self._name = '' + orig_name = name - # Set the Material class attributes - self.name = name + if '-' in name: + name = name.replace('-', '') + name = name.replace('Nat', '0') + if name.endswith('m'): + name = name[:-1] + '_m1' - def __eq__(self, other): - if isinstance(other, Nuclide): - if self.name != other.name: - return False - else: - return True - elif isinstance(other, string_types) and other == self.name: - return True - else: - return False + msg = 'OpenMC nuclides follow the GND naming convention. Nuclide ' \ + '"{}" is being renamed as "{}".'.format(orig_name, name) + warnings.warn(msg) - def __ne__(self, other): - return not self == other - - def __gt__(self, other): - return repr(self) > repr(other) - - def __lt__(self, other): - return not self > other - - def __hash__(self): - return hash(repr(self)) - - def __repr__(self): - return 'Nuclide - {0}\n'.format(self._name) + return super(Nuclide, cls).__new__(cls, name) @property def name(self): - return self._name - - @name.setter - def name(self, name): - cv.check_type('name', name, string_types) - self._name = name - - if '-' in name: - self._name = name.replace('-', '') - self._name = self._name.replace('Nat', '0') - if self._name.endswith('m'): - self._name = self._name[:-1] + '_m1' - - msg = 'OpenMC nuclides follow the GND naming convention. Nuclide ' \ - '"{}" is being renamed as "{}".'.format(name, self._name) - warnings.warn(msg) + return self