pint icon indicating copy to clipboard operation
pint copied to clipboard

parse_pattern doesn´t parse compound units

Open keigezellig opened this issue 1 year ago • 5 comments

When using parse_pattern with compound units in the pattern (e.g. 'meter per second'), it won´t parse.

Example:

>>> input = '44 mpsec'
>>> pattern = '{meter per second} mpsec´                 //Tried also 'meter / second' but didn´t work either.
>>> ureg.parse_pattern(input_string=input, pattern=pattern)
>>> []                                                   //Would expect <Quantity(44, 'meter / second')> here 

With a single unit in the pattern (like 'meter') it does work fine

keigezellig avatar Jun 01 '24 09:06 keigezellig

Hello, this is because you are missing the underscore characters associated with the unit definition

It works with:

>>> input = '44 mpsec'
>>> pattern = '{meter_per_second} mpsec'
>>> ureg.parse_pattern(input_string=input, pattern=pattern)
[44.0 <Unit('meter_per_second')>]

Greetings

yerkoescalona avatar Jun 04 '24 14:06 yerkoescalona

perhaps we should add some regex to parse_pattern so it replaces " per " with " / "?

andrewgsavage avatar Jun 27 '24 09:06 andrewgsavage

@yerkoescalona Ok, that was not very obvious (i was already looking for the unit definitions in the code, but i couldn´t find them, maybe we could put a description of all unit definitions somewhere in the documentation?)

keigezellig avatar Jun 27 '24 11:06 keigezellig

At the top of the tutorial

https://pint.readthedocs.io/en/stable/getting/tutorial.html

If no parameters are given to the constructor, the UnitRegistry is populated with the default list of units and prefixes.

andrewgsavage avatar Jun 27 '24 12:06 andrewgsavage

Can we consider this issue closed or do we need better docs ?

jules-ch avatar Dec 12 '24 22:12 jules-ch