Skip to content

Commit

Permalink
Fix bug which allowed formulas like Li+2-
Browse files Browse the repository at this point in the history
  • Loading branch information
xnx committed Jul 3, 2022
1 parent 4cc214f commit d862165
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
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.7",
version="2.5.8",
description="A package for managing simple chemical species and states",
long_description=long_description,
long_description_content_type="text/x-rst",
Expand Down
3 changes: 3 additions & 0 deletions src/pyvalem/formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ def _parse_formula(self, formula):

if any(s in formula for s in ("++", "--", "+-", "-+")):
raise FormulaParseError("Invalid formula syntax: {}".format(formula))
# NB whilst NH2+CH3CHO2- is allowed, things like 'Li+2-' are not...
if re.search(r"[+-]\d+[+-]", formula):
raise FormulaParseError("Invalid formula syntax: {}".format(formula))

# We make a particular exception for various special cases, including
# photons, electrons, positrons and "M", denoting an unspecified
Expand Down
8 changes: 8 additions & 0 deletions tests/test_formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def test_charged_species(self):
self.assertRaises(FormulaParseError, Formula, "H--")
self.assertRaises(FormulaParseError, Formula, "H+-")
self.assertRaises(FormulaParseError, Formula, "H-+")
self.assertRaises(FormulaParseError, Formula, "Li-2+")

cf2 = Formula("NH2+CH3CHO2-")
self.assertEqual(
cf2.html,
"NH<sub>2</sub><sup>+</sup>CH<sub>3</sub>CHO<sub>2</sub><sup>-</sup>",
)
self.assertEqual(cf2.charge, 0)

def test_special_formulas(self):
# stoichiometric_formula, html, latex, slug, rmm, natoms, charge, atoms
Expand Down

0 comments on commit d862165

Please sign in to comment.