Skip to content

Commit

Permalink
Update __hash__ to use all of self.attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
DNIIBOY committed Feb 25, 2024
1 parent d3c617f commit 5c5f09f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
17 changes: 8 additions & 9 deletions chempy/chemistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,14 @@ def __eq__(self, other):
return True

def __hash__(self) -> int:
return sum(
map(
hash,
(
getattr(self, k)
for k in ("name", "latex_name", "unicode_name", "html_name")
),
)
)
hashed_values = []
for key in self.attrs:
value = getattr(self, key)
if isinstance(value, dict):
hashed_values.append(hash(tuple(sorted(value.items()))))
else:
hashed_values.append(hash(value))
return sum(hashed_values)

@property
def charge(self):
Expand Down
1 change: 1 addition & 0 deletions chempy/tests/test_chemistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_Substance():
assert s.charge == 1
assert abs(s.mass - 1.008) < 1e-3
assert s in {s: 1}
assert hash(s) != hash(Substance.from_formula("He"))


def test_Substance__2():
Expand Down

0 comments on commit 5c5f09f

Please sign in to comment.