mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
minor optimization to convert_to_int(). No longer runs through convert_to_float(), but uses standard float() function.
This commit is contained in:
11
jc/utils.py
11
jc/utils.py
@ -85,7 +85,7 @@ def has_data(data):
|
|||||||
|
|
||||||
def convert_to_int(value):
|
def convert_to_int(value):
|
||||||
"""
|
"""
|
||||||
Converts string input to integer by stripping all non-numeric characters
|
Converts string and float input to int. Strips all non-numeric characters from strings.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
|
|
||||||
@ -96,11 +96,12 @@ def convert_to_int(value):
|
|||||||
integer/None Integer if successful conversion, otherwise None
|
integer/None Integer if successful conversion, otherwise None
|
||||||
"""
|
"""
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
|
str_val = re.sub(r'[^0-9\-\.]', '', value)
|
||||||
try:
|
try:
|
||||||
return int(re.sub(r'[^0-9\-\.]', '', value))
|
return int(str_val)
|
||||||
except ValueError:
|
except (ValueError, TypeError):
|
||||||
try:
|
try:
|
||||||
return int(convert_to_float(value))
|
return int(float(str_val))
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -113,7 +114,7 @@ def convert_to_int(value):
|
|||||||
|
|
||||||
def convert_to_float(value):
|
def convert_to_float(value):
|
||||||
"""
|
"""
|
||||||
Converts string input to float by stripping all non-numeric characters
|
Converts string and int input to float. Strips all non-numeric characters from strings.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user