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

How should one parse unit names that are not valid identifiers? #554

Closed
jameshiebert opened this issue Sep 21, 2017 · 5 comments
Closed

Comments

@jameshiebert
Copy link

I'm using pint to do checking and conversion of units for some database insertion code and I'm a bit confused about how to define the unit '%'.

Basically, I have a set of weather observations coming in from multi-source input files that are attributed with units, and I'm stuffing them into a database table that may be attributed with different (but compatible) units.

In particular, we're pulling in relative humidity data that are attributed with the units "%" in the database.

Based on the comments in #226, I assumed that the new parser would allow units like " ' and %. But then in pint's test suite the test for this case seems to be intended to fail?

    @unittest.expectedFailure
    def test_issue25(self):
        x = ParserHelper.from_string('10 %')

Or am I reading that wrong?

Defining the unit '%' doesn't error out, but then trying to parse it does:

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> ureg.define('% = percent')
>>> ureg.parse_expression('%')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/hiebert/code/crmprtd/env36/lib/python3.6/site-packages/pint/registry.py", line 840, in parse_expression
    return build_eval_tree(gen).evaluate(lambda x: self._eval_token(x,
AttributeError: 'NoneType' object has no attribute 'evaluate'

Is there some other way that I should be doing this? Thanks for any info that you can provide.

@hgrecco
Copy link
Owner

hgrecco commented Sep 22, 2017

The new parser should definitely allow this as we have decoupled from Python's eval. But we are still using the Python tokenizer. Notice this:
The interesting thing is that:

>>> ureg.parse_expression('10%')
10

I think that the quickest way in your case will be to introduce a replacement rule '%' -> '* 0.01'.

@dalito
Copy link
Contributor

dalito commented Sep 27, 2017

Also interesting:

>>> ureg.parse_expression(u'2%4')
8                               
>>> 2%4                         
2                               

@jameshiebert
Copy link
Author

Thanks @hgrecco , I'll give that a shot.

>>> 2%4 
2

Well... that's 2 [mod] 4, so that doesn't surprise me.

@hgrecco
Copy link
Owner

hgrecco commented Dec 21, 2019

You can now add your own preprocessors See #911

@hgrecco hgrecco closed this as completed Dec 21, 2019
@jameshiebert
Copy link
Author

Perfect, thanks!

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

No branches or pull requests

3 participants