Skip to content

Instantly share code, notes, and snippets.

@biesnecker
Created October 16, 2014 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biesnecker/68b9d9d87068e11ce24f to your computer and use it in GitHub Desktop.
Save biesnecker/68b9d9d87068e11ce24f to your computer and use it in GitHub Desktop.
import csv
import json
from collections import defaultdict
datafiles = ['./GL{0}.TXT'.format(x) for x in range(1960,2014)]
counts = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
max_i = 0
def process_linescore(s):
results = []
while s:
s0 = s[0]
if s0 == "(":
results.append(int(s[1:3]))
s = s[4:]
else:
if s0 == 'x':
results.append(0)
else:
results.append(int(s0))
s = s[1:]
return results
for df in datafiles:
with open(df, 'rb') as datafile:
reader = csv.reader(datafile, delimiter=',')
for line in reader:
visitor_score = int(line[9])
home_score = int(line[10])
if visitor_score == home_score:
result = 't'
elif visitor_score > home_score:
result = 'v'
else:
result = 'h'
visitor_linescore = process_linescore(line[19])
home_linescore = process_linescore(line[20])
if len(visitor_linescore) != len(home_linescore):
continue # sanity check
score_diff = 0
for idx in range(0, len(visitor_linescore)):
for bottom in (False, True):
if bottom:
score_diff += home_linescore[idx]
else:
score_diff -= visitor_linescore[idx]
effective_diff = max(min(score_diff, 10), -10) + 10
inning_num = idx * 2 + int(bottom)
counts[inning_num][effective_diff][result] += 1
print(json.dumps(counts))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment