def test_unit_wrong_input(self):
     self.assertRaises(ValueError, tu.apply_units, '', self.units)
     self.assertRaises(ValueError, tu.apply_units, 'wrong input',
                       self.units)
     self.assertRaises(ValueError, tu.apply_units, 'wrong13 input',
                       self.units)
     self.assertRaises(ValueError, tu.apply_units, 'wrong input42',
                       self.units)
     with self.assertRaises(ValueError) as cm:
         tu.apply_units('42 cakes', self.units)
     self.assertIn('invalid unit cakes.', str(cm.exception))
    def test_empty_inter(self):
        def inter(value):
            return int(float(value)) * 2

        result = tu.apply_units('12.4', {}, inter=inter)
        self.assertEqual(result, 12 * 2)
        self.assertIsInstance(result, float)
Esempio n. 3
0
 def _ensure_correctly_typed(self, form, value):
     if isinstance(value, str):
         value = value.strip()
         if not value:
             return None
         try:
             value = apply_units(value, TIME_UNITS)
         except ValueError:
             raise ProcessFormError(
                 form._cw.
                 _('a number (in seconds) or 20s, 10min, 24h or 4d are expected'
                   ))
     return timedelta(0, value)
Esempio n. 4
0
def check_bytes(option: Optional["Option"], opt: str, value: Any) -> int:
    if hasattr(value, "__int__"):
        return value
    # mypy: Incompatible return value type (got "Union[float, int]", expected "int")
    # we force "int" using "final=int"
    return apply_units(value, BYTE_UNITS, final=int)  # type: ignore
 def test_blank_space(self):
     result = tu.apply_units(' 1 337 ', {}, final=int)
     self.assertEqual(result, 1337)
 def test_empty_inter(self):
     def inter(value):
         return int(float(value)) * 2
     result = tu.apply_units('12.4', {}, inter=inter)
     self.assertEqual(result, 12 * 2)
     self.assertIsInstance(result, float)
 def test_empty_base(self):
     self.assertEqual(tu.apply_units('17', {}), 17)
 def test_empty_inter_final(self):
     result = tu.apply_units('12.4', {}, inter=float, final=int)
     self.assertEqual(result, 12)
     self.assertIsInstance(result, int)
def check_time(option, opt, value):
    from logilab.common.textutils import TIME_UNITS, apply_units
    if isinstance(value, (int, long, float)):
        return value
    return apply_units(value, TIME_UNITS)
Esempio n. 10
0
def check_time(option, opt, value):
    if isinstance(value, (int, long, float)):
        return value
    return apply_units(value, TIME_UNITS)
 def test_unit_singleunit_singleletter(self):
     result = tu.apply_units('15m', self.units)
     self.assertEqual(result, 15 * self.units['m'] )
 def test_unit_singleunit_multipleletter(self):
     result = tu.apply_units('47KB', self.units)
     self.assertEqual(result, 47 * self.units['kb'] )
 def test_blank_coma(self):
     result = tu.apply_units(' 4,298.42 ', {})
     self.assertEqual(result, 4298.42)
 def test_blank_mixed(self):
     result = tu.apply_units('45, 317, 337', {}, final=int)
     self.assertEqual(result, 45317337)
 def test_blank_space(self):
     result = tu.apply_units(' 1 337 ', {}, final=int)
     self.assertEqual(result, 1337)
 def test_blank_base(self):
     result = tu.apply_units(' 42  ', {}, final=int)
     self.assertEqual(result, 42)
 def test_empty_inter_final(self):
     result = tu.apply_units('12.4', {}, inter=float, final=int)
     self.assertEqual(result, 12)
     self.assertIsInstance(result, int)
Esempio n. 18
0
def check_time(option, opt, value):
    if isinstance(value, (int, long, float)):
        return value
    return apply_units(value, TIME_UNITS)
 def test_unit_singleunit_caseinsensitive(self):
     result = tu.apply_units('47kb', self.units)
     self.assertEqual(result, 47 * self.units['kb'] )
Esempio n. 20
0
def check_bytes(option, opt, value):
    if hasattr(value, '__int__'):
        return value
    return apply_units(value, BYTE_UNITS)
 def test_unit_multipleunit(self):
     result = tu.apply_units('47KB 1.5MB', self.units)
     self.assertEqual(result, 47 * self.units['kb'] + 1.5 * self.units['mb'])
def check_bytes(option, opt, value):
    from logilab.common.textutils import BYTE_UNITS, apply_units
    if hasattr(value, '__int__'):
        return value
    return apply_units(value, BYTE_UNITS)
 def test_unit_with_blank(self):
     result = tu.apply_units('1 000 KB', self.units)
     self.assertEqual(result, 1000 * self.units['kb'])
Esempio n. 24
0
def check_bytes(option, opt, value):
    if hasattr(value, '__int__'):
        return value
    return apply_units(value, BYTE_UNITS)
Esempio n. 25
0
 def test_blank_mixed(self):
     result = tu.apply_units('45, 317, 337', {}, final=int)
     self.assertEqual(result, 45317337)
Esempio n. 26
0
def check_time(option, opt, value):
    if isinstance(value, integer_types + (float,)):
        return value
    return apply_units(value, TIME_UNITS)
Esempio n. 27
0
 def test_unit_singleunit_multipleletter(self):
     result = tu.apply_units('47KB', self.units)
     self.assertEqual(result, 47 * self.units['kb'])
Esempio n. 28
0
 def test_blank_base(self):
     result = tu.apply_units(' 42  ', {}, final=int)
     self.assertEqual(result, 42)
Esempio n. 29
0
 def test_unit_multipleunit(self):
     result = tu.apply_units('47KB 1.5MB', self.units)
     self.assertEqual(result,
                      47 * self.units['kb'] + 1.5 * self.units['mb'])
Esempio n. 30
0
 def test_blank_coma(self):
     result = tu.apply_units(' 4,298.42 ', {})
     self.assertEqual(result, 4298.42)
 def test_empty_base(self):
     self.assertEqual(tu.apply_units('17', {}), 17)
Esempio n. 32
0
 def test_unit_singleunit_singleletter(self):
     result = tu.apply_units('15m', self.units)
     self.assertEqual(result, 15 * self.units['m'])
Esempio n. 33
0
def check_time(option, opt, value):
    from logilab.common.textutils import TIME_UNITS, apply_units
    if isinstance(value, (int, long, float)):
        return value
    return apply_units(value, TIME_UNITS)
Esempio n. 34
0
 def test_unit_singleunit_caseinsensitive(self):
     result = tu.apply_units('47kb', self.units)
     self.assertEqual(result, 47 * self.units['kb'])
Esempio n. 35
0
def check_bytes(option, opt, value):
    from logilab.common.textutils import BYTE_UNITS, apply_units
    if hasattr(value, '__int__'):
        return value
    return apply_units(value, BYTE_UNITS)
Esempio n. 36
0
 def test_unit_with_blank(self):
     result = tu.apply_units('1 000 KB', self.units)
     self.assertEqual(result, 1000 * self.units['kb'])
Esempio n. 37
0
def check_time(option, opt, value):
    if isinstance(value, integer_types + (float, )):
        return value
    return apply_units(value, TIME_UNITS)