Exemple #1
0
def run():
    """Runs the `nflrank` command."""
    db = nfldb.connect()
    _, cur_year, _ = nfldb.current(db)

    parser = argparse.ArgumentParser(
        description='Show NFL player rankings for statistical categories.')
    aa = parser.add_argument
    aa(dest='categories', metavar='CATEGORY', nargs='+')
    aa('--years', type=str, default=str(cur_year),
       help='Show rankings only for the inclusive range of years given,\n'
            'e.g., "2010-2011". Other valid examples: "2010", "-2010",\n'
            '"2010-".')
    aa('--weeks', type=str, default='',
       help='Show rankings only for the inclusive range of weeks given,\n'
            'e.g., "4-8". Other valid examples: "4", "-8",\n'
            '"4-".')
    aa('--pre', action='store_true',
       help='When set, only data from the preseason will be used.')
    aa('--post', action='store_true',
       help='When set, only data from the postseason will be used.')
    aa('--pos', type=str, default=[], nargs='+',
       help='When set, only show players in the given positions.')
    aa('--teams', type=str, default=[], nargs='+',
       help='When set, only show players currently on the given teams.')
    aa('--limit', type=int, default=10,
       help='Restrict the number of results shown.')
    args = parser.parse_args()

    for cat in args.categories:
        if cat not in nfldb.stat_categories:
            eprint("%s is not a valid statistical category.", cat)
            sys.exit(1)

    stype = 'Regular'
    if args.pre:
        stype = 'Preseason'
    if args.post:
        stype = 'Postseason'

    years = nflcmd.arg_range(args.years, 2009, cur_year)
    weeks = nflcmd.arg_range(args.weeks, 1, 17)

    def to_games(agg):
        syrs = years[0] if len(years) == 1 else '%d-%d' % (years[0], years[-1])
        qgames = nflcmd.query_games(db, agg.player, years, stype, weeks)
        return nflcmd.Games(db, syrs, qgames.as_games(), agg)

    catq = nfldb.QueryOR(db)
    for cat in args.categories:
        k = cat + '__ne'
        catq.play_player(**{k: 0})

    q = nfldb.Query(db)
    q.game(season_year=years, season_type=stype, week=weeks)
    q.andalso(catq)
    if len(args.pos) > 0:
        posq = nfldb.QueryOR(db)
        for pos in args.pos:
            posq.player(position=nfldb.Enums.player_pos[pos])
        q.andalso(posq)
    if len(args.teams) > 0:
        q.player(team=args.teams)
    q.sort([(cat, 'desc') for cat in args.categories])
    q.limit(args.limit)
    pstats = map(to_games, q.as_aggregate())

    spec = ['name', 'team', 'game_count'] + args.categories
    rows = [nflcmd.header_row(spec)]
    rows += map(partial(nflcmd.pstat_to_row, spec), pstats)
    print(nflcmd.table(rows))
Exemple #2
0
def run():
    """Runs the `nflstats` command."""
    db = nfldb.connect()
    _, cur_year, _ = nfldb.current(db)

    parser = argparse.ArgumentParser(
        description='Show NFL game stats for a player.')
    aa = parser.add_argument
    aa(dest='player_query', metavar='PLAYER', nargs='+')
    aa('--team', type=str, default=None,
       help='Specify the team of the player to help the search.')
    aa('--pos', type=str, default=None,
       help='Specify the position of the player to help the search.')
    aa('--soundex', action='store_true',
       help='When set, player names are compared using Soundex instead '
            'of Levenshtein.')
    aa('--year', type=str, default=cur_year,
       help='Show game logs for only this year. (Not applicable if '
            '--season is set.)')
    aa('--pre', action='store_true',
       help='When set, only games from the preseason will be used.')
    aa('--post', action='store_true',
       help='When set, only games from the postseason will be used.')
    aa('--weeks', type=str, default='',
       help='Show stats only for the inclusive range of weeks given,\n'
            'e.g., "4-8". Other valid examples: "4", "-8",\n'
            '"4-". Has no effect when --season is used.')
    aa('--season', action='store_true',
       help='When set, statistics are shown by season instead of by game.')
    aa('--show-as', type=str, default=None,
       help='Force display of player as a particular position. This may need '
            'to be set for inactive players.')
    args = parser.parse_args()

    args.player_query = ' '.join(args.player_query)
    player = nflcmd.search(db, args.player_query, args.team, args.pos,
                           args.soundex)
    if player is None:
        eprint("Could not find a player given the criteria.")
        sys.exit(1)
    print('Player matched: %s' % player)

    week_range = nflcmd.arg_range(args.weeks, 1, 17)
    stype = 'Regular'
    if args.pre:
        stype = 'Preseason'
    if args.post:
        stype = 'Postseason'


    pos = None
    if args.show_as is not None:
        pos = nfldb.Enums.player_pos[args.show_as]
    elif player.position == nfldb.Enums.player_pos.UNK:
        q = nfldb.Query(db)
        q.play_player(player_id=player.player_id)
        q.sort(('gsis_id', 'desc'))
        pos = nfldb.guess_position(q.as_play_players())
        if pos == nfldb.Enums.player_pos.UNK:
            eprint("The player matched is not active and I could not guess\n"
                   "his position. Specify it with the '--show-as' flag.")
            sys.exit(1)
        print("Guessed position: %s" % pos)

    if args.season:
        show_season_table(db, player, stype, week_range, pos)
    else:
        show_game_table(db, player, args.year, stype, week_range, pos)