1

I am trying to check for dimensionality of a unit that is complex such as volume (m^3) or velocity (ft/min). How can I use the "pint.check()" method to see if a quantity is of that type of dimension?

This is what I have tried:

import pint
ureg = pint.UnitRegistry()

volume = 4.3 * ureg.gal

Doing this makes sense:

volume.dimensionality
Out[3]: <UnitsContainer({'[length]': 3.0})>

So I tried the "check" function but I don't know how to do it for volume:

volume.check('[length]', 3)

Unfortunately, this doesn't work:

Traceback (most recent call last):
  File "C:\Users\jle\...\interactiveshell.py", line 3291, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-4722a8cb0b0c>", line 1, in <module>
    volume.check('[length]', 3)
TypeError: check() takes 2 positional arguments but 3 were given
JasonArg123
  • 188
  • 1
  • 1
  • 14
  • 1
    Did you try `volume.check('[length]')`? Or `volume.check(3)`? – Scott Hunter Mar 26 '19 at 18:34
  • I tried both of these and they both return False. I would expect the check('[length]') to return false since that is not Length*length*length for volume. The second option "volume.check(3)" just returns False so I'm not even sure what that is doing. Thanks though! – JasonArg123 Mar 26 '19 at 18:40
  • Oh I figured it out. I need to do ```volume.check('[length]**3')``` and that worked – JasonArg123 Mar 26 '19 at 19:10

1 Answers1

2

You can check for volume using check('[volume]'):

import pint
ureg = pint.UnitRegistry()

volume = 4.3 * ureg.gal

# Returns True
volume.check('[volume]')

You can check for velocity using check('[length]/[time]'):

velocity = 1 * ureg.feet / ureg.second

# Returns True
velocity.check('[length]/[time]')
benwshul
  • 365
  • 3
  • 8