:copyright: (c) 2013 by the Babel Team.
:license: BSD, see LICENSE for more details.
"""
-import decimal
import re
import sys
+from babel._compat import Decimal
+
_plural_tags = ('zero', 'one', 'two', 'few', 'many', 'other')
_fallback_tag = 'other'
# 2.6's Decimal cannot convert from float directly
if sys.version_info < (2, 7):
n = str(n)
- n = decimal.Decimal(n)
+ n = Decimal(n)
- if isinstance(n, decimal.Decimal):
+ if isinstance(n, Decimal):
dec_tuple = n.as_tuple()
exp = dec_tuple.exponent
fraction_digits = dec_tuple.digits[exp:] if exp < 0 else ()
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://babel.edgewall.org/log/.
-import decimal
import unittest
import pytest
+
from babel import plural
+from babel._compat import Decimal
def test_plural_rule():
def test_plural_rule_operands_v():
rule = plural.PluralRule({'one': 'v is 2'})
- assert rule(decimal.Decimal('1.20')) == 'one'
- assert rule(decimal.Decimal('1.2')) == 'other'
+ assert rule(Decimal('1.20')) == 'one'
+ assert rule(Decimal('1.2')) == 'other'
assert rule(2) == 'other'
def test_plural_rule_operands_w():
rule = plural.PluralRule({'one': 'w is 2'})
- assert rule(decimal.Decimal('1.23')) == 'one'
- assert rule(decimal.Decimal('1.20')) == 'other'
+ assert rule(Decimal('1.23')) == 'one'
+ assert rule(Decimal('1.20')) == 'other'
assert rule(1.2) == 'other'
def test_plural_rule_operands_f():
rule = plural.PluralRule({'one': 'f is 20'})
- assert rule(decimal.Decimal('1.23')) == 'other'
- assert rule(decimal.Decimal('1.20')) == 'one'
+ assert rule(Decimal('1.23')) == 'other'
+ assert rule(Decimal('1.20')) == 'one'
assert rule(1.2) == 'other'
def test_plural_rule_operands_t():
rule = plural.PluralRule({'one': 't = 5'})
- assert rule(decimal.Decimal('1.53')) == 'other'
- assert rule(decimal.Decimal('1.50')) == 'one'
+ assert rule(Decimal('1.53')) == 'other'
+ assert rule(Decimal('1.50')) == 'one'
assert rule(1.5) == 'one'
@pytest.mark.parametrize('source,n,i,v,w,f,t', EXTRACT_OPERANDS_TESTS)
def test_extract_operands(source, n, i, v, w, f, t):
- source = decimal.Decimal(source) if isinstance(source, str) else source
+ source = Decimal(source) if isinstance(source, str) else source
assert (plural.extract_operands(source) ==
- decimal.Decimal(n), i, v, w, f, t)
+ Decimal(n), i, v, w, f, t)