module documentation

Undocumented

Function format​_compound​_unit Format a compound number value, i.e. "kilometers per hour" or similar.
Function format​_unit Format a value of a given unit.
Function get​_unit​_name Get the display name for a measurement unit in the given locale.
Class ​Unknown​Unit​Error Undocumented
Function ​_find​_compound​_unit Find a predefined compound unit pattern.
Function ​_find​_unit​_pattern Expand an unit into a qualified form.
def format_compound_unit(numerator_value, numerator_unit=None, denominator_value=1, denominator_unit=None, length='long', format=None, locale=LC_NUMERIC):

Format a compound number value, i.e. "kilometers per hour" or similar.

Both unit specifiers are optional to allow for formatting of arbitrary values still according to the locale's general "per" formatting specifier.

>>> format_compound_unit(7, denominator_value=11, length="short", locale="pt")
'7/11'
>>> format_compound_unit(150, "kilometer", denominator_unit="hour", locale="sv")
'150 kilometer per timme'
>>> format_compound_unit(150, "kilowatt", denominator_unit="year", locale="fi")
'150 kilowattia / vuosi'
>>> format_compound_unit(32.5, "ton", 15, denominator_unit="hour", locale="en")
'32.5 tons per 15 hours'
>>> format_compound_unit(160, denominator_unit="square-meter", locale="fr")
'160 par m\xe8tre carr\xe9'
>>> format_compound_unit(4, "meter", "ratakisko", length="short", locale="fi")
'4 m/ratakisko'
>>> format_compound_unit(35, "minute", denominator_unit="fathom", locale="sv")
'35 minuter per famn'
>>> from babel.numbers import format_currency
>>> format_compound_unit(format_currency(35, "JPY", locale="de"), denominator_unit="liter", locale="de")
'35\xa0\xa5 pro Liter'

See https://www.unicode.org/reports/tr35/tr35-general.html#perUnitPatterns

Parameters
numerator​_valueThe numerator value. This may be a string, in which case it is considered preformatted and the unit is ignored.
numerator​_unitThe numerator unit. See format_unit.
denominator​_valueThe denominator value. This may be a string, in which case it is considered preformatted and the unit is ignored.
denominator​_unitThe denominator unit. See format_unit.
lengthThe formatting length. "short", "long" or "narrow"
formatAn optional format, as accepted by format_decimal.
localethe Locale object or locale identifier
Returns
A formatted compound value.
def format_unit(value, measurement_unit, length='long', format=None, locale=LC_NUMERIC):

Format a value of a given unit.

Values are formatted according to the locale's usual pluralization rules and number formats.

>>> format_unit(12, 'length-meter', locale='ro_RO')
u'12 metri'
>>> format_unit(15.5, 'length-mile', locale='fi_FI')
u'15,5 mailia'
>>> format_unit(1200, 'pressure-millimeter-ofhg', locale='nb')
u'1\xa0200 millimeter kvikks\xf8lv'
>>> format_unit(270, 'ton', locale='en')
u'270 tons'

Number formats may be overridden with the format parameter.

>>> from babel._compat import decimal
>>> format_unit(decimal.Decimal("-42.774"), 'temperature-celsius', 'short', format='#.0', locale='fr')
u'-42,8\u202f\xb0C'

The locale's usual pluralization rules are respected.

>>> format_unit(1, 'length-meter', locale='ro_RO')
u'1 metru'
>>> format_unit(0, 'length-mile', locale='cy')
u'0 mi'
>>> format_unit(1, 'length-mile', locale='cy')
u'1 filltir'
>>> format_unit(3, 'length-mile', locale='cy')
u'3 milltir'
>>> format_unit(15, 'length-horse', locale='fi')
Traceback (most recent call last):
    ...
UnknownUnitError: length-horse is not a known unit in fi
New in version 2.2.0.
Parameters
valuethe value to format. If this is a string, no number formatting will be attempted.
measurement​_unitthe code of a measurement unit. Known units can be found in the CLDR Unit Validity XML file: https://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml
length"short", "long" or "narrow"
formatAn optional format, as accepted by format_decimal.
localethe Locale object or locale identifier
def get_unit_name(measurement_unit, length='long', locale=LC_NUMERIC):

Get the display name for a measurement unit in the given locale.

>>> get_unit_name("radian", locale="en")
'radians'

Unknown units will raise exceptions:

>>> get_unit_name("battery", locale="fi")
Traceback (most recent call last):
    ...
UnknownUnitError: battery/long is not a known unit/length in fi
Parameters
measurement​_unitthe code of a measurement unit. Known units can be found in the CLDR Unit Validity XML file: https://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml
length"short", "long" or "narrow"
localethe Locale object or locale identifier
Returns
The unit display name, or None.
def _find_compound_unit(numerator_unit, denominator_unit, locale=LC_NUMERIC):

Find a predefined compound unit pattern.

Used internally by format_compound_unit.

>>> _find_compound_unit("kilometer", "hour", locale="en")
'speed-kilometer-per-hour'
>>> _find_compound_unit("mile", "gallon", locale="en")
'consumption-mile-per-gallon'

If no predefined compound pattern can be found, None is returned.

>>> _find_compound_unit("gallon", "mile", locale="en")
>>> _find_compound_unit("horse", "purple", locale="en")
Parameters
numerator​_unitThe numerator unit's identifier
denominator​_unitThe denominator unit's identifier
localethe Locale object or locale identifier
Returns
str|NoneA key to the unit_patterns mapping, or None.
def _find_unit_pattern(unit_id, locale=LC_NUMERIC):

Expand an unit into a qualified form.

Known units can be found in the CLDR Unit Validity XML file: https://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml

>>> _find_unit_pattern("radian", locale="en")
'angle-radian'

Unknown values will return None.

>>> _find_unit_pattern("horse", locale="en")
Parameters
unit​_idthe code of a measurement unit.
localeUndocumented
Returns
A key to the unit_patterns mapping, or None.