Say I define a new dimension and define new units along that dimension. In this example, I use currencies and made-up exchange rates, but could be any other custom-made dimension:
import pint
ureg = pint.UnitRegistry()
Q_ = ureg.Quantity
import io
ctx_def = io.StringIO("""\
EUR = [currency]
DKK = 0.14 EUR
JPY = 0.01 EUR
USD = 0.9 EUR
GBP = 1.1 EUR
""")
ureg.load_definitions(ctx_def)
Here, EUR
is the base unit, and conversion to this base unit works fine:
Q_(42, "JPY").to_base_units()
# returns 0.42 EUR as expected
My question is: Given this unit registry, and given the custom dimension name as input i.e. "[currency]"
, how do I get the base unit "EUR"
?
If it's a built-in dimension such as [mass]
, then I can do this (not elegant, but works):
ureg.get_base_units(list(ureg.get_compatible_units("[mass]"))[0])[1]
# returns "kilogram"
However, this trick doesn't work for my custom-made dimension [currency]
:
ureg.get_base_units(list(ureg.get_compatible_units("[currency]"))[0])[1]
# raises:
# KeyError: <UnitsContainer({'[currency]': 1})>