Skip to content

Commit

Permalink
Refactor year to use regex (lunaflight#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
N00bcak authored Sep 5, 2024
1 parent 82253c3 commit 0984b0b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/history/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
import re
import sqlite3
from datetime import datetime
from pathlib import Path
Expand All @@ -12,7 +13,7 @@

def _clean_year(year: Union[str, int]) -> str:
"""
Clean the string to YYYY.
Year strings are either "YYYY[ /-]YYYY" or "YYYY".
Clean the year string by standardising all input to YYYY, e.g. 2223.
Expand All @@ -25,9 +26,11 @@ def _clean_year(year: Union[str, int]) -> str:
str: The cleaned year string.
"""
year = str(year).strip().replace("/", "").replace(" ", "").replace("-", "")
# Turns 20222023 to 2223
if len(year) == 8:
year = year[2] + year[3] + year[6] + year[7]

# If the year string is YYYY[ /-]YYYY, standardise it.
if re.match(r"[0-9]{8}", year):
year = year[2:4] + year[6:]

return year


Expand Down

0 comments on commit 0984b0b

Please sign in to comment.