def test_safe_parse(self): cases = ( ('1%', 0.01,), ('1%x', '1%x',), ('x1%x', 'x1%x',), ('1', 1,), ('1-', '1-',), ('x1y', 'x1y',), ('1K', 1024,), ('1Kqq', '1Kqq',), ('1.01', 1.01,), ('1..01', '1..01',), ) for _in, expected in cases: rst = humannum.parsenum(_in, safe=True) msg = 'parse: in: {_in} expect: {expected}, rst: {rst}'.format( _in=repr(_in), expected=repr(expected), rst=repr(rst), ) dd(msg) self.assertEqual(expected, rst) if not isinstance(expected, types.StringTypes): rst = humannum.parseint(_in, safe=True) self.assertEqual(int(expected), rst)
def test_parse_percentage(self): cases = ( ('0%', 0, ), ('-0%', 0, ), ('1%', 0.01, ), ('-1%', -0.01, ), ('1.00%', 0.01, ), ('1.1%', 0.011, ), ('100%', 1.0, ), ('-100%', -1.0, ), ('100.1%', 1.001, ), ('1200.123%', 12.00123, ), ('-1200.123%', -12.00123, ), ) for _in, expected in cases: rst = humannum.parsenum(_in) msg = 'parse: in: {_in} expect: {expected}, rst: {rst}'.format( _in=repr(_in), expected=repr(expected), rst=repr(rst), ) self.assertTrue(0.000000001 > expected - rst > -0.000000001, msg) self.assertEqual(int(expected), humannum.parseint(_in), 'int: ' + msg)
def _out(): print port, args.db for _repr, tbl_stat in rsts: if args.size is not None: op = args.size[0] num = humannum.parseint(args.size[1:]) if op == '>' and tbl_stat['Data_length'] < num: continue if op == '<' and tbl_stat['Data_length'] > num: continue print _repr
def test_parse_number(self): cases = ( ('0', 0, ''), ('1', 1, ''), ('1.00', 1.0, ''), ('1.01', 1.01, ''), ('1.00', 1, ''), ('1023', 1023, ''), ('1K', 1024, ''), ('1.00K', 1024, ''), ('1.01K', 1024 + 10.24, ''), ('1000.1K', 1024102.4, ''), ('1M', 1024**2, ''), ('1.01M', 1059061.76, ''), ('1G', 1024**3, ''), ('1T', 1024**4, ''), ('1P', 1024**5, ''), ('1E', 1024**6, ''), ('1Z', 1024**7, ''), ('1Y', 1024**8, ''), ('1024Y', 1024**9, ''), ('1048576Y', 1024**10, ''), ('1.00M', 1048576, ''), ) suffixes = ( '', 'b', 'i', 'ib', 'B', 'I', 'iB', ) for _in, _out, _msg in cases: rst = humannum.parsenum(_in) msg = 'parse: in: {_in} expect: {_out}, rst: {rst}; {_msg}'.format( _in=repr(_in), _out=repr(_out), rst=rst, _msg=_msg) self.assertEqual(_out, rst, msg) self.assertEqual(0 - _out, humannum.parsenum('-' + _in), msg + ': negative') for suff in suffixes: dd('parseint:', _in, ' suffix: ', suff) self.assertEqual(int(_out), humannum.parseint(_in + suff), msg + '; parseint with suffix: ' + repr(suff))