-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
225 lines (170 loc) · 6.4 KB
/
main.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from __future__ import print_function
import re
import csv
import datetime
import sys
import json
import os
from collections import defaultdict
from StringIO import StringIO
from os import listdir
from os.path import isfile, join
class GameState(object):
def __init__(self):
# empty 2-by-9 array
self.players = []
for i in range(2):
self.players.append([None] * 12)
self.game_id = None
self.date = None
self.k_graph = defaultdict(set)
self.strikeouts = defaultdict(list)
self.names = {}
self.teams = {}
def load_teams(self, path):
f = open(path)
reader = csv.reader(f, delimiter=',')
for l in reader:
self.teams[l[0]] = l[2] + ' ' + l[3]
def parse_line(self, line):
if line[0] == 'com':
pass
elif line[0] == 'id':
self.game_id = line[1]
elif line[0] == 'info':
if line[1] == 'date':
self.date = datetime.datetime.strptime(line[2], '%Y/%m/%d')
if line[1] == 'visteam':
self.visteam = line[2]
if line[1] == 'hometeam':
self.hometeam = line[2]
elif line[0] == 'version':
pass
elif line[0] == 'start':
player = line[1]
team = int(line[3])
pos = int(line[-1]) - 1
self.players[team][pos] = player
self.names[player] = line[2]
elif line[0] == 'play':
# is this a strikeout?
if line[-1][0] == 'K':
team = int(line[2])
batter = line[3]
pitcher = self.players[1 - team][0]
self.k_graph[pitcher].add(batter)
# `team` is the batter's team, so team == 0 means the away team
# struck out
if team == 0:
self.strikeouts[(pitcher, batter)].append((self.hometeam,
self.visteam,
self.date))
else:
self.strikeouts[(pitcher, batter)].append((self.visteam,
self.hometeam,
self.date))
elif line[0] == 'sub':
player = line[1]
team = int(line[3])
pos = int(line[-1]) - 1
self.players[team][pos] = player
self.names[player] = line[2]
elif line[0] == 'data':
pass
def run(self, path):
f = open(path)
reader = csv.reader(f, delimiter=',')
for l in reader:
self.parse_line(l)
def do_search(self, start, end):
starts = []
ends = []
if start not in self.names:
for k, v in self.names.items():
if start.lower() == v.lower():
starts.append(k)
if end not in self.names:
for k, v in self.names.items():
if end.lower() == v.lower():
ends.append(k)
for s in starts:
for e in ends:
res = self.search(s, e)
if res is not None:
return res
return None
def search(self, start, end):
q = [[p] for p in self.k_graph[start]]
seen = set()
while q:
path = q.pop(0)
if path[-1] == end:
return [start] + path
for p in self.k_graph[path[-1]]:
if p not in seen:
seen.add(p)
q.append(path + [p])
print("No path")
def graph(self):
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
ks = []
for p, batters in self.k_graph.items():
for k in batters:
ks.append((p, k))
df = pd.DataFrame(columns=['source', 'target'], data=ks)
return nx.from_pandas_edgelist(df)
nx.draw(G, with_labels=False)
plt.show()
def to_json(self):
json.dump({k: list(v) for k, v in self.k_graph.items()}, open('k_graph.json', 'w'))
json.dump({'.'.join(k): [[s[0], s[1], s[2].strftime('%m/%d/%Y')] for s in v] for k, v in self.strikeouts.items()}, open('strikeouts.json', 'w'))
json.dump(self.names, open('names.json', 'w'))
def parse(path):
oldpath = os.getcwd()
os.chdir(path)
gs = GameState()
files = [f for f in listdir('./') if isfile(f)
and re.match('^\d{4}\w+\.EV\w$', f)]
for f in files:
gs.run(f)
gs.load_teams('./TEAMABR.TXT')
print('Finished parsing %d edges' % len(gs.k_graph), file=sys.stderr)
os.chdir(oldpath)
return gs
def load(path):
gs = GameState()
gs.k_graph = json.load(open(join(path, 'k_graph.json')))
gs.strikeouts = json.load(open(join(path, 'strikeouts.json')))
gs.names = json.load(open(join(path, 'names.json')))
gs.load_teams(join(path, 'TEAMABR.TXT'))
print('Finished loading %d edges' % len(gs.k_graph), file=sys.stderr)
return gs
from flask import Flask, request, render_template
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
@app.route('/', methods=['GET'])
def search():
start = request.args.get('start', 'Adam Ottavino')
end = request.args.get('end', 'Babe Ruth')
path = gs.do_search(start, end)
if path:
text = '<h3>%s could strike out %s.</h3>\n' % (gs.names[path[0]],
gs.names[path[-1]])
for i in range(len(path) - 1):
p = path[i]
b = path[i+1]
text += "<p>%s struck out %s:\n<ul>\n" % (gs.names[p], gs.names[b])
for k in gs.strikeouts[(p, b)]:
text += "<li>pitching for the %s against the %s on %s</li>\n" % (
gs.teams[k[0]], gs.teams[k[1]], k[2].strftime('%m/%d/%Y'))
text += "</ul></p>\n"
else:
text = '<h3>%s couldn\'t strike out %s.</h3>' % (start, end)
return render_template('index.html', start=start, end=end, text=text)
if __name__ == '__main__':
gs = parse('./data')
app.run(host="0.0.0.0", port=443,
ssl_context=('/etc/letsencrypt/live/transitivestrikeouts.com/fullchain.pem',
'/etc/letsencrypt/live/transitivestrikeouts.com/privkey.pem'))