BurntSushi / nfldb

A library to manage and update NFL data in a relational database.
The Unlicense
1.08k stars 264 forks source link

Combining player objects #65

Open ochawkeye opened 10 years ago

ochawkeye commented 10 years ago

Doing something like the following results in having multiple "keys" in my points dictionary that are the same type and appear to be the same key (but aren't).

from collections import defaultdict
import nfldb

def tally_points(db, y, w, ph):
    q = nfldb.Query(db)
    player_score = defaultdict(int)

    q.game(season_year=y, week=w, season_type=ph)
    for pp in q.as_aggregate():
        player_score[pp.player] += 1.0*pp.kicking_xpmade

    q.play_player(kicking_fgm=1)
    for pp in q.as_play_players():
        fg_length = getattr(pp, 'kicking_fgm_yds')
        if fg_length < 40:
            player_score[pp.player] += 3.0
        elif fg_length < 50:
            player_score[pp.player] += 4.0
        else:
            player_score[pp.player] += 5.0
    return player_score

if __name__ == '__main__':
    db = nfldb.connect()
    player_points = tally_points(db, 2014, 6, 'Regular')
    for player in player_points:
        if player.player_id == '00-0028660':  # Dan Bailey: 3XP, 1short, 1medium, 1long FG
            print player, type(player), player_points[player]
    print '-'*25
    for player in player_points:
        if player.player_id == '00-0025565':  # Nick Folk: 2XP, 1short FG
            print player, type(player), player_points[player]
    print '-'*25
    for player in player_points:
        if player.player_id == '00-0026858':  # Graham Gano: 4XP, 2short, 1medium FG
            print player, type(player), player_points[player]
Dan Bailey (DAL, K) <class 'nfldb.types.Player'> 3.0
Dan Bailey (DAL, K) <class 'nfldb.types.Player'> 4.0
Dan Bailey (DAL, K) <class 'nfldb.types.Player'> 5.0
Dan Bailey (DAL, K) <class 'nfldb.types.Player'> 3.0
-------------------------
Nick Folk (NYJ, K) <class 'nfldb.types.Player'> 3.0
Nick Folk (NYJ, K) <class 'nfldb.types.Player'> 2.0
-------------------------
Graham Gano (CAR, K) <class 'nfldb.types.Player'> 4.0
Graham Gano (CAR, K) <class 'nfldb.types.Player'> 3.0
Graham Gano (CAR, K) <class 'nfldb.types.Player'> 4.0
Graham Gano (CAR, K) <class 'nfldb.types.Player'> 3.0

With Bailey kicking 3FGs & 3XP and Folk kicking 1FG & 1 XP I would expect the output to be

Dan Bailey (DAL, K) <class 'nfldb.types.Player'> 15.0
-------------------------
Nick Folk (NYJ, K) <class 'nfldb.types.Player'> 5.0
-------------------------
Graham Gano (CAR, K) <class 'nfldb.types.Player'> 14.0

Is there a way to merge these pp.player objects like I am trying to do or should I be using something else as the key in my player_score dictionary? If I use something else, can I still retain all of the pp.player objects properties so that I could still access all of that player info from the object?

    for player in player_points:
        if player.player_id == '00-0025565':  # Nick Folk
            print player, type(player), player_points[player], player.team
Nick Folk (NYJ, K) <class 'nfldb.types.Player'> 5.0 NYJ

gist

ochawkeye commented 10 years ago

What I ended up doing is convert the key to a string that would be consistent across queries and pre-collected info that I might want to use later on.

from collections import defaultdict
import nfldb

def tally_points(db, y, w, ph):
    q = nfldb.Query(db)
    player_score = defaultdict(int)
    player_metadata = {}

    q.game(season_year=y, week=w, season_type=ph)
    for pp in q.as_aggregate():
        p_id = str(pp.player)
        player_metadata[p_id] = [pp.player.team, pp.player.position]
        player_score[p_id] += 1.0*pp.kicking_xpmade

    q.play_player(kicking_fgm=1)
    for pp in q.as_play_players():
        p_id = str(pp.player)
        player_metadata[p_id] = [pp.player.team, pp.player.position]
        fg_length = getattr(pp, 'kicking_fgm_yds')
        if fg_length < 40:
            player_score[p_id] += 3.0
        elif fg_length < 50:
            player_score[p_id] += 4.0
        else:
            player_score[p_id] += 5.0

    return player_score, player_metadata

if __name__ == '__main__':
    db = nfldb.connect()
    player_points, p_info = tally_points(db, 2014, 6, 'Regular')
    for player in player_points:
        if player == 'Dan Bailey (DAL, K)':
            print type(player), player, player_points[player], p_info[player][0], p_info[player][1]
    print '-'*25
    for player in player_points:
        if player == 'Nick Folk (NYJ, K)':
            print type(player), player, player_points[player], p_info[player][0], p_info[player][1]
    print '-'*25
    for player in player_points:
        if player == 'Graham Gano (CAR, K)':
            print type(player), player, player_points[player], p_info[player][0], p_info[player][1]
<type 'str'> Dan Bailey (DAL, K) 15.0 DAL K
-------------------------
<type 'str'> Nick Folk (NYJ, K) 5.0 NYJ K
-------------------------
<type 'str'> Graham Gano (CAR, K) 14.0 CAR K