-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkenpom.py
More file actions
171 lines (130 loc) · 3.89 KB
/
kenpom.py
File metadata and controls
171 lines (130 loc) · 3.89 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
Extract team data from kenpom.com.
The kenpom module parses the html of kenpom.com, and builds Team objects
to store team information and statistics.
Usage is quite simple.
>>> import kenpom
>>> kp = kenpom.get()
>>> uk = kp['Kentucky']
>>> if uk.rank != 1:
print('Not that good this year!')
The returned object from get() is a dictionary where the keys are team
names and the values are instances of the kenpom.Team class.
For more information on the meaning of particular team statistics, and
how they are calculated, refer to kenpom.com.
"""
from dataclasses import dataclass
from urllib.request import urlopen
from html.parser import HTMLParser
@dataclass
class Team:
"""
Store team information and statistics.
Most attributes have long and short names, for example:
team.defense == team.d
Available attributes:
rank
name
conference
wins, w
losses, l
efficiency, e
offense, o
defense, d
tempo, t
luck
opponent_efficiency, o_e
opponent_offense, o_o
opponent_defense, o_d
nonconference_opponent_efficiency, nc_o_e
record
For more information on what these values mean, refer to kenpom.com.
"""
rank: int
name: str
conference: str
wins: int
losses: int
efficiency: float
offense: float
defense: float
tempo: float
luck: float
opponent_efficiency: float
opponent_offense: float
opponent_defense: float
nonconference_opponent_efficiency: float
@property
def d(self):
return self.defense
@property
def e(self):
return self.efficiency
@property
def l(self):
return self.losses
@property
def nc_o_e(self):
return self.nonconference_opponent_efficiency
@property
def o(self):
return self.offense
@property
def o_d(self):
return self.opponent_defense
@property
def o_e(self):
return self.opponent_efficiency
@property
def o_o(self):
return self.opponent_offense
@property
def record(self):
return '-'.join((self.wins, self.losses))
@property
def t(self):
return self.tempo
@property
def w(self):
return self.wins
class KenPomParser(HTMLParser):
"""Parse the HTML of kenpom.com, extracting team data."""
def __init__(self):
super(KenPomParser, self).__init__()
self.row = None
self.rows = []
def handle_starttag(self, tag, attrs):
"""Look for the start of a table row."""
if tag == 'tr':
self.row = []
def handle_endtag(self, tag):
"""Look for the end of a table row."""
if tag == 'tr':
if len(self.row) == 22:
self.rows.append(self.row[1:])
self.row = None
def handle_data(self, data):
"""Include team data from the table."""
if type(self.row) == list:
self.row.append(data)
def get():
"""Parse kenpom.com and return a team data lookup (dictionary)."""
with urlopen('https://www.kenpom.com') as kp:
kp_html = kp.read().decode('utf-8')
parser = KenPomParser()
parser.feed(kp_html)
return _create_teams(parser.rows)
def _create_teams(team_data):
"""Build a team lookup from kenpom table data."""
teams = {}
for (rank, name, conf, rec, e, o, _, d, _, t, _, luck, _,
o_e, _, o_o, _, o_d, _, nc_o_e, _) in team_data:
wins, losses = rec.split('-')
rank, wins, losses = map(int, (rank, wins, losses))
e, o, d, t, luck = map(float, (e, o, d, t, luck))
o_e, o_o, o_d, nc_o_e = map(float, (o_e, o_o, o_d, nc_o_e))
teams[name] = Team(rank, name, conf, wins, losses,
e, o, d, t, luck, o_e, o_o, o_d, nc_o_e)
return teams
if __name__ == '__main__':
pass