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
4

1 回答 1

1

您可以使用以下命令检查音量check('[volume]')

import pint
ureg = pint.UnitRegistry()

volume = 4.3 * ureg.gal

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

您可以使用以下方法检查速度check('[length]/[time]')

velocity = 1 * ureg.feet / ureg.second

# Returns True
velocity.check('[length]/[time]')
于 2021-10-14T17:55:53.350 回答