Skip to content

Commit

Permalink
Merge branch '0.8.x' of github.com:bjodah/chempy into 0.8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
bjodah committed Apr 24, 2024
2 parents 3a32480 + b55a3d0 commit d135609
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
18 changes: 7 additions & 11 deletions chempy/util/parsing.py
Expand Up @@ -96,7 +96,7 @@ def _get_formula_parser():
| '{' formula '}'
| '[' formula ']' ) count prime charge?
formula :: term+
hydrate :: ( '.' | '\u00B7' | '*' ) count? formula
hydrate :: ( '..' | '\u00B7' | '.' ) count? formula
state :: '(' ( 's' | 'l' | 'g' | 'aq' | 'cr' ) ')'
compound :: count formula hydrate? state?
Expand All @@ -115,7 +115,7 @@ def _get_formula_parser():
| '{' formula '}'
| '[' formula ']' ) count prime charge?
formula :: term+
hydrate :: ( '..' | '\u00B7' | '*' ) count? formula
hydrate :: ( '..' | '\u00B7' | '.' ) count? formula
state :: '(' ( 's' | 'l' | 'g' | 'aq' | 'cr' ) ')'
compound :: count formula hydrate? state?
"""
Expand Down Expand Up @@ -378,7 +378,7 @@ def formula_to_composition(
True
>>> formula_to_composition('.NHO-(aq)') == {0: -1, 1: 1, 7: 1, 8: 1}
True
>>> formula_to_composition('Na2CO3*7H2O') == {11: 2, 6: 1, 8: 10, 1: 14}
>>> formula_to_composition('Na2CO3..7H2O') == {11: 2, 6: 1, 8: 10, 1: 14}
True
"""
Expand All @@ -387,10 +387,10 @@ def formula_to_composition(

stoich_tok, chg_tok = _formula_to_parts(formula, prefixes, suffixes)[:2]
tot_comp = {}
if ".." in stoich_tok:
parts = stoich_tok.split("..")
elif "\u00b7" in stoich_tok:
if "\u00b7" in stoich_tok:
parts = stoich_tok.split('\u00b7')
elif '..' in stoich_tok:
parts = stoich_tok.split("..")
elif '.' in stoich_tok:
warnings.warn(
("dot is ambiguous in chempy-0.8.x, prefer '*' or '\u00b7' for complexes."
Expand All @@ -399,7 +399,7 @@ def formula_to_composition(
)
parts = stoich_tok.split('.')
else:
parts = list(filter(len, internal_asterisk.split(stoich_tok)))
parts = [stoich_tok]

for idx, stoich in enumerate(parts):
if idx == 0:
Expand Down Expand Up @@ -536,9 +536,6 @@ def to_reaction(line, substance_keys, token, Cls, globals_=None, **kwargs):
)


internal_asterisk = re.compile(r"([^\s\*]+)\*([a-zA-Z0-9]+)")


def _formula_to_format(
sub,
sup,
Expand All @@ -549,7 +546,6 @@ def _formula_to_format(
):
parts = _formula_to_parts(formula, prefixes.keys(), suffixes)
parts0 = parts[0].replace("..", "\u00B7")
parts0 = internal_asterisk.sub("\u00B7", parts0)
if '.' in parts0:
warnings.warn(
("dot is ambiguous in chempy-0.8.x, prefer '*' or '' for complexes."
Expand Down
2 changes: 1 addition & 1 deletion chempy/util/tests/test_parsing.py
Expand Up @@ -724,7 +724,7 @@ def test_composition_dot_as_crystal_water_chempy08x():
as floating point delimiter in fractional stoichiometric coefficients."""
ref = {30: 1, 7: 2, 8: 12, 1: 12}
assert formula_to_composition('Zn(NO3)2{}6H2O'.format('\u00B7')) == ref
assert formula_to_composition('Zn(NO3)2*6H2O') == ref
assert formula_to_composition('Zn(NO3)2..6H2O') == ref
# https://docs.pytest.org/en/7.1.x/how-to/capture-warnings.html#ensuring-code-triggers-a-deprecation-warning
with pytest.deprecated_call():
assert formula_to_composition('Zn(NO3)2.6H2O') == ref

0 comments on commit d135609

Please sign in to comment.