def runparse(PBPFiles, conn): ''' This will probably iterate over several pbp files, but could just be one; for now, just deal with the basic stuff (as-provided play-by- play files, NOT new-fangled player action data, global player IDs, player game IDs, etc.); ''' for PBP in PBPFiles: # Step 1: Parse file (for now, just basic parse...) ''' Load and close; Use parse play-by-play; etc; ''' gstats, pstats = parsegamefile(args_in) # commit all of pbp to table # load play-by-play and find game indicies; pbp is # ['gameID','linenum','times','actions'] gamedict, pbp = LAS.getpbp(fhandlepbp, 'all') cursor = conn.cursor() for entry in pbp: cursor.execute(''' INSERT INTO GamePBP (GameID, LineNum, TimeRemaining, Entry) VALUES(%s,%s,%s,%s,%s) ''', entry) cursor.close() # Step 2: Get last line in pbp table: last_val = getlastID(conn, "GamePBP") # Step 3: Create [(GameID, Start_Value, Stop_Value)] using game_dict cursor = conn.cursor() last_val += 1 for game in gamedict.keys(): cursor.execute(''' INSERT INTO GamePBP (GameID, StartPBPLine, StopPBPLine) VALUES(%s,%s,%s) ''',(game, last_val+gamedict[game][0]+1, last_val+gamedict[game][1]+1)) cursor.close()
''' out = dict() for game in games.keys(): out[game] = game.getgame() out['statlist'] = NPAP.getstatlist() return out print('checking files and loading game files...') chdir = os.getcwd() pbpfname = "playbyplay20072008reg20081211" plafname = "players20072008reg20081211" fhandlepbp = os.path.join(chdir,'DataFiles', pbpfname+'.txt') fhandlenam = os.path.join(chdir,'DataFiles', plafname+'.txt') # initialize players dictionary, game dictionary pstats = LAS.createplayers(fhandlenam) gstats = [] # load play-by-play and find game indicies; pbp is times and actions gamedict, pbp = LAS.getpbp(fhandlepbp, ['times', 'actions']) # iterate over games and parse play-by-play print('parsing play-by-play data...') for game in gamedict.keys(): print('on game', game) # get teams home, away = game[-3:], game[-6:-3] teams = [home, away] # get players on those teams active = [p for p in pstats.keys() if pstats[p].team() in teams] # initialize new game for those players for p in active:
import NBA_loadandsplit as LAS import NBA_parseplaybyplay as PPBP import NBA_playerclass as NBAP ''' Assume that each play-by-play file has an associated "players" file that provides the list of active players and their ESPN IDs, and an associated player-stats file with data for each game that allows starters to be assessed; ''' datadirectory = os.path.join(chdir, 'DataFiles') playbyplayfile = os.path.join(datadirectory, playbyplayname) playerstotfile = os.path.join(datadirectory, playerstotname) playesstatfile = os.path.join(datadirectory, playesstatname) gstats = list() pstats = LAS.createplayers(playerstotfile) gamedict, pbp = LAS.getpbp(playbyplayfile, ['times', 'actions']) print('parsing play-by-play data...') for game in gamedict.keys(): print('on game', game) teams = [game[-3:], game[-6:-3]] # [home, away] curplayers = [p for p in pstats.keys() if pstats[p].team() in teams] curgame = pbp[gamedict[game][0]:gamedict[game][1]] for p in curplayers: pstats[p]._newgame(game) pstats, score = PPBP.processgame(curgame, pstats, teams, curplayers) played_game = [p for p in curplayers if pstats[p].playedgame()] for p in active: pstats[p]._flushgame()
import NBA_parseplaybyplay as PPBP import NBA_playerclass as NBAP ''' Assume that each play-by-play file has an associated "players" file that provides the list of active players and their ESPN IDs, and an associated player-stats file with data for each game that allows starters to be assessed; ''' datadirectory = os.path.join(chdir,'DataFiles') playbyplayfile = os.path.join(datadirectory, playbyplayname) playerstotfile = os.path.join(datadirectory, playerstotname) playesstatfile = os.path.join(datadirectory, playesstatname) gstats = list() pstats = LAS.createplayers(playerstotfile) gamedict, pbp = LAS.getpbp(playbyplayfile, ['times', 'actions']) print('parsing play-by-play data...') for game in gamedict.keys(): print('on game', game) teams = [game[-3:], game[-6:-3]] # [home, away] curplayers = [p for p in pstats.keys() if pstats[p].team() in teams] curgame = pbp[gamedict[game][0]:gamedict[game][1]] for p in curplayers: pstats[p]._newgame(game) pstats, score = PPBP.processgame(curgame, pstats, teams, curplayers) played_game = [p for p in curplayers if pstats[p].playedgame()] for p in active: pstats[p]._flushgame() return gstats, pstats print('Done with current file')