Skip to content

Add unicode form formula - #225

Open
montmorill wants to merge 1 commit into
bjodah:masterfrom
montmorill:patch-2
Open

Add unicode form formula#225
montmorill wants to merge 1 commit into
bjodah:masterfrom
montmorill:patch-2

Conversation

@montmorill

@montmorill montmorill commented Sep 18, 2023

Copy link
Copy Markdown
Contributor

eg.:

from chempy import balance_stoichiometry, Reaction
from chempy.printing import unicode_

reac = {"H+", "SO4-2"}
prod = {"H2SO4"}
reac, prod = balance_stoichiometry(reac, prod)
print(unicode_(Reaction(reac, prod)))

before: 2 H+ + SO4-2 → H2SO4
after: 2 H⁺ + SO₄²⁻ → H₂SO₄

@bjodah

bjodah commented Sep 18, 2023

Copy link
Copy Markdown
Owner

For simple substances this is a nice change of default. However, I do worry that for other substances which are typically entered by their field specific code name (which may contain digits), e.g G6P for glucose-6-phosphate, then this default might be unwanted. We have a .unicode_name field on the Substance class. But it is only set by the parser I think.

Perhaps something along these lines would be preferable (I'm thinking out loud here, I'm not asking you the implement the following):

>>> r1 = Reaction.parse("H+ + SO4-2 -> H2SO4", elements_in_formula=True)
>>> r2 = r1.balanced()
>>> r2
2 H⁺ + SO₄²⁻ → H₂SO₄

That way, someone who is entering e.g. "G6P" may pass a different flag to the .parse method. What do you think?

I find that printing options in general are a bit tricky, since the preferred defaults may differ quite a bit between different fields...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants