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

Support preprocessing in to_units_container for string inputs #1402

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

eabruzzese
Copy link

@eabruzzese eabruzzese commented Oct 28, 2021

Implement support for registry preprocessing against string inputs to the to_units_container util.

This allows string inputs to use the given registry preprocessors if available, e.g.:

from pint import UnitRegistry
from pint.utils import to_units_container

ureg = UnitRegistry(preprocessors=[lambda s: s.replace('%', ' percent ')])
ureg.define('percent = 0.01 = %')

# Used directly, specifying the registry with defined preprocessors.
to_units_container('%', registry=ureg)
# <UnitsContainer({'percent': 1})>

# Used via .to('%')
quantity = ureg.Quantity(1)
quantity.to('%')
# 100.0 <Unit('percent')>

Implement support for registry preprocessing against string inputs to
the to_units_container util.
@eabruzzese
Copy link
Author

@hgrecco I think this closes/addresses several issues, though I'm not sure I've found all of them. I've added the ones I could find with my limited search vocabulary.

The unit tests pass and the change seems to work well for the use cases I've tried, but I'm not experienced enough with the library to know if my change will have any knock-on effects beyond what's covered by the test suite.

Do you think this is a usable solution, or am I misunderstanding something in #429?

@eabruzzese eabruzzese marked this pull request as ready for review October 28, 2021 18:00
@eabruzzese eabruzzese changed the title Support preprocessing in to_units_container Support preprocessing in to_units_container for string inputs Oct 28, 2021
@MichaelTiemannOSC
Copy link
Collaborator

Want!

@MichaelTiemannOSC
Copy link
Collaborator

xref #1687

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants