Skip to content

Commit

Permalink
Allow \u00b7 to be parsed as complex/crystal water
Browse files Browse the repository at this point in the history
  • Loading branch information
bjodah committed Apr 23, 2024
1 parent 3636f2a commit 9fe07c9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions .woodpecker.yaml
Expand Up @@ -27,6 +27,7 @@ steps:
- export CPATH=$SUNDBASE/include:$CPATH
- export LIBRARY_PATH=$SUNDBASE/lib
- export LD_LIBRARY_PATH=$SUNDBASE/lib
- 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
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -385,7 +385,7 @@ If you make use of ChemPy in e.g. academic work you may cite the following peer-
Depending on what underlying solver you are using you should also cite the appropriate paper
(you can look at the list of references in the JOSS article). If you need to reference,
in addition to the paper, a specific point version of ChemPy (for e.g. reproducibility)
you can get per-version DOIs from the zendodo archive:
you can get per-version DOIs from the zenodo archive:

.. image:: https://zenodo.org/badge/8840/bjodah/chempy.svg
:target: https://zenodo.org/badge/latestdoi/8840/bjodah/chempy
Expand Down
16 changes: 11 additions & 5 deletions chempy/util/parsing.py
Expand Up @@ -95,7 +95,7 @@ def _get_formula_parser():
| '{' formula '}'
| '[' formula ']' ) count prime charge?
formula :: term+
hydrate :: '..' count? formula
hydrate :: ( '..' | '\u00B7' ) count? formula
state :: '(' ( 's' | 'l' | 'g' | 'aq' | 'cr' ) ')'
compound :: count formula hydrate? state?
Expand All @@ -114,7 +114,7 @@ def _get_formula_parser():
| '{' formula '}'
| '[' formula ']' ) count prime charge?
formula :: term+
hydrate :: '..' count? formula
hydrate :: ( '..' | '\u00B7' ) count? formula
state :: '(' ( 's' | 'l' | 'g' | 'aq' | 'cr' ) ')'
compound :: count formula hydrate? state?
"""
Expand Down Expand Up @@ -334,7 +334,7 @@ def _parse_stoich(stoich):

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

_html_mapping = {k + "-": "&" + k + ";-" for k in _greek_letters}
_html_mapping["."] = "⋅"
Expand Down Expand Up @@ -386,7 +386,10 @@ def formula_to_composition(

stoich_tok, chg_tok = _formula_to_parts(formula, prefixes, suffixes)[:2]
tot_comp = {}
parts = stoich_tok.split("..")
if '\u00b7' in stoich_tok:
parts = stoich_tok.split('\u00b7')
else:
parts = stoich_tok.split("..")

for idx, stoich in enumerate(parts):
if idx == 0:
Expand Down Expand Up @@ -532,7 +535,10 @@ def _formula_to_format(
suffixes=("(s)", "(l)", "(g)", "(aq)"),
):
parts = _formula_to_parts(formula, prefixes.keys(), suffixes)
stoichs = parts[0].split("..")
if '\u00b7' in parts[0]:
stoichs = parts[0].split('\u00b7')
else:
stoichs = parts[0].split("..")
string = ""
for idx, stoich in enumerate(stoichs):
if idx == 0:
Expand Down
7 changes: 7 additions & 0 deletions chempy/util/tests/test_parsing.py
Expand Up @@ -750,3 +750,10 @@ def test_formula_to_html(species, html):
def test_formula_to_html_caged(species, html):
"""Should produce HTML for cage species."""
assert formula_to_html(species) == html



def test_composition_dot_as_crystal_water_chempy08x():
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

0 comments on commit 9fe07c9

Please sign in to comment.