From 4be3c64da91ac224c1f4ff2056cf68253589eed5 Mon Sep 17 00:00:00 2001 From: "wbinventor@gmail.com" Date: Sun, 29 Nov 2015 09:00:47 -0500 Subject: [PATCH] Added __hash__ routines to all Python classes based on __repr__ methods --- openmc/element.py | 2 +- openmc/filter.py | 2 +- openmc/geometry.py | 2 +- openmc/material.py | 3 +++ openmc/mesh.py | 3 +++ openmc/nuclide.py | 2 +- openmc/tallies.py | 16 +--------------- openmc/universe.py | 12 ++++++++++++ 8 files changed, 23 insertions(+), 19 deletions(-) diff --git a/openmc/element.py b/openmc/element.py index d395b434f7..cdc422ed2b 100644 --- a/openmc/element.py +++ b/openmc/element.py @@ -58,7 +58,7 @@ class Element(object): return not self == other def __hash__(self): - return hash((self._name, self._xs)) + return hash(str(self)) def __repr__(self): string = 'Element - {0}\n'.format(self._name) diff --git a/openmc/filter.py b/openmc/filter.py index 2c4915f511..4c058c0855 100644 --- a/openmc/filter.py +++ b/openmc/filter.py @@ -81,7 +81,7 @@ class Filter(object): return not self == other def __hash__(self): - return hash((self.type, tuple(self.bins))) + return hash(str(self)) def __deepcopy__(self, memo): existing = memo.get(id(self)) diff --git a/openmc/geometry.py b/openmc/geometry.py index e848e0cddf..c0b248780e 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -147,7 +147,7 @@ class Geometry(object): if cell._type == 'normal': material_cells.add(cell) - material_cells = list(material_cells) + material_cells = list(set(material_cells)) material_cells.sort(key=lambda x: x.id) return material_cells diff --git a/openmc/material.py b/openmc/material.py index 5138cc59e2..88c8443086 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -108,6 +108,9 @@ class Material(object): def __ne__(self, other): return not self == other + def __hash__(self): + return hash(str(self)) + def __repr__(self): string = 'Material\n' string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id) diff --git a/openmc/mesh.py b/openmc/mesh.py index 3b66076b79..b963a25a8e 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -189,6 +189,9 @@ class Mesh(object): cv.check_length('mesh width', width, 2, 3) self._width = width + def __hash__(self): + return hash(str(self)) + def __repr__(self): string = 'Mesh\n' string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id) diff --git a/openmc/nuclide.py b/openmc/nuclide.py index 2dd8eb1534..b95601bf7d 100644 --- a/openmc/nuclide.py +++ b/openmc/nuclide.py @@ -61,7 +61,7 @@ class Nuclide(object): return not self == other def __hash__(self): - return hash((self._name, self._xs)) + return hash(str(self)) def __repr__(self): string = 'Nuclide - {0}\n'.format(self._name) diff --git a/openmc/tallies.py b/openmc/tallies.py index 0661ab67b1..8498fbb5b5 100644 --- a/openmc/tallies.py +++ b/openmc/tallies.py @@ -190,21 +190,7 @@ class Tally(object): return not self == other def __hash__(self): - hashable = [] - - for filter in self.filters: - hashable.append((filter.type, tuple(filter.bins))) - - for nuclide in self.nuclides: - hashable.append(nuclide.name) - - for score in self.scores: - hashable.append(score) - - hashable.append(self.estimator) - hashable.append(self.name) - - return hash(tuple(hashable)) + return hash(str(self)) def __repr__(self): string = 'Tally\n' diff --git a/openmc/universe.py b/openmc/universe.py index 0eb77cdf9a..9a8262af39 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -94,6 +94,9 @@ class Cell(object): def __ne__(self, other): return not self == other + def __hash__(self): + return hash(str(self)) + def __repr__(self): string = 'Cell\n' string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id) @@ -479,6 +482,9 @@ class Universe(object): def __ne__(self, other): return not self == other + def __hash__(self): + return hash(str(self)) + def __repr__(self): string = 'Universe\n' string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id) @@ -964,6 +970,9 @@ class RectLattice(Lattice): def __ne__(self, other): return not self == other + def __hash__(self): + return hash(str(self)) + def __repr__(self): string = 'RectLattice\n' string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id) @@ -1198,6 +1207,9 @@ class HexLattice(Lattice): def __ne__(self, other): return not self == other + def __hash__(self): + return hash(str(self)) + def __repr__(self): string = 'HexLattice\n' string += '{0: <16}{1}{2}\n'.format('\tID', '=\t', self._id)