Skip to content

Commit

Permalink
feat: clean '0' times from participant laps
Browse files Browse the repository at this point in the history
  • Loading branch information
iagocanalejas committed Nov 14, 2024
1 parent 146ae54 commit 776aece
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
6 changes: 5 additions & 1 deletion rscraping/data/normalization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
split_by_edition_parts as split_by_edition_parts,
find_race_sponsor as find_race_sponsor,
)
from .times import normalize_lap_time as normalize_lap_time, normalize_spanish_months as normalize_spanish_months
from .times import (
normalize_lap_time as normalize_lap_time,
normalize_spanish_months as normalize_spanish_months,
time_or_none as time_or_none,
)
from .towns import (
normalize_town as normalize_town,
amend_town as amend_town,
Expand Down
14 changes: 10 additions & 4 deletions rscraping/data/normalization/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@ def normalize_lap_time(value: str) -> time | None:
parts[1] = parts[1][:-1]
if len(parts[0]) == 4:
# try to fix '2102:48'
return datetime.strptime(f"{parts[0][0:2]}:{parts[0][2:]},{parts[1]}", "%M:%S,%f").time()
return time_or_none(datetime.strptime(f"{parts[0][0:2]}:{parts[0][2:]},{parts[1]}", "%M:%S,%f").time())
if len(parts[1]) == 4:
# try to fix '25:2257'
return datetime.strptime(f"{parts[0]}:{parts[1][0:2]},{parts[1][2:]}", "%M:%S,%f").time()
return datetime.strptime(f"{parts[0]}:{parts[1]}", "%M:%S").time()
return time_or_none(datetime.strptime(f"{parts[0]}:{parts[1][0:2]},{parts[1][2:]}", "%M:%S,%f").time())
return time_or_none(datetime.strptime(f"{parts[0]}:{parts[1]}", "%M:%S").time())
if len(parts) == 3:
return datetime.strptime(f"{parts[0]}:{parts[1]},{parts[2]}", "%M:%S,%f").time()
return time_or_none(datetime.strptime(f"{parts[0]}:{parts[1]},{parts[2]}", "%M:%S,%f").time())
return None


def time_or_none(value: time | None) -> time | None:
if value is None or value == time(0, 0, 0):
return None
return value


MONTHS = {
"ENERO": ["XANEIRO"],
"FEBRERO": ["FEBREIRO"],
Expand Down
6 changes: 4 additions & 2 deletions tests/normalization/time_normalization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ def test_lap_time_normalization(self):
"21:02.48",
"25:22.57",
"28:24.00",
"00:00.00",
None,
"21:13.66",
]

for idx, lap_time in enumerate(self.TIMES):
self.assertEqual(normalize_lap_time(lap_time), datetime.strptime(results[idx], "%M:%S.%f").time())
result = results[idx]
result = datetime.strptime(result, "%M:%S.%f").time() if result else None
self.assertEqual(normalize_lap_time(lap_time), result)

0 comments on commit 776aece

Please sign in to comment.