Last active
December 10, 2015 14:29
-
-
Save dnmiller/4448202 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from collections import namedtuple | |
from datetime import timedelta | |
team = lambda *x: dict(zip(('City', 'Mascot', 'Division'), x)) | |
teams = { | |
'BUF': team('Buffalo', 'Bills', 'AFC East'), | |
'MIA': team('Miami', 'Dolphins', 'AFC East'), | |
'NE': team('New England', 'Patriots', 'AFC East'), | |
'NYJ': team('New York', 'Jets', 'AFC East'), | |
'BAL': team('Baltimore', 'Ravens', 'AFC North'), | |
'CIN': team('Cincinnati', 'Bengals', 'AFC North'), | |
'CLE': team('Cleveland', 'Browns', 'AFC North'), | |
'PIT': team('Pittsburgh', 'Steelers', 'AFC North'), | |
'HOU': team('Houston', 'Texans', 'AFC South'), | |
'IND': team('Indianapolis', 'Colts', 'AFC South'), | |
'JAC': team('Jacksonville', 'Jaguars', 'AFC South'), | |
'TEN': team('Tennessee', 'Titans', 'AFC South'), | |
'DEN': team('Denver', 'Broncos', 'AFC West'), | |
'KC': team('Kansas City', 'Chiefs', 'AFC West'), | |
'OAK': team('Oakland', 'Raiders', 'AFC West'), | |
'SD': team('San Diego', 'Chargers', 'AFC West'), | |
'DAL': team('Dallas', 'Cowboys', 'NFC East'), | |
'NYG': team('New York', 'Giants', 'NFC East'), | |
'PHI': team('Philadelphia', 'Eagles', 'NFC East'), | |
'WAS': team('Washington', 'Redskins', 'NFC East'), | |
'CHI': team('Chicago', 'Bears', 'NFC North'), | |
'DET': team('Detroit', 'Lions', 'NFC North'), | |
'GB': team('Green Bay', 'Packers', 'NFC North'), | |
'MIN': team('Minnesota', 'Vikings', 'NFC North'), | |
'ATL': team('Atlanta', 'Falcons', 'NFC South'), | |
'CAR': team('Carolina', 'Panthers', 'NFC South'), | |
'NO': team('New Orleans', 'Saints', 'NFC South'), | |
'TB': team('Tampa Bay', 'Buccaneers', 'NFC South'), | |
'ARI': team('Arizona', 'Cardinals', 'NFC West'), | |
'SEA': team('Seattle', 'Seahawks', 'NFC West'), | |
'SF': team('San Francisco', '49ers', 'NFC West'), | |
'STL': team('St. Louis', 'Rams', 'NFC West')} | |
PlayEntry = namedtuple('PlayEntry', ['game_id', 'quarter', 'minute', 'second', | |
'offense', 'defense', 'down', 'to_go', | |
'yard_line', 'description', | |
'offensive_score', 'defensive_score', | |
'season']) | |
with open('2009_nfl_pbp_data.csv', 'r') as csvfile: | |
reader = csv.reader(csvfile, delimiter=',', quoting=csv.QUOTE_NONE) | |
plays = map(PlayEntry._make, reader) | |
plays.pop(0) | |
class Play(object): | |
def __init__(self, entry): | |
self.game_id = entry.game_id | |
self.quarter = int(entry.quarter) | |
self.time = timedelta(minutes=int(entry.minute), | |
seconds=int(entry.seconds)) | |
def __repr__(self): | |
return ( | |
'Game ID: {game_id}\n' + | |
' Quarter: {quarter:d} | |
Play(plays[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment