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 do you handle the "percent" unit ?... and some general questions about load_definitions #379

Closed
ChristianTremblay opened this issue May 25, 2016 · 6 comments

Comments

@ChristianTremblay
Copy link

I'm actually trying to integrate pint into pyhaystack, a project to connect Python to building automation systems. Pint would add great features to our project by allowing us to exploit the power of units given by the servers (temperature conversion, energy computation, etc.)

So as the title says : I didn't see any reference to a % unit in the default file. Is this something that can be handled ?

Our project use www.project-haystack.org units so I'm trying to create some kind of bridge between Pint and haystack so the units will be compatible. Most of the time, it works without problems but there are a lot of little edge cases that need some tweaks.

I tried to use load_definitions but I always get AttributeError: 'int' object has no attribute 'split' when I try to load the file... Maybe I'm doing something wrong.

On a project like ours, would you suggest us to use our own file loaded in the module to isolate us from your default file ?
Or using a load_definition only ? (in that case, what if some day, duplicates appear ?)

Thanks for your answers.

@ChristianTremblay
Copy link
Author

Finally made up a custom ureg inside our app to handle special cases. Is this the way to go ?

ureg = UnitRegistry()
ureg.define('% = [] = percent')
ureg.define('pixel = [] = px = dot = picture_element = pel')
[...]

@tadhgmister
Copy link
Contributor

generally you want to define as FULL_NAME = <derived or dimensionality> = ABBREVIATED so you might want to define percent like this:

ureg.define('percent = 0.01*count = %')

This way it realizes that percent is 1/100 of the base dimensionless unit so it can be added with other dimensionless quantities correctly and by putting % where the abbreviated notation goes using ~ string formatting correctly displays it:

x = 50 * ureg.percent
print(format(x,"~")) # 50 %
print(x + 1) #1.5 dimensionless

@mcgibbon
Copy link

mcgibbon commented Dec 6, 2016

The unit registry can't handle "%" as a unit, at least on my system. Pint 0.7.2 on Windows, Python 3.

ureg = pint.UnitRegistry()
ureg.define('percent = 0.01*count = %')
(50*ureg('percent')).to('count')
Out[53]:
<Quantity(0.5, 'count')>
(50*ureg('percent')).to('')
Out[54]:
<Quantity(0.5, 'dimensionless')>
(50*ureg('%')).to('')
Traceback (most recent call last):
File "C:\Users\Jeremy\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-55-c06989b92518>", line 1, in <module>
(50*ureg('%')).to('')
File "C:\Users\Jeremy\Anaconda3\lib\site-packages\pint\unit.py", line 1271, in parse_expression
return build_eval_tree(gen).evaluate(lambda x : self._eval_token(x, case_sensitive=case_sensitive,
AttributeError: 'NoneType' object has no attribute 'evaluate'

Same error if I just say ureg('%') (with or without defining it). Specifically I get the AttributeError when I should get the UndefinedUnitError.

@jonc125
Copy link

jonc125 commented Mar 2, 2017

I get the same error as @mcgibbon with Pint 0.7.2 running on Anaconda Python 3.5. I also don't see the formatting behaviour @tadhgmister describes:

ureg = pint.UnitRegistry()
ureg.define('percent = 0.01*count = %')
x = 50 * ureg.percent
print(format(x,"~"))  # Output: 50  [was expecting 50 %]
print(x + 1)          # Output: 1.5 dimensionless
print(x.to('count'))  # Output: 0.5 count

@mcgibbon
Copy link

mcgibbon commented Mar 2, 2017

I posted a workaround in #429.

@hgrecco
Copy link
Owner

hgrecco commented Dec 3, 2019

Closing. Feel free to reopen.

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

5 participants