widesky / hszinc

Project Haystack Zinc (Zinc is not CSV) parser and dumper for Python 2.7 and 3.x
https://widesky.cloud
BSD 2-Clause "Simplified" License
10 stars 8 forks source link

Percent (and python identifiers) characters not allowed when definining UnitRegistry #38

Open quajada opened 1 year ago

quajada commented 1 year ago

Error under: _def define_haystackunits() in pintutil.py

DefinitionSyntaxError: Cannot define '%' (UnitDefinition): is not a valid unit name (must follow Python identifier rules) 1,0-1,10 % = [] = %

Percentage units must be defined some other way. Additionally, DefinitionSyntaxError: missing unary operator "%" is thrown when tried to bypass with following code:

_ureg = UnitRegistry(onredefinition='ignore') ureg.define("percent = [] = %") ureg('50 %')

This error prevents hszinc to be pip installed

versions: hszinc - 1.3.1 pint - 0.20.1

sjlongland commented 1 year ago

Ick, yeah… I can see that being a potential problem. It might be a while before I can get to looking at this problem as not many of us get to work with Python stuff these days.

Do you know if it happens with a particular release of pint?

Alternatively, if you do not require unit conversion, you can remove pint from requirements.txt. It is an optional extra dependency (I need to figure out how to declare it as such) and not actually required to make hszinc work.

quajada commented 1 year ago

Thanks for your reply! It is happening with pint 0.20.1, but I think the issue comes from trying to use python identifiers to define the UnitRegistry - so no special characters.

Perhaps they can all be treated as:

ureg.define('fraction = [] = frac') ureg.define('percent = 1e-2 frac = pct')

But I am not sure how to recognize and assign the unit string from haystack

sjlongland commented 1 year ago

Could those affected please try this branch and let me know how they go?

https://github.com/widesky/hszinc/tree/bugfix/WC-5825-issue38-pint-percent

I had some fun and games, because nose is no longer maintained, I had to port a number of tests over… and because Travis CI is no more, I had to replace that with Github actions.

At this time, I am not testing on Python 2.7. Not sure if I should bother actually going forward, if it breaks now, it breaks. But anyway… that branch should resolve the issues with pint as it is today.