babel.numbers ~~~~~~~~~~~~~ CLDR Plural support. See UTS #35. :copyright: (c) 2013-2023 by the Babel Team. :license: BSD, see LICENSE for more details.
Class |
|
Represents a set of language pluralization rules. The constructor accepts a list of (tag, expr) tuples or a dict of `CLDR rules`_. The resulting object is callable and accepts one parameter with a positive or negative number (both integer and float) for the number that indicates the plural form for a string and returns the tag for the format:... |
Exception |
|
Raised if a rule is malformed. |
Function | cldr |
Javaish modulo. This modulo operator returns the value with the sign of the dividend rather than the divisor like Python does: |
Function | extract |
Extract operands from a decimal, a float or an int, according to `CLDR rules`_. |
Function | ident |
Undocumented |
Function | in |
Integer range list test. This is the callback for the "in" operator of the UTS #35 pluralization rule language: |
Function | negate |
Undocumented |
Function | range |
Undocumented |
Function | skip |
Undocumented |
Function | test |
Undocumented |
Function | to |
The plural rule as gettext expression. The gettext expression is technically limited to integers and returns indices rather than tags. |
Function | to |
Convert a list/dict of rules or a `PluralRule` object into a JavaScript function. This function depends on no external library: |
Function | to |
Convert a list/dict of rules or a `PluralRule` object into a regular Python function. This is useful in situations where you need a real function and don't are about the actual rule object: |
Function | tokenize |
Undocumented |
Function | value |
Undocumented |
Function | within |
Float range test. This is the callback for the "within" operator of the UTS #35 pluralization rule language: |
Variable | compile |
Undocumented |
Class | _ |
The compilers are able to transform the expressions into multiple output formats. |
Class | _ |
Compile into a gettext plural expression. |
Class | _ |
Compiles the expression to plain of JavaScript. |
Class | _ |
Internal parser. This class can translate a single rule into an abstract tree of tuples. It implements the following grammar:: |
Class | _ |
Compiles an expression to Python. |
Class | _ |
Returns a unicode pluralization rule again. |
Function | _binary |
Compiler factory for the `_Compiler`. |
Function | _unary |
Compiler factory for the `_Compiler`. |
Constant | _RULES |
Undocumented |
Constant | _VARS |
Undocumented |
Variable | _fallback |
Undocumented |
Variable | _plural |
Undocumented |
Javaish modulo. This modulo operator returns the value with the sign of the dividend rather than the divisor like Python does: >>> cldr_modulo(-3, 5) -3 >>> cldr_modulo(-3, -5) -3 >>> cldr_modulo(3, 5) 3
float|decimal.Decimal
) -> tuple[ decimal.Decimal|int, int, int, int, int, int, Literal[ 0], Literal[ 0]]
:
(source)
¶
Extract operands from a decimal, a float or an int, according to `CLDR rules`_. The result is a 8-tuple (n, i, v, w, f, t, c, e), where those symbols are as follows: ====== =============================================================== Symbol Value ------ --------------------------------------------------------------- n absolute value of the source number (integer and decimals). i integer digits of n. v number of visible fraction digits in n, with trailing zeros. w number of visible fraction digits in n, without trailing zeros. f visible fractional digits in n, with trailing zeros. t visible fractional digits in n, without trailing zeros. c compact decimal exponent value: exponent of the power of 10 used in compact decimal formatting. e currently, synonym for ‘c’. however, may be redefined in the future. ====== =============================================================== .. _`CLDR rules`: https://www.unicode.org/reports/tr35/tr35-61/tr35-numbers.html#Operands :param source: A real number :type source: int|float|decimal.Decimal :return: A n-i-v-w-f-t-c-e tuple :rtype: tuple[decimal.Decimal, int, int, int, int, int, int, int]
float|decimal.Decimal
, range_list: Iterable[ Iterable[ float|decimal.Decimal]]
) -> bool
:
(source)
¶
Integer range list test. This is the callback for the "in" operator of the UTS #35 pluralization rule language: >>> in_range_list(1, [(1, 3)]) True >>> in_range_list(3, [(1, 3)]) True >>> in_range_list(3, [(1, 3), (5, 8)]) True >>> in_range_list(1.2, [(1, 4)]) False >>> in_range_list(10, [(1, 4)]) False >>> in_range_list(10, [(1, 4), (6, 8)]) False
Iterable[ Iterable[ float|decimal.Decimal]]
) -> tuple[ Literal[ 'range_list'], Iterable[ Iterable[ float|decimal.Decimal]]]
:
(source)
¶
Undocumented
list[ tuple[ str, str]]
, type_: str
, value: str|None
= None) -> list[ tuple[ str, str]]|bool
:
(source)
¶
Undocumented
The plural rule as gettext expression. The gettext expression is technically limited to integers and returns indices rather than tags. >>> to_gettext({'one': 'n is 1', 'two': 'n is 2'}) 'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2);' :param rule: the rules as list or dict, or a `PluralRule` object :raise RuleError: if the expression is malformed
(Mapping[ str, str]|Iterable[ tuple[ str, str]])|PluralRule
) -> str
:
(source)
¶
Convert a list/dict of rules or a `PluralRule` object into a JavaScript function. This function depends on no external library: >>> to_javascript({'one': 'n is 1'}) "(function(n) { return (n == 1) ? 'one' : 'other'; })" Implementation detail: The function generated will probably evaluate expressions involved into range operations multiple times. This has the advantage that external helper functions are not required and is not a big performance hit for these simple calculations. :param rule: the rules as list or dict, or a `PluralRule` object :raise RuleError: if the expression is malformed
(Mapping[ str, str]|Iterable[ tuple[ str, str]])|PluralRule
) -> Callable[ [ float|decimal.Decimal], str]
:
(source)
¶
Convert a list/dict of rules or a `PluralRule` object into a regular Python function. This is useful in situations where you need a real function and don't are about the actual rule object: >>> func = to_python({'one': 'n is 1', 'few': 'n in 2..4'}) >>> func(1) 'one' >>> func(3) 'few' >>> func = to_python({'one': 'n in 1,11', 'few': 'n in 3..10,13..19'}) >>> func(11) 'one' >>> func(15) 'few' :param rule: the rules as list or dict, or a `PluralRule` object :raise RuleError: if the expression is malformed
float|decimal.Decimal
, range_list: Iterable[ Iterable[ float|decimal.Decimal]]
) -> bool
:
(source)
¶
Float range test. This is the callback for the "within" operator of the UTS #35 pluralization rule language: >>> within_range_list(1, [(1, 3)]) True >>> within_range_list(1.0, [(1, 3)]) True >>> within_range_list(1.2, [(1, 4)]) True >>> within_range_list(8.8, [(1, 4), (7, 15)]) True >>> within_range_list(10, [(1, 4)]) False >>> within_range_list(10.5, [(1, 4), (20, 30)]) False
Undocumented
Value |
|