Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asterisk is already used for excited state in photochemistry #231

Merged
merged 2 commits into from Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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