Search code examples
pythonpint

How to print python Pint quantities and its meaning


I am very new to python and python pint. I just want to know the supportable units of python pint. Also what is the meaning of the below representation:

{length: 1, time: -1}

Reference from pint unit. Also how do I print the available pint units for energy and temperature.

from pint import UnitRegistry
ureg = pint.UnitRegistry()
ureg.Unit

Solution

  • The keys of the dictionary are the dimensions, and the corresponding value is the power of that dimension. That is, {length: 1, time: -1} is the representation of velocity, and an example of a unit whose dimensionality is represented by the dictionary would be m/s. For acceleration, the representation would be {length: 1, time: -2}:

    In [13]: ureg.Unit('m/s').dimensionality
    Out[13]: <UnitsContainer({'[length]': 1, '[time]': -1})>
    
    In [14]: ureg.Unit('m/s^2').dimensionality
    Out[14]: <UnitsContainer({'[length]': 1, '[time]': -2})>
    

    To get available units of a given dimensionality, use UnitRegistry.get_compatible_units; for instance, for your two examples:

    In [9]: ureg.get_compatible_units('[energy]')
    Out[9]:
    frozenset({<Unit('electron_volt')>,
               <Unit('rydberg')>,
               <Unit('hartree')>,
               <Unit('erg')>,
               <Unit('joule')>,
               <Unit('foot_pound')>,
               <Unit('calorie')>,
               <Unit('fifteen_degree_calorie')>,
               <Unit('international_calorie')>,
               <Unit('atmosphere_liter')>,
               <Unit('thermochemical_british_thermal_unit')>,
               <Unit('international_british_thermal_unit')>,
               <Unit('british_thermal_unit')>,
               <Unit('watt_hour')>,
               <Unit('US_therm')>,
               <Unit('therm')>,
               <Unit('ton_TNT')>,
               <Unit('tonne_of_oil_equivalent')>,
               <Unit('quadrillion_Btu')>})
    
    In [10]: ureg.get_compatible_units('[temperature]')
    Out[10]:
    frozenset({<Unit('degree_Rankine')>,
               <Unit('kelvin')>,
               <Unit('degree_Fahrenheit')>,
               <Unit('degree_Reaumur')>,
               <Unit('degree_Celsius')>,
               <Unit('atomic_unit_of_temperature')>,
               <Unit('planck_temperature')>})