Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing the Game Clock #200

Closed
mrg1999 opened this issue Oct 10, 2016 · 2 comments
Closed

Accessing the Game Clock #200

mrg1999 opened this issue Oct 10, 2016 · 2 comments

Comments

@mrg1999
Copy link

mrg1999 commented Oct 10, 2016

I'm trying to get the current quarter and clock for games that are in progress. I have the following code:

import time
import` nfldb
db = nfldb.connect()
q = nfldb.Query(db)
q.game(season_year=2016, season_type='Regular', week=5, finished='false')
for game in q.as_games():
    p_c = game.plays[-1].time
    at = game.away_team
    print p_c, at 

I get the following error:
Traceback (most recent call last):
File "pc.py", line 9, in
p_c = game.plays[-1].time
IndexError: list index out of range
_

If I change the finished parameter to 'true' I get the list of all final games without an error but of course I don't need the quarter and time for a game that is already final.
Am I getting this error because the only game that isn't finished hasn't started yet?

@ochawkeye
Copy link
Contributor

Am I getting this error because the only game that isn't finished hasn't started yet?

Precisely. game.plays generates a list of all of the plays that occurred in a game. When you reference game.plays[-1] you are indexing the last play in the list. But a game that hasn't started yet doesn't any any items in the list and therefore can't have a last item in the list (ie. index out of range).

You could take advantage of game.is_playing to give you that desired info for games that are current in progress that will not error out on games that are done or have not started yet.

import nfldb

db = nfldb.connect()
q = nfldb.Query(db)
q.game(season_year=2016, season_type='Regular', week=5)
for game in q.as_games():
    print game, game.finished
    if game.is_playing:
        print game.plays[-1].time
Regular 2016 week 5 on 10/10 at 07:30PM, TB (0) at CAR (0) False
Regular 2016 week 5 on 10/06 at 07:25PM, ARI (33) at SF (21) True
Regular 2016 week 5 on 10/09 at 12:00PM, WAS (16) at BAL (10) True
Regular 2016 week 5 on 10/09 at 12:00PM, NE (33) at CLE (13) True
Regular 2016 week 5 on 10/09 at 12:00PM, PHI (23) at DET (24) True
Regular 2016 week 5 on 10/09 at 12:00PM, CHI (23) at IND (29) True
Regular 2016 week 5 on 10/09 at 12:00PM, TEN (30) at MIA (17) True
Regular 2016 week 5 on 10/09 at 12:00PM, HOU (13) at MIN (31) True
Regular 2016 week 5 on 10/09 at 12:00PM, NYJ (13) at PIT (31) True
Regular 2016 week 5 on 10/09 at 03:05PM, ATL (23) at DEN (16) True
Regular 2016 week 5 on 10/09 at 03:25PM, CIN (14) at DAL (28) True
Regular 2016 week 5 on 10/09 at 03:25PM, BUF (30) at LA (19) True
Regular 2016 week 5 on 10/09 at 03:25PM, SD (31) at OAK (34) True
Regular 2016 week 5 on 10/09 at 07:30PM, NYG (16) at GB (23) True

(Obviously not a great time to run this example since there aren't actually any game being played at the moment)

@mrg1999
Copy link
Author

mrg1999 commented Oct 10, 2016

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants