You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@goodmami The code for the function is shown below.
def _default_cast(x):
if isinstance(x, basestring):
if x.startswith('"'):
x = x # strip quotes?
elif re.match(
r'-?(0|[1-9]\d*)(\.\d+[eE][-+]?|\.|[eE][-+]?)\d+', x):
x = float(x)
elif re.match(r'-?\d+', x):
x = int(x)
return x
Here are some examples that cause unexpected behavior.
_default_cast("123a") # expected: "123a", actual: ValueError from int()
_default_cast("-3.14z") # expected: "-3.14z", actual: ValueError from float()
The problem arises from using regex patterns without ^ and $. Adding these characters as shown below fixes the problems.
def _default_cast(x):
if isinstance(x, basestring):
if x.startswith('"'):
x = x # strip quotes?
elif re.match(
r'^-?(0|[1-9]\d*)(\.\d+[eE][-+]?|\.|[eE][-+]?)\d+$', x):
x = float(x)
elif re.match(r'^-?\d+$', x):
x = int(x)
return x
Another alternative would be not to use regex at all here, and use try-except blocks instead, as shown below.
def _default_cast(x):
if isinstance(x, basestring):
try:
x = int(x)
except ValueError:
try:
x = float(x)
except ValueError:
pass
return x
The text was updated successfully, but these errors were encountered:
Since I'm using re.match() and not re.search(), the initial ^ anchor is unnecessary, but you're right that the final $ is important.
I added some unit tests to model the issue, then tried both of your solutions. They're both equally capable, but when I benchmarked them the latter solution is a bit slower (except when the value is an integer), so I think I'll go with the first one.
@goodmami The code for the function is shown below.
Here are some examples that cause unexpected behavior.
The problem arises from using regex patterns without
^
and$
. Adding these characters as shown below fixes the problems.Another alternative would be not to use regex at all here, and use try-except blocks instead, as shown below.
The text was updated successfully, but these errors were encountered: