MCPcopy
hub / github.com/probberechts/soccerdata / parse

Method parse

soccerdata/_common.py:99–193  ·  view source on GitHub ↗

Convert a string or int to a standard season format.

(self, season: Union[str, int])

Source from the content-addressed store, hash-verified

97 return SeasonCode.MULTI_YEAR
98
99 def parse(self, season: Union[str, int]) -> str: # noqa: C901
100 """Convert a string or int to a standard season format."""
101 season = str(season)
102 patterns = [
103 (
104 re.compile(r"^[0-9]{4}$"), # 1994 | 9495
105 lambda s: process_four_digit_year(s),
106 ),
107 (
108 re.compile(r"^[0-9]{2}$"), # 94
109 lambda s: process_two_digit_year(s),
110 ),
111 (
112 re.compile(r"^[0-9]{4}-[0-9]{4}$"), # 1994-1995
113 lambda s: process_full_year_range(s),
114 ),
115 (
116 re.compile(r"^[0-9]{4}/[0-9]{4}$"), # 1994/1995
117 lambda s: process_full_year_range(s.replace("/", "-")),
118 ),
119 (
120 re.compile(r"^[0-9]{4}-[0-9]{2}$"), # 1994-95
121 lambda s: process_partial_year_range(s),
122 ),
123 (
124 re.compile(r"^[0-9]{2}-[0-9]{2}$"), # 94-95
125 lambda s: process_short_year_range(s),
126 ),
127 (
128 re.compile(r"^[0-9]{2}/[0-9]{2}$"), # 94/95
129 lambda s: process_short_year_range(s.replace("/", "-")),
130 ),
131 ]
132
133 current_year = datetime.now(tz=timezone.utc).year
134
135 def process_four_digit_year(season: str) -> str:
136 """Process a 4-digit string like '1994' or '9495'."""
137 if self == SeasonCode.MULTI_YEAR:
138 if int(season[2:]) == int(season[:2]) + 1:
139 if season == "2021":
140 msg = (
141 f'Season id "{season}" is ambiguous: '
142 f'interpreting as "{season[:2]}-{season[-2:]}"'
143 )
144 warnings.warn(msg, stacklevel=1)
145 return season
146 if season[2:] == "99":
147 return "9900"
148 return season[-2:] + f"{int(season[-2:]) + 1:02d}"
149 if season == "1920":
150 return "1919"
151 if season == "2021":
152 return "2020"
153 if season[:2] == "19" or season[:2] == "20":
154 return season
155 if int(season) <= current_year:
156 return "20" + season[:2]

Callers 15

read_versionsMethod · 0.80
read_teamsMethod · 0.80
read_playersMethod · 0.80
read_team_ratingsMethod · 0.80
read_player_ratingsMethod · 0.80
read_seasonsMethod · 0.80
read_season_stagesMethod · 0.80
_read_game_infoMethod · 0.80
read_missing_playersMethod · 0.80
read_eventsMethod · 0.80
read_seasonsMethod · 0.80
read_leaguesMethod · 0.80

Calls

no outgoing calls

Tested by 8

test_season_pattern1aFunction · 0.64
test_season_pattern1bFunction · 0.64
test_season_pattern1cFunction · 0.64
test_season_pattern2Function · 0.64
test_season_pattern3Function · 0.64
test_season_pattern4Function · 0.64
test_season_pattern5Function · 0.64