Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../lib/python2..../site-pac.../babel
File: units.py
# -- encoding: UTF-8 --
[0] Fix | Delete
[1] Fix | Delete
from babel._compat import string_types
[2] Fix | Delete
from babel.core import Locale
[3] Fix | Delete
from babel.numbers import format_decimal, LC_NUMERIC
[4] Fix | Delete
[5] Fix | Delete
[6] Fix | Delete
class UnknownUnitError(ValueError):
[7] Fix | Delete
def __init__(self, unit, locale):
[8] Fix | Delete
ValueError.__init__(self, "%s is not a known unit in %s" % (unit, locale))
[9] Fix | Delete
[10] Fix | Delete
[11] Fix | Delete
def get_unit_name(measurement_unit, length='long', locale=LC_NUMERIC):
[12] Fix | Delete
"""
[13] Fix | Delete
Get the display name for a measurement unit in the given locale.
[14] Fix | Delete
[15] Fix | Delete
>>> get_unit_name("radian", locale="en")
[16] Fix | Delete
'radians'
[17] Fix | Delete
[18] Fix | Delete
Unknown units will raise exceptions:
[19] Fix | Delete
[20] Fix | Delete
>>> get_unit_name("battery", locale="fi")
[21] Fix | Delete
Traceback (most recent call last):
[22] Fix | Delete
...
[23] Fix | Delete
UnknownUnitError: battery/long is not a known unit/length in fi
[24] Fix | Delete
[25] Fix | Delete
:param measurement_unit: the code of a measurement unit.
[26] Fix | Delete
Known units can be found in the CLDR Unit Validity XML file:
[27] Fix | Delete
http://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml
[28] Fix | Delete
[29] Fix | Delete
:param length: "short", "long" or "narrow"
[30] Fix | Delete
:param locale: the `Locale` object or locale identifier
[31] Fix | Delete
:return: The unit display name, or None.
[32] Fix | Delete
"""
[33] Fix | Delete
locale = Locale.parse(locale)
[34] Fix | Delete
unit = _find_unit_pattern(measurement_unit, locale=locale)
[35] Fix | Delete
if not unit:
[36] Fix | Delete
raise UnknownUnitError(unit=measurement_unit, locale=locale)
[37] Fix | Delete
return locale.unit_display_names.get(unit, {}).get(length)
[38] Fix | Delete
[39] Fix | Delete
[40] Fix | Delete
def _find_unit_pattern(unit_id, locale=LC_NUMERIC):
[41] Fix | Delete
"""
[42] Fix | Delete
Expand an unit into a qualified form.
[43] Fix | Delete
[44] Fix | Delete
Known units can be found in the CLDR Unit Validity XML file:
[45] Fix | Delete
http://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml
[46] Fix | Delete
[47] Fix | Delete
>>> _find_unit_pattern("radian", locale="en")
[48] Fix | Delete
'angle-radian'
[49] Fix | Delete
[50] Fix | Delete
Unknown values will return None.
[51] Fix | Delete
[52] Fix | Delete
>>> _find_unit_pattern("horse", locale="en")
[53] Fix | Delete
[54] Fix | Delete
:param unit_id: the code of a measurement unit.
[55] Fix | Delete
:return: A key to the `unit_patterns` mapping, or None.
[56] Fix | Delete
"""
[57] Fix | Delete
locale = Locale.parse(locale)
[58] Fix | Delete
unit_patterns = locale._data["unit_patterns"]
[59] Fix | Delete
if unit_id in unit_patterns:
[60] Fix | Delete
return unit_id
[61] Fix | Delete
for unit_pattern in sorted(unit_patterns, key=len):
[62] Fix | Delete
if unit_pattern.endswith(unit_id):
[63] Fix | Delete
return unit_pattern
[64] Fix | Delete
[65] Fix | Delete
[66] Fix | Delete
def format_unit(value, measurement_unit, length='long', format=None, locale=LC_NUMERIC):
[67] Fix | Delete
"""Format a value of a given unit.
[68] Fix | Delete
[69] Fix | Delete
Values are formatted according to the locale's usual pluralization rules
[70] Fix | Delete
and number formats.
[71] Fix | Delete
[72] Fix | Delete
>>> format_unit(12, 'length-meter', locale='ro_RO')
[73] Fix | Delete
u'12 metri'
[74] Fix | Delete
>>> format_unit(15.5, 'length-mile', locale='fi_FI')
[75] Fix | Delete
u'15,5 mailia'
[76] Fix | Delete
>>> format_unit(1200, 'pressure-inch-hg', locale='nb')
[77] Fix | Delete
u'1\\xa0200 tommer kvikks\\xf8lv'
[78] Fix | Delete
[79] Fix | Delete
Number formats may be overridden with the ``format`` parameter.
[80] Fix | Delete
[81] Fix | Delete
>>> from babel._compat import decimal
[82] Fix | Delete
>>> format_unit(decimal.Decimal("-42.774"), 'temperature-celsius', 'short', format='#.0', locale='fr')
[83] Fix | Delete
u'-42,8 \\xb0C'
[84] Fix | Delete
[85] Fix | Delete
The locale's usual pluralization rules are respected.
[86] Fix | Delete
[87] Fix | Delete
>>> format_unit(1, 'length-meter', locale='ro_RO')
[88] Fix | Delete
u'1 metru'
[89] Fix | Delete
>>> format_unit(0, 'length-picometer', locale='cy')
[90] Fix | Delete
u'0 picometr'
[91] Fix | Delete
>>> format_unit(2, 'length-picometer', locale='cy')
[92] Fix | Delete
u'2 bicometr'
[93] Fix | Delete
>>> format_unit(3, 'length-picometer', locale='cy')
[94] Fix | Delete
u'3 phicometr'
[95] Fix | Delete
[96] Fix | Delete
>>> format_unit(15, 'length-horse', locale='fi')
[97] Fix | Delete
Traceback (most recent call last):
[98] Fix | Delete
...
[99] Fix | Delete
UnknownUnitError: length-horse is not a known unit in fi
[100] Fix | Delete
[101] Fix | Delete
.. versionadded:: 2.2.0
[102] Fix | Delete
[103] Fix | Delete
:param value: the value to format. If this is a string, no number formatting will be attempted.
[104] Fix | Delete
:param measurement_unit: the code of a measurement unit.
[105] Fix | Delete
Known units can be found in the CLDR Unit Validity XML file:
[106] Fix | Delete
http://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml
[107] Fix | Delete
:param length: "short", "long" or "narrow"
[108] Fix | Delete
:param format: An optional format, as accepted by `format_decimal`.
[109] Fix | Delete
:param locale: the `Locale` object or locale identifier
[110] Fix | Delete
"""
[111] Fix | Delete
locale = Locale.parse(locale)
[112] Fix | Delete
[113] Fix | Delete
q_unit = _find_unit_pattern(measurement_unit, locale=locale)
[114] Fix | Delete
if not q_unit:
[115] Fix | Delete
raise UnknownUnitError(unit=measurement_unit, locale=locale)
[116] Fix | Delete
unit_patterns = locale._data["unit_patterns"][q_unit].get(length, {})
[117] Fix | Delete
[118] Fix | Delete
if isinstance(value, string_types): # Assume the value is a preformatted singular.
[119] Fix | Delete
formatted_value = value
[120] Fix | Delete
plural_form = "one"
[121] Fix | Delete
else:
[122] Fix | Delete
formatted_value = format_decimal(value, format, locale)
[123] Fix | Delete
plural_form = locale.plural_form(value)
[124] Fix | Delete
[125] Fix | Delete
if plural_form in unit_patterns:
[126] Fix | Delete
return unit_patterns[plural_form].format(formatted_value)
[127] Fix | Delete
[128] Fix | Delete
# Fall back to a somewhat bad representation.
[129] Fix | Delete
# nb: This is marked as no-cover, as the current CLDR seemingly has no way for this to happen.
[130] Fix | Delete
return '%s %s' % ( # pragma: no cover
[131] Fix | Delete
formatted_value,
[132] Fix | Delete
(get_unit_name(measurement_unit, length=length, locale=locale) or measurement_unit)
[133] Fix | Delete
)
[134] Fix | Delete
[135] Fix | Delete
[136] Fix | Delete
def _find_compound_unit(numerator_unit, denominator_unit, locale=LC_NUMERIC):
[137] Fix | Delete
"""
[138] Fix | Delete
Find a predefined compound unit pattern.
[139] Fix | Delete
[140] Fix | Delete
Used internally by format_compound_unit.
[141] Fix | Delete
[142] Fix | Delete
>>> _find_compound_unit("kilometer", "hour", locale="en")
[143] Fix | Delete
'speed-kilometer-per-hour'
[144] Fix | Delete
[145] Fix | Delete
>>> _find_compound_unit("mile", "gallon", locale="en")
[146] Fix | Delete
'consumption-mile-per-gallon'
[147] Fix | Delete
[148] Fix | Delete
If no predefined compound pattern can be found, `None` is returned.
[149] Fix | Delete
[150] Fix | Delete
>>> _find_compound_unit("gallon", "mile", locale="en")
[151] Fix | Delete
[152] Fix | Delete
>>> _find_compound_unit("horse", "purple", locale="en")
[153] Fix | Delete
[154] Fix | Delete
:param numerator_unit: The numerator unit's identifier
[155] Fix | Delete
:param denominator_unit: The denominator unit's identifier
[156] Fix | Delete
:param locale: the `Locale` object or locale identifier
[157] Fix | Delete
:return: A key to the `unit_patterns` mapping, or None.
[158] Fix | Delete
:rtype: str|None
[159] Fix | Delete
"""
[160] Fix | Delete
locale = Locale.parse(locale)
[161] Fix | Delete
[162] Fix | Delete
# Qualify the numerator and denominator units. This will turn possibly partial
[163] Fix | Delete
# units like "kilometer" or "hour" into actual units like "length-kilometer" and
[164] Fix | Delete
# "duration-hour".
[165] Fix | Delete
[166] Fix | Delete
numerator_unit = _find_unit_pattern(numerator_unit, locale=locale)
[167] Fix | Delete
denominator_unit = _find_unit_pattern(denominator_unit, locale=locale)
[168] Fix | Delete
[169] Fix | Delete
# If either was not found, we can't possibly build a suitable compound unit either.
[170] Fix | Delete
if not (numerator_unit and denominator_unit):
[171] Fix | Delete
return None
[172] Fix | Delete
[173] Fix | Delete
# Since compound units are named "speed-kilometer-per-hour", we'll have to slice off
[174] Fix | Delete
# the quantities (i.e. "length", "duration") from both qualified units.
[175] Fix | Delete
[176] Fix | Delete
bare_numerator_unit = numerator_unit.split("-", 1)[-1]
[177] Fix | Delete
bare_denominator_unit = denominator_unit.split("-", 1)[-1]
[178] Fix | Delete
[179] Fix | Delete
# Now we can try and rebuild a compound unit specifier, then qualify it:
[180] Fix | Delete
[181] Fix | Delete
return _find_unit_pattern("%s-per-%s" % (bare_numerator_unit, bare_denominator_unit), locale=locale)
[182] Fix | Delete
[183] Fix | Delete
[184] Fix | Delete
def format_compound_unit(
[185] Fix | Delete
numerator_value, numerator_unit=None,
[186] Fix | Delete
denominator_value=1, denominator_unit=None,
[187] Fix | Delete
length='long', format=None, locale=LC_NUMERIC
[188] Fix | Delete
):
[189] Fix | Delete
"""
[190] Fix | Delete
Format a compound number value, i.e. "kilometers per hour" or similar.
[191] Fix | Delete
[192] Fix | Delete
Both unit specifiers are optional to allow for formatting of arbitrary values still according
[193] Fix | Delete
to the locale's general "per" formatting specifier.
[194] Fix | Delete
[195] Fix | Delete
>>> format_compound_unit(7, denominator_value=11, length="short", locale="pt")
[196] Fix | Delete
'7/11'
[197] Fix | Delete
[198] Fix | Delete
>>> format_compound_unit(150, "kilometer", denominator_unit="hour", locale="sv")
[199] Fix | Delete
'150 kilometer per timme'
[200] Fix | Delete
[201] Fix | Delete
>>> format_compound_unit(150, "kilowatt", denominator_unit="year", locale="fi")
[202] Fix | Delete
'150 kilowattia vuodessa'
[203] Fix | Delete
[204] Fix | Delete
>>> format_compound_unit(32.5, "ton", 15, denominator_unit="hour", locale="en")
[205] Fix | Delete
'32.5 tons per 15 hours'
[206] Fix | Delete
[207] Fix | Delete
>>> format_compound_unit(160, denominator_unit="square-meter", locale="fr")
[208] Fix | Delete
'160 par m\\xe8tre carr\\xe9'
[209] Fix | Delete
[210] Fix | Delete
>>> format_compound_unit(4, "meter", "ratakisko", length="short", locale="fi")
[211] Fix | Delete
'4 m/ratakisko'
[212] Fix | Delete
[213] Fix | Delete
>>> format_compound_unit(35, "minute", denominator_unit="fathom", locale="sv")
[214] Fix | Delete
'35 minuter per famn'
[215] Fix | Delete
[216] Fix | Delete
>>> from babel.numbers import format_currency
[217] Fix | Delete
>>> format_compound_unit(format_currency(35, "JPY", locale="de"), denominator_unit="liter", locale="de")
[218] Fix | Delete
'35\\xa0\\xa5 pro Liter'
[219] Fix | Delete
[220] Fix | Delete
See http://www.unicode.org/reports/tr35/tr35-general.html#perUnitPatterns
[221] Fix | Delete
[222] Fix | Delete
:param numerator_value: The numerator value. This may be a string,
[223] Fix | Delete
in which case it is considered preformatted and the unit is ignored.
[224] Fix | Delete
:param numerator_unit: The numerator unit. See `format_unit`.
[225] Fix | Delete
:param denominator_value: The denominator value. This may be a string,
[226] Fix | Delete
in which case it is considered preformatted and the unit is ignored.
[227] Fix | Delete
:param denominator_unit: The denominator unit. See `format_unit`.
[228] Fix | Delete
:param length: The formatting length. "short", "long" or "narrow"
[229] Fix | Delete
:param format: An optional format, as accepted by `format_decimal`.
[230] Fix | Delete
:param locale: the `Locale` object or locale identifier
[231] Fix | Delete
:return: A formatted compound value.
[232] Fix | Delete
"""
[233] Fix | Delete
locale = Locale.parse(locale)
[234] Fix | Delete
[235] Fix | Delete
# Look for a specific compound unit first...
[236] Fix | Delete
[237] Fix | Delete
if numerator_unit and denominator_unit and denominator_value == 1:
[238] Fix | Delete
compound_unit = _find_compound_unit(numerator_unit, denominator_unit, locale=locale)
[239] Fix | Delete
if compound_unit:
[240] Fix | Delete
return format_unit(numerator_value, compound_unit, length=length, format=format, locale=locale)
[241] Fix | Delete
[242] Fix | Delete
# ... failing that, construct one "by hand".
[243] Fix | Delete
[244] Fix | Delete
if isinstance(numerator_value, string_types): # Numerator is preformatted
[245] Fix | Delete
formatted_numerator = numerator_value
[246] Fix | Delete
elif numerator_unit: # Numerator has unit
[247] Fix | Delete
formatted_numerator = format_unit(
[248] Fix | Delete
numerator_value, numerator_unit, length=length, format=format, locale=locale
[249] Fix | Delete
)
[250] Fix | Delete
else: # Unitless numerator
[251] Fix | Delete
formatted_numerator = format_decimal(numerator_value, format=format, locale=locale)
[252] Fix | Delete
[253] Fix | Delete
if isinstance(denominator_value, string_types): # Denominator is preformatted
[254] Fix | Delete
formatted_denominator = denominator_value
[255] Fix | Delete
elif denominator_unit: # Denominator has unit
[256] Fix | Delete
if denominator_value == 1: # support perUnitPatterns when the denominator is 1
[257] Fix | Delete
denominator_unit = _find_unit_pattern(denominator_unit, locale=locale)
[258] Fix | Delete
per_pattern = locale._data["unit_patterns"].get(denominator_unit, {}).get(length, {}).get("per")
[259] Fix | Delete
if per_pattern:
[260] Fix | Delete
return per_pattern.format(formatted_numerator)
[261] Fix | Delete
# See TR-35's per-unit pattern algorithm, point 3.2.
[262] Fix | Delete
# For denominator 1, we replace the value to be formatted with the empty string;
[263] Fix | Delete
# this will make `format_unit` return " second" instead of "1 second".
[264] Fix | Delete
denominator_value = ""
[265] Fix | Delete
[266] Fix | Delete
formatted_denominator = format_unit(
[267] Fix | Delete
denominator_value, denominator_unit, length=length, format=format, locale=locale
[268] Fix | Delete
).strip()
[269] Fix | Delete
else: # Bare denominator
[270] Fix | Delete
formatted_denominator = format_decimal(denominator_value, format=format, locale=locale)
[271] Fix | Delete
[272] Fix | Delete
per_pattern = locale._data["compound_unit_patterns"].get("per", {}).get(length, "{0}/{1}")
[273] Fix | Delete
[274] Fix | Delete
return per_pattern.format(formatted_numerator, formatted_denominator)
[275] Fix | Delete
[276] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function