I have a pint quantity and I want the string to appear with unit abbreviations (i.e. SI prefixes and unit letters), but without a /
to denote a division.
So in other words I want the string to appear as
'cm g µs⁻¹'
The built-in pretty formatting gets me almost what I want, but I'd like to customize it so that multiplication is indicated by a space and denominator quantities are indicated by negative exponents.
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> u = ureg['g cm / us']
>>> f"{u.units:~P}"
'cm·g/µs'
I figured out a way to do what I want by looking at the pint source code, but it involves accessing the private attribute _units
of the units class so I'm wondering if there's a better way
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> u = ureg['g cm / us']
>>> abbrv_units = {ureg.get_symbol(k) : v for k, v in u.units._units.items()}
>>> pint.formatting.format_unit(abbrv_units, 'P', as_ratio=False, product_fmt=' ')
'cm g µs⁻¹'
I guess it would be nice if I could use the ~
format specifier or some sort of function to abbreviate the strings.