[utils] Improve `parse_count`

This commit is contained in:
pukkandan 2021-12-24 03:02:50 +05:30
parent d43de6821c
commit 352d5da812
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698
2 changed files with 17 additions and 2 deletions

View File

@ -1156,9 +1156,16 @@ class TestUtil(unittest.TestCase):
self.assertEqual(parse_count('1000'), 1000) self.assertEqual(parse_count('1000'), 1000)
self.assertEqual(parse_count('1.000'), 1000) self.assertEqual(parse_count('1.000'), 1000)
self.assertEqual(parse_count('1.1k'), 1100) self.assertEqual(parse_count('1.1k'), 1100)
self.assertEqual(parse_count('1.1 k'), 1100)
self.assertEqual(parse_count('1,1 k'), 1100)
self.assertEqual(parse_count('1.1kk'), 1100000) self.assertEqual(parse_count('1.1kk'), 1100000)
self.assertEqual(parse_count('1.1kk '), 1100000) self.assertEqual(parse_count('1.1kk '), 1100000)
self.assertEqual(parse_count('1,1kk'), 1100000)
self.assertEqual(parse_count('100 views'), 100)
self.assertEqual(parse_count('1,100 views'), 1100)
self.assertEqual(parse_count('1.1kk views'), 1100000) self.assertEqual(parse_count('1.1kk views'), 1100000)
self.assertEqual(parse_count('10M views'), 10000000)
self.assertEqual(parse_count('has 10M views'), 10000000)
def test_parse_resolution(self): def test_parse_resolution(self):
self.assertEqual(parse_resolution(None), {}) self.assertEqual(parse_resolution(None), {})

View File

@ -2211,7 +2211,7 @@ def parse_count(s):
if s is None: if s is None:
return None return None
s = s.strip() s = re.sub(r'^[^\d]+\s', '', s).strip()
if re.match(r'^[\d,.]+$', s): if re.match(r'^[\d,.]+$', s):
return str_to_int(s) return str_to_int(s)
@ -2223,9 +2223,17 @@ def parse_count(s):
'M': 1000 ** 2, 'M': 1000 ** 2,
'kk': 1000 ** 2, 'kk': 1000 ** 2,
'KK': 1000 ** 2, 'KK': 1000 ** 2,
'b': 1000 ** 3,
'B': 1000 ** 3,
} }
return lookup_unit_table(_UNIT_TABLE, s) ret = lookup_unit_table(_UNIT_TABLE, s)
if ret is not None:
return ret
mobj = re.match(r'([\d,.]+)(?:$|\s)', s)
if mobj:
return str_to_int(mobj.group(1))
def parse_resolution(s): def parse_resolution(s):