mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2026-08-22 11:58:49 +03:00
[utils] str_to_int: support optional decimal-dot parsing
This commit is contained in:
+10
-4
@@ -2010,13 +2010,19 @@ def str_or_none(v, default=None):
|
||||
return default if v is None else str(v)
|
||||
|
||||
|
||||
def str_to_int(int_str):
|
||||
def str_to_int(int_str, *, dot_decimal=False):
|
||||
""" A more relaxed version of int_or_none """
|
||||
if isinstance(int_str, int):
|
||||
return int_str
|
||||
elif isinstance(int_str, str):
|
||||
int_str = re.sub(r'[,\.\+]', '', int_str)
|
||||
return int_or_none(int_str)
|
||||
if not isinstance(int_str, str):
|
||||
return None
|
||||
int_str = re.sub(r'[,+]', '', int_str)
|
||||
|
||||
if dot_decimal:
|
||||
f = float_or_none(int_str)
|
||||
return int(f) if f is not None else None
|
||||
else:
|
||||
return int_or_none(int_str.replace('.', ''))
|
||||
|
||||
|
||||
@partial_application
|
||||
|
||||
Reference in New Issue
Block a user