From e194d8118f871cfb55bfca5c0721aa8926156f48 Mon Sep 17 00:00:00 2001 From: Jeremy A Gray Date: Sat, 23 Sep 2023 11:48:00 -0500 Subject: [PATCH] fix(parser): handle decimal subscripts Translate a Unicode decimal point in a subscript as a text decimal point. Use `.` as the decimal point operator and `..` as the hydrate operator. Add zinc nitrate example from bjodah/chempy#207. Signed-off-by: Jeremy A Gray --- chempy/util/parsing.py | 8 ++++--- chempy/util/tests/test_parsing.py | 38 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/chempy/util/parsing.py b/chempy/util/parsing.py index 0313545c..b4f89bb9 100644 --- a/chempy/util/parsing.py +++ b/chempy/util/parsing.py @@ -95,7 +95,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? @@ -600,9 +600,11 @@ def formula_to_latex(formula, prefixes=None, infixes=None, **kwargs): ) -_unicode_sub = {} +_unicode_sub = { + ".": ".", +} -for k, v in enumerate("₀₁₂₃₄₅₆₇₈₉"): +for k, v in enumerate("₀₁₂₃₄₅₆₇₈₉."): _unicode_sub[str(k)] = v _unicode_sup = { diff --git a/chempy/util/tests/test_parsing.py b/chempy/util/tests/test_parsing.py index 67386278..12ebffa8 100644 --- a/chempy/util/tests/test_parsing.py +++ b/chempy/util/tests/test_parsing.py @@ -11,6 +11,7 @@ parsing_library, to_reaction, ) + from ..testing import requires @@ -625,6 +626,22 @@ def test_formula_to_latex_caged(species, latex): ("[Fe(H2O)6][Fe(CN)6]..19H2O(aq)", r"[Fe(H₂O)₆][Fe(CN)₆]·19H₂O(aq)"), ("[Fe(CN)6]-3", r"[Fe(CN)₆]³⁻"), ("[Fe(CN)6]-3(aq)", r"[Fe(CN)₆]³⁻(aq)"), + ( + "Ca2.832Fe0.6285Mg5.395(CO3)6", + r"Ca₂.₈₃₂Fe₀.₆₂₈₅Mg₅.₃₉₅(CO₃)₆", + ), + ( + "Ca2.832Fe0.6285Mg5.395(CO3)6(s)", + r"Ca₂.₈₃₂Fe₀.₆₂₈₅Mg₅.₃₉₅(CO₃)₆(s)", + ), + ( + "Ca2.832Fe0.6285Mg5.395(CO3)6..8H2O(s)", + r"Ca₂.₈₃₂Fe₀.₆₂₈₅Mg₅.₃₉₅(CO₃)₆·8H₂O(s)", + ), + ( + "Zn(NO3)2..6H2O", + r"Zn(NO₃)₂·6H₂O", + ), ], ) @requires(parsing_library) @@ -692,8 +709,29 @@ def test_formula_to_unicode_caged(species, unicode): ), ("[Fe(CN)6]-3", r"[Fe(CN)6]3-"), ("[Fe(CN)6]-3(aq)", r"[Fe(CN)6]3-(aq)"), + ( + "Ca2.832Fe0.6285Mg5.395(CO3)6", + r"Ca2.832Fe0.6285Mg5.395(CO3)6", + ), + ( + "Ca2.832Fe0.6285Mg5.395(CO3)6(s)", + r"Ca2.832Fe0.6285Mg5.395(CO3)6(s)", + ), + ( + "Ca2.832Fe0.6285Mg5.395(CO3)6..8H2O(s)", + r"Ca2.832Fe0.6285Mg5.395(CO3)6⋅8H2O(s)", + ), + ( + "Ca2.832Fe0.6285Mg5.395(CO3)6..8H2O(s)", + r"Ca2.832Fe0.6285Mg5.395(CO3)6⋅8H2O(s)", + ), + ( + "Zn(NO3)2..6H2O", + r"Zn(NO3)2⋅6H2O", + ), ], ) + @requires(parsing_library) def test_formula_to_html(species, html): assert formula_to_html(species) == html