Skip to content

Commit

Permalink
FIX: incorrect regex for matching float string
Browse files Browse the repository at this point in the history
    * add tests for `save_int` and `save_float` in `fastf1.ergast.structure`
  • Loading branch information
harningle committed Sep 6, 2023
1 parent 17759e6 commit 2b8df2c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions fastf1/ergast/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def save_int(i_str) -> int:
"""
# Match pure integer strings, e.g.
# - '1234' -> 1234
if re.match(r'^-?\d+$', i_str):
if re.match(r'^[+-]?\d+$', i_str):
return int(i_str)

# Otherwise, return -1. A notable example is #432: 1954 British GP, where
Expand All @@ -115,7 +115,7 @@ def save_float(f_str) -> float:
"""
# Match pure float strings, e.g.
# - '1234.5678' -> 1234.5678
if re.match(r'^-?\d*\.?\d*$', f_str):
if re.match(r'^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$', f_str):
return float(f_str)

# Otherwise, return np.nan
Expand Down
29 changes: 28 additions & 1 deletion fastf1/tests/test_ergast.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from fastf1.ergast.structure import \
date_from_ergast, \
time_from_ergast, \
timedelta_from_ergast
timedelta_from_ergast, \
save_int, \
save_float


# ############### test structure.py #################################
Expand Down Expand Up @@ -176,6 +178,31 @@ def test_timedelta_from_ergast_error(time_string, caplog):
assert "Failed to parse" in caplog.text


@pytest.mark.parametrize(
"int_string, expected",
(
("123", 123),
("-4", -4),
("", -1),
("123.0", -1)
)
)
def test_save_int(int_string, expected):
assert save_int(int_string) == expected


@pytest.mark.parametrize(
"float_string, expected",
(
("123.4", 123.4),
("-5.6", -5.6),
("", float('nan'))
)
)
def test_save_float(float_string, expected):
assert save_float(float_string) == pytest.approx(expected, nan_ok=True)


@pytest.mark.parametrize(
"data, cast, rename, expected",
(
Expand Down

0 comments on commit 2b8df2c

Please sign in to comment.