Skip to content

Commit

Permalink
apply old parsing behavior to 0.8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
bjodah committed Apr 22, 2024
1 parent 8bf81f9 commit 4ed766e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .woodpecker.yaml
Expand Up @@ -27,7 +27,7 @@ steps:
- export CPATH=$SUNDBASE/include:$CPATH
- export LIBRARY_PATH=$SUNDBASE/lib
- export LD_LIBRARY_PATH=$SUNDBASE/lib
- python3 -m pip install --user --upgrade-strategy=eager --upgrade cython
- python3 -m pip install --cache-dir $CACHE_ROOT/pip_cache --user --upgrade-strategy=eager --upgrade cython
- python3 -m pip install --cache-dir $CACHE_ROOT/pip_cache --user -e .[all]
- python3 -c "import pycvodes; import pyodesys; import pygslodeiv2" # debug this CI config
- git fetch -tq
Expand Down
30 changes: 20 additions & 10 deletions chempy/util/parsing.py
Expand Up @@ -5,8 +5,9 @@
from collections import defaultdict

import re
import warnings

from .pyutil import memoize
from .pyutil import memoize, ChemPyDeprecationWarning
from .periodic import symbols

parsing_library = "pyparsing" # info used for selective testing.
Expand Down Expand Up @@ -95,7 +96,7 @@ def _get_formula_parser():
| '{' formula '}'
| '[' formula ']' ) count prime charge?
formula :: term+
hydrate :: '.' count? formula
hydrate :: ( '.' | '·' | '*' ) count? formula
state :: '(' ( 's' | 'l' | 'g' | 'aq' | 'cr' ) ')'
compound :: count formula hydrate? state?
Expand All @@ -114,7 +115,7 @@ def _get_formula_parser():
| '{' formula '}'
| '[' formula ']' ) count prime charge?
formula :: term+
hydrate :: '..' count? formula
hydrate :: ( '..' | '·' | '*' ) count? formula
state :: '(' ( 's' | 'l' | 'g' | 'aq' | 'cr' ) ')'
compound :: count formula hydrate? state?
"""
Expand Down Expand Up @@ -330,16 +331,16 @@ def _parse_stoich(stoich):
_latex_mapping["epsilon-"] = "\\varepsilon-"
_latex_mapping["omicron-"] = "o-"
_latex_mapping["."] = "^\\bullet "
_latex_infix_mapping = {"..": "\\cdot "}
_latex_infix_mapping = {"·": "\\cdot "}

_unicode_mapping = {k + "-": v + "-" for k, v in zip(_greek_letters, _greek_u)}
_unicode_mapping["."] = "⋅"
_unicode_infix_mapping = {"..": "·"}
_unicode_infix_mapping = {"·": "·"}

_html_mapping = {k + "-": "&" + k + ";-" for k in _greek_letters}
_html_mapping["."] = "⋅"
# _html_infix_mapping = _html_mapping
_html_infix_mapping = {"..": "⋅"}
_html_infix_mapping = {"·": "⋅"}


def _get_leading_integer(s):
Expand Down Expand Up @@ -377,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'.format(s)) == {11: 2, 6: 1, 8: 10, 1: 14}
True
"""
Expand All @@ -386,7 +387,7 @@ def formula_to_composition(

stoich_tok, chg_tok = _formula_to_parts(formula, prefixes, suffixes)[:2]
tot_comp = {}
parts = stoich_tok.split("..")
parts = stoich_tok.split("·")

for idx, stoich in enumerate(parts):
if idx == 0:
Expand Down Expand Up @@ -532,14 +533,23 @@ def _formula_to_format(
suffixes=("(s)", "(l)", "(g)", "(aq)"),
):
parts = _formula_to_parts(formula, prefixes.keys(), suffixes)
stoichs = parts[0].split("..")
parts0 = parts[0].replace("..", '·').replace('*', '·')
if '.' in parts0:
warnings.warn(
("dot is ambiguous in chempy-0.8.x, prefer '*' or '' for complexes."
" Dot will be interpreted as floating point in chempy-0.9+"),
ChemPyDeprecationWarning
)
parts0 = parts0.replace('.', '·')
stoichs = parts0.split("·")

string = ""
for idx, stoich in enumerate(stoichs):
if idx == 0:
m = 1
else:
m, stoich = _get_leading_integer(stoich)
string += _subs("..", infixes)
string += _subs("·", infixes)
if m != 1:
string += str(m)
string += re.sub(r"([0-9]+\.[0-9]+|[0-9]+)", lambda m: sub(m.group(1)), stoich)
Expand Down

0 comments on commit 4ed766e

Please sign in to comment.