-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
76 lines (57 loc) · 2.62 KB
/
stats.py
File metadata and controls
76 lines (57 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import logging
from database import create_connection
logger = logging.getLogger(__name__)
# ── constants ─────────────────────────────────────────────────────────────────
FORM_RESULTS_LIMIT = 5
# ── public ────────────────────────────────────────────────────────────────────
def get_team_history(team_name: str, competition_id: int) -> list:
# returns all historical standings for a team in a competition, ordered by date
with create_connection() as conn:
cursor = conn.cursor()
cursor.execute(
"""
SELECT s.position, s.points, s.scraped_date
FROM standings s
JOIN teams t ON s.team_id = t.team_id
WHERE t.team_name = ?
AND s.competition_id = ?
ORDER BY s.scraped_date ASC
""",
(team_name, competition_id),
)
rows = cursor.fetchall()
return [{"position": row[0], "points": row[1], "date": row[2]} for row in rows]
def get_team_form(team_name: str, results: list) -> list:
# returns last 5 results for a team as a list of W/L/D strings
form = []
for match in results:
result = _get_result_for_team(team_name, match)
if result:
form.append(result)
return form[-FORM_RESULTS_LIMIT:]
def build_form_data(standings: list, results: list) -> dict:
# builds a dict mapping each team name to their last 5 results
return {
row["team_name"]: get_team_form(row["team_name"], results) for row in standings
}
# ── helpers ───────────────────────────────────────────────────────────────────
def _get_result_for_team(team_name: str, match: dict) -> str | None:
# returns W, L, or D for a team in a single match — None if scores are invalid
try:
home_score = int(match["home_score"])
away_score = int(match["away_score"])
except ValueError:
return None
if match["home"] == team_name:
if home_score > away_score:
return "W"
if home_score < away_score:
return "L"
return "D"
if match["away"] == team_name:
if away_score > home_score:
return "W"
if away_score < home_score:
return "L"
return "D"
return None