Question: Is there a way to return a list of direct conversion options? #1697
Answered
by
keewis
Nathan-Harlan
asked this question in
Q&A
-
Looking for a way to see all available direct conversions, especially for basic dimensions. I realize this could get messy if every possible option was returned. For Example
Thanks for all the effort on a great library btw! |
Beta Was this translation helpful? Give feedback.
Answered by
keewis
Jan 12, 2023
Replies: 1 comment 1 reply
-
you can generate that list yourself: In [13]: import pint
...:
...: ureg = pint.UnitRegistry()
...:
...: def direct_conversions(ureg, unit):
...: def unit_dimensionality(ureg, name):
...: unit = getattr(ureg, name, None)
...:
...: if unit is None or not isinstance(unit, pint.Unit):
...: return {}
...:
...: return unit.dimensionality
...:
...: if isinstance(unit, str):
...: unit = ureg.parse_units(unit)
...:
...: return [
...: name
...: for name in ureg
...: if unit.dimensionality == unit_dimensionality(ureg, name)
...: ]
...:
...: conversions = direct_conversions(ureg, "m / s ** 2")
...: {name: getattr(ureg, name).dimensionality for name in conversions}
Out[13]:
{'Gal': <UnitsContainer({'[length]': 1, '[time]': -2})>,
'g0': <UnitsContainer({'[length]': 1, '[time]': -2})>,
'g_0': <UnitsContainer({'[length]': 1, '[time]': -2})>,
'g_n': <UnitsContainer({'[length]': 1, '[time]': -2})>,
'galileo': <UnitsContainer({'[length]': 1, '[time]': -2})>,
'gravity': <UnitsContainer({'[length]': 1, '[time]': -2})>,
'standard_gravity': <UnitsContainer({'[length]': 1, '[time]': -2})>} Edit: although this suggests converting between |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Nathan-Harlan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can generate that list yourself: