-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_meta.py
95 lines (88 loc) · 3.58 KB
/
result_meta.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import re
from datetime import datetime, timedelta
from utils import *
def add_result_meta(rows, results, result_file):
def _get_short_name(division):
m = re.search("[0-9]", division)
if m != None:
nb = m.group(0)
m = re.search("Junior|Senior", division)
if m != None:
div = m.group(0)
m = re.search("South|North|West", division)
if m != None:
zone = m.group(0)
try:
return f"{div}-{nb}-{zone}"
except:
return f"{div}-{nb}"
def _get_all_matches(team_name, results, type="Home"):
def conv_date(str):
d = datetime.strptime(str, "%Y/%m/%d")
return d
matches = []
for a_result in results:
if type == "Home" and a_result["Home"] == team_name:
matches.append(a_result)
if type == "Away" and a_result["Away"] == team_name:
matches.append(a_result)
matches = sorted(matches, key=lambda d: conv_date(d["Date"]))
return matches
def _get_gap(home_team, away_team, matches):
matched = []
for match in matches:
if (match["Home"] == home_team and match["Away"] == away_team) or (
match["Away"] == home_team and match["Home"] == away_team
):
matched.append(match)
# print (matched, home_team, away_team)
assert 2 == len(matched), print (matched, home_team, away_team)
# , print (matched)
match_date = datetime.strptime(matched[0]["Date"], "%Y/%m/%d")
prev_match_date = datetime.strptime(matched[1]["Date"], "%Y/%m/%d")
difference = abs((match_date - prev_match_date).days)
return int(difference / 7)
def _get_max_consecutive(team, results, type="Home"):
# matches are ordered by date
matches = _get_all_matches(team, results, type)
max_consecutive = 0
prev_match = None
consecutive = 1
for match in matches:
if prev_match == None:
consecutive = 1
else:
match_date = datetime.strptime(match["Date"], "%Y/%m/%d")
prev_match_date = datetime.strptime(prev_match["Date"], "%Y/%m/%d")
assert match_date > prev_match_date
difference = (match_date - prev_match_date).days
if difference == 7:
consecutive += 1
else:
consecutive = 1
if consecutive > max_consecutive:
max_consecutive = consecutive
prev_match = match
return max_consecutive
div_dict = {}
for division in get_all_divisions(rows):
print (division)
result_metas = []
for home_team in get_all_teams(rows, division):
# row = get_row_for_team(rows, home_team)
meta = {}
meta["home_team"] = home_team
meta["home_cons"] = _get_max_consecutive(home_team, results, "Home")
meta["away_cons"] = _get_max_consecutive(home_team, results, "Away")
for away_team in get_all_teams(rows, division):
if home_team == away_team:
meta[home_team] = "-"
continue
meta[away_team] = _get_gap(home_team, away_team, results)
result_metas.append(meta)
sorted_metas = []
div = _get_short_name(division)
for meta in result_metas:
sorted_metas.append(dict(sorted(meta.items())))
div_dict[div] = sorted_metas
update_excel(result_file, sheets_dict=div_dict)