Skip to content

Commit

Permalink
Silently change hv to hν in _parse_formula and add a hash method
Browse files Browse the repository at this point in the history
  • Loading branch information
xnx committed Jul 27, 2022
1 parent 6bf8a29 commit 760679a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="pyvalem",
version="2.5.9",
version="2.5.10",
description="A package for managing simple chemical species and states",
long_description=long_description,
long_description_content_type="text/x-rst",
Expand Down
18 changes: 12 additions & 6 deletions src/pyvalem/formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ class Formula:
Notes
-----
The ``__repr__`` method is overloaded to provide a *canonicalised* representation
of the formula. The idea is that two formulas representing the same physical entity
will have the same ``repr(formula)`` representation.
The ``__repr__`` method is intended to be a *canonicalised* representation
of the formula, but no check is made to ensure that a single formula is used
to describe a unique species (e.g. "HD" and "DH" and "H(2H)" all have
different __repr__ representations.
Examples
--------
Expand Down Expand Up @@ -321,6 +322,10 @@ def _parse_formula(self, formula):
if formula == "e":
# Quietly convert e to e-.
self.formula = formula = "e-"

if self.formula == "hv":
self.formula = "hν"

if formula in special_cases:
for attr, val in special_cases[formula].items():
setattr(self, attr, val)
Expand Down Expand Up @@ -506,12 +511,13 @@ def _get_charge_reps(charge):
return "", "", ""

def __repr__(self):
if self.formula == "hv":
return "hν"
return self.formula

def __hash__(self):
return hash(self.formula)

def __eq__(self, other):
return repr(self.formula) == repr(other.formula)
return self.formula == other.formula

def _stoichiometric_formula_atomic_number(self):
"""Return a list of atoms/isotopes and their stoichiometries.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ def test_parse_fail(self):
self.assertRaises(FormulaParseError, Formula, "H3O^+")
self.assertRaises(FormulaParseError, Formula, "H_2S")

def test_formula_hash(self):
f1 = Formula("hv")
f2 = Formula("Ar")
test_dict = {f1: 0, f2: "a"}
test_set = set((f1, f1, f2))

f3 = Formula("hν")
self.assertEqual(hash(f1), hash(f3))
self.assertEqual(repr(f1), repr(f3))


if __name__ == "__main__":
unittest.main()

0 comments on commit 760679a

Please sign in to comment.