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))
예제 #2
0
    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)
예제 #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)
예제 #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
예제 #5
0
 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)
예제 #7
0
 def test_empty_base(self):
     self.assertEqual(tu.apply_units('17', {}), 17)
예제 #8
0
 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)
예제 #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)
예제 #18
0
파일: optik_ext.py 프로젝트: falcom520/vim
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'] )
예제 #20
0
파일: optik_ext.py 프로젝트: falcom520/vim
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'])
예제 #24
0
def check_bytes(option, opt, value):
    if hasattr(value, '__int__'):
        return value
    return apply_units(value, BYTE_UNITS)
예제 #25
0
 def test_blank_mixed(self):
     result = tu.apply_units('45, 317, 337', {}, final=int)
     self.assertEqual(result, 45317337)
예제 #26
0
def check_time(option, opt, value):
    if isinstance(value, integer_types + (float,)):
        return value
    return apply_units(value, TIME_UNITS)
예제 #27
0
 def test_unit_singleunit_multipleletter(self):
     result = tu.apply_units('47KB', self.units)
     self.assertEqual(result, 47 * self.units['kb'])
예제 #28
0
 def test_blank_base(self):
     result = tu.apply_units(' 42  ', {}, final=int)
     self.assertEqual(result, 42)
예제 #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'])
예제 #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)
예제 #32
0
 def test_unit_singleunit_singleletter(self):
     result = tu.apply_units('15m', self.units)
     self.assertEqual(result, 15 * self.units['m'])
예제 #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)
예제 #34
0
 def test_unit_singleunit_caseinsensitive(self):
     result = tu.apply_units('47kb', self.units)
     self.assertEqual(result, 47 * self.units['kb'])
예제 #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)
예제 #36
0
 def test_unit_with_blank(self):
     result = tu.apply_units('1 000 KB', self.units)
     self.assertEqual(result, 1000 * self.units['kb'])
예제 #37
0
def check_time(option, opt, value):
    if isinstance(value, integer_types + (float, )):
        return value
    return apply_units(value, TIME_UNITS)