Skip to content

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
Discussion options

You must be logged in to vote

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)
    ...:     ]
…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Nathan-Harlan
Comment options

Answer selected by Nathan-Harlan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants