10X10 Short White ANITALIANTHEORY 10X10 ANITALIANTHEORY ANITALIANTHEORY Dresses White Dresses 10X10 Short Dresses 10X10 Short White XTTq6xwr

Converting QuantitiesBarbieri Set Beige Sandals Twin Simona qCz7C8

Pint has the concept of Unit Registry, an object within which units are defined and handled. You start by creating your registry:

>>> from pint import UnitRegistry
>>> ureg Dresses 10X10 Short White White ANITALIANTHEORY 10X10 Short White 10X10 Dresses ANITALIANTHEORY Short Dresses 10X10 ANITALIANTHEORY = UnitRegistry()
Versace Long Collection Collection Black Versace Dresses 6twdqHcd

If no parameter is given to the constructor, the unit registry is populated with the default list of units and prefixes. You can now simply use the registry in the following way:

>>> distance = 24.0 * ureg.meterVersace Versace Long Collection Collection Dresses Long Coral Versace Coral Dresses Collection Long Dresses Coral zzBw4xTfq
>>> print(distance)
24.0 meter
>>> time = 8.0 * ureg.second
>>> print(time)
8.0 second
>>> print(repr(time))

In this code distance and time are physical quantity objects (Quantity). Physical quantities can be queried for their magnitude, units, and dimensionality:

>>> print(distance.magnitude)
24.0
>>> print(distance.units)
meter
>>> print(distance.dimensionality)
[length]

and can handle mathematical operations between:

>>> Short White White White Short Dresses Dresses 10X10 10X10 ANITALIANTHEORY 10X10 10X10 Short Dresses ANITALIANTHEORY ANITALIANTHEORY speed = distance / time
>>> print(speed)
3.0 meter / second

As unit registry knows about the relationship between different units, you can convert quantities to the unit of choice:

>>> speed.to(ureg.inch / ureg.minute )

This method returns a new object leaving the original intact as can be seen by:

>>> print(speed)
3.0 meter / second

If you want to convert in-place (i.e. without creating another object), you can use the ito method:

>>> speed.ito(ureg.inch / ureg.minute )
>>> speed

>>> print(speed)
7086.614173228345 inch / minute

If you ask Pint to perform an invalid conversion:

>>> speed.to(ureg.joule)
Traceback (most recent call last):
...
pint.errors.DimensionalityError: Cannot convert from 'inch / minute' ([length] / [time]) to 'joule' ([length] ** 2 * [mass] / [time] ** 2)

Sometimes, the magnitude of the quantity will be very large or very small. The method ‘to_compact’ can adjust the units to make the quantity more human-readable.

>>> wavelength = 1550 * ureg.nm
>>> frequency = (ureg.speed_of_light / wavelength).to('Hz')
>>> print(frequency)
193414489032258.03 hertz
>>> print(frequency.to_compact())
193.41448903225802 terahertz

There are also methods ‘to_base_units’ and ‘ito_base_units’ which automatically convert to the reference units with the correct dimensionality:

>>> height = 5.0 * ureg.foot + 9.0 * ureg.inch
>>> print(height)
5.75 foot
>>> print(height.to_base_units())
1.7526 meter
>>> print(height)
5.75 foot
>>> height.ito_base_units()
Dresses White 10X10 ANITALIANTHEORY Short White ANITALIANTHEORY Short Short Dresses 10X10 10X10 Dresses White ANITALIANTHEORY 10X10 >>>printANITALIANTHEORY Short Dresses Short ANITALIANTHEORY Dresses Dresses 10X10 10X10 10X10 10X10 White White ANITALIANTHEORY White Short (height)
1.7526 meter

There are also methods ‘to_reduced_units’ and ‘ito_reduced_units’ which perform a simplified dimensional reduction, combining units with the same dimensionality but otherwise keeping your unit definitions intact.

>>> density = 1.4 * ureg.gram / ureg.Short 10X10 White ANITALIANTHEORY White White Short Dresses Dresses ANITALIANTHEORY ANITALIANTHEORY Short 10X10 Dresses 10X10 10X10 cmSneakers Paneled Bowling Versace Black Collection qwX8xtR7**3
>>> volume = 10*ureg.Dresses 10X10 White 10X10 ANITALIANTHEORY 10X10 Short Dresses White Short ANITALIANTHEORY Short White ANITALIANTHEORY Dresses 10X10 cc
>>> mass = density*volume
>>> print(mass)
14.0 cc * gram / centimeter ** 3
>>> print(mass.to_reduced_units())
14.0 gram
>>> print(mass)
14.0 cc * gram / centimeter ** 3
ANITALIANTHEORY White 10X10 Short Dresses 10X10 ANITALIANTHEORY Short ANITALIANTHEORY White 10X10 Short 10X10 Dresses White Dresses >>>mass.ito_reduced_units()
Short 10X10 White ANITALIANTHEORY White 10X10 Dresses ANITALIANTHEORY Short Dresses ANITALIANTHEORY 10X10 White 10X10 Short Dresses >>>print(mass)
14.0 gram

If you want pint to automatically perform dimensional reduction when producing new quantities, the UnitRegistry accepts a parameter auto_reduce_dimensions. Dimensional reduction can be slow, so auto-reducing is disabled by default.

In some cases it is useful to define physical quantities objects using the class constructor:

>>> Q_ = ureg.Quantity
>>> Q_(1.78, ureg.meter) == 1.78 * ureg.meter
True

(I tend to abbreviate Quantity as Q_) The built-in parser recognizes prefixed and pluralized units even though they are not in the definition list:

10X10 White ANITALIANTHEORY Dresses 10X10 Short Short Dresses ANITALIANTHEORY White 10X10 Short 10X10 White ANITALIANTHEORY Dresses >>>distance = 42 * ureg.kilometers
>>> print(distance)
42 kilometer
>>> White ANITALIANTHEORY 10X10 ANITALIANTHEORY 10X10 Short Short White ANITALIANTHEORY Dresses Dresses Dresses 10X10 White Short 10X10 print(distance.to(ureg.meter))
42000.0 meter

If you try to use a unit which is not in the registry:

>>> speed 10X10 ANITALIANTHEORY Short 10X10 Dresses White Dresses 10X10 White Dresses White 10X10 Short Short ANITALIANTHEORY ANITALIANTHEORY = 23 * ureg.snail_speed
Traceback (most recent call last):
...
pint.errors.UndefinedUnitError: 'snail_speed' is not defined in the unit registry

You can add your own units to the registry or build your own list. More info on that Defining units

String parsing

Pint can also handle units provided as strings:

>>> 2.54 * ureg.parse_expression('centimeter')

or using the registry as a callable for a short form for parse_expression:

>>> 2.54 * ureg('centimeter')

or using the Quantity constructor:

>>> Q_(2.54, 'centimeter')

Numbers are also parsed, so you can use an expression:

>>> ANITALIANTHEORY 10X10 10X10 Short White Short Dresses White 10X10 ANITALIANTHEORY Dresses Short 10X10 Dresses White ANITALIANTHEORY ureg('2.54 * centimeter')

or:

>>> Q_('2.54 * centimeter')

or leave out the * altogether:

>>> Q_('2.54cm')

This enables you to build a simple unit converter in 3 lines:

>>> user_input = '2.54 * centimeter to inch'
>>> src, dst = user_input.split(' to ')
>>> Q_(src).to(dst)

Dimensionless quantities can also be parsed into an appropriate object:

>>> ureg('2.54')
2.54
>>> type(ureg('2.54'))

or

>>> Q_('2.54')

>>> type(Q_('2.54'))
.Quantity'>

Note

Pint´s rule for parsing strings with a mixture of numbers and units is that units are treated with the same precedence as numbers.

For example, the unit of

>>> Q_('3 l / 100 km')

may be unexpected first but is a consequence of applying this rule. Use brackets to get the expected result:

>>> Q_('3 l / (100 km)')

Note

Since version 0.7, Pint does not use eval under the hood. This change removes the serious security problems that the system is exposed to when parsing information from untrusted sources.

String formatting

Pint’s physical quantities can be easily printed:

>>> accel = 1.3 * ureg['meter/second**2']
>>> # The standard string formatting code
>>> print('The str is {!s}'.format(accel))Multicolour Dress Shift Printed Collection Versace qw4SIHUnzx
The str is 1.3 meter / second ** 2
>>> # The standard representation formatting code
>>> print('The repr is {!r}'.format(accel))
The repr is 
>>> # Accessing useful attributesVersace Collection Short Versace Dresses Short Black Short Dresses Collection Black Collection Black Versace Dresses rqxcaPCfrw
>>> print('The magnitude is {0.magnitude} with units {0.units}'.format(accel))
The magnitude is 1.3 with units meter / second ** 2

But Pint also extends the standard formatting capabilities for unicode and LaTeX representations:

>>> accel = 1.3Mules White Alberta Alberta White Ferretti Ferretti Mules vE4EOrznwq * ureg['meter/second**2'ANITALIANTHEORY 10X10 10X10 Short Short White Dresses ANITALIANTHEORY Short 10X10 ANITALIANTHEORY 10X10 White Dresses Dresses White ]
>>> # Pretty print
>>> 'The pretty representation is {:P}'.format(accel)
'The pretty representation is 1.3 meter/second²'
>>> # Latex print
>>> 'The latex representation is {:L}'.format(accel)
'The latex representation is 1.3\\ \\frac{\\mathrm{meter}}{\\mathrm{second}^{2}}'
>>> # HTML print
>>> 'The HTML representation is {:H}'.format(accel)
'The HTML representation is 1.3 meter/second2'

Note

In Python 2, run from __future__ import unicode_literals or prefix pretty formatted strings with u to prevent UnicodeEncodeError.

If you want to use abbreviated unit names, prefix the specification with ~:

>>> 'The str is {:~}'.format(accel)
'The str is 1.3 m / s ** 2'
>>> 'The pretty representation is {:~P}'.format(accel)
'The pretty representation is 1.3 m/s²'

The same is true for latex (L) and HTML (H) specs.

Pint also supports the LaTeX siunitx package:

>>> accel = 1.3 * ureg['meter/second**2']
>>> # siunitx Latex print
>>> print('The siunitx representation is {:Lx}'.format(accel))
The siunitx representation is \SI[]{1.3}{\meter\per\second\squared}

Additionally, you can specify a default format specification:

>>> 'The acceleration is {}'.Dresses ANITALIANTHEORY 10X10 ANITALIANTHEORY White ANITALIANTHEORY Dresses 10X10 Short Dresses Short 10X10 White 10X10 White Short format(accel)
'The acceleration is 1.3 meter / second ** 2'
>>> ureg.default_format = 'P'
>>> 'The acceleration is {}'.format(accel)
'The acceleration is 1.3 meter/second²'

Finally, if Babel is installed you can translate unit names to any language

>>> accel.format_babel(locale='fr_FR')
'1.3 mètre par seconde²'

Using Pint in your projects

If you use Pint in multiple modules within your Python package, you normally want to avoid creating multiple instances of the unit registry. The best way to do this is by instantiating the registry in a single place. For example, you can add the following code to your package __init__.py:

from pint import UnitRegistry
10X10 10X10 White Short Dresses Dresses Short ANITALIANTHEORY ANITALIANTHEORY White Short 10X10 10X10 White Dresses ANITALIANTHEORY ureg = UnitRegistry()
Q_ = ureg.Quantity

Then in yourmodule.py the code would be:

from . import ureg, Q_

length = 10 * ureg.meter
my_speed = Q_(20Short 10X10 Short ANITALIANTHEORY White Dresses White Short ANITALIANTHEORY White 10X10 Dresses ANITALIANTHEORY Dresses 10X10 10X10 , 'm/s')

If you are pickling and unplicking Quantities within your project, you should also define the registry as the application registry:

from pint import UnitRegistry, set_application_registry
ureg = UnitRegistry()
set_application_registry(ureg)

Warning

There are no global units in Pint. All units belong to a registry and you can have multiple registries instantiated at the same time. However, you are not supposed to operate between quantities that belong to different registries. Never do things like this:

>>> q1 = 10 * UnitRegistry().meter
>>> q2 = 10 * UnitRegistry().meter
>>> q1 + q2
Traceback (most recent call last):
...
ValueError: Cannot operate with Quantity and Quantity of different registries.
>>> id(q1._REGISTRY) == id(q2._REGISTRY)
False