BurntSushi / nflgame

An API to retrieve and read NFL Game Center JSON data. It can work with real-time data, which can be used for fantasy football.
http://pdoc.burntsushi.net/nflgame
The Unlicense
1.27k stars 412 forks source link

UnboundLocalError while running nfldb-update #60

Closed brianwu02 closed 10 years ago

brianwu02 commented 10 years ago

running nflgame-update-players:

Loading games for REG 2013 week 13
Finding (profile id -> gsis id) mapping for players...
1/1 complete. (100.00%)Traceback (most recent call last):
  File "/home/brian/Envs/fantasyfootballtracker/bin/nflgame-update-players", line 4, in <module>
    nflgame.update_players.run()
  File "/home/brian/Envs/fantasyfootballtracker/local/lib/python2.7/site-packages/nflgame/update_players.py", line 341, in run
    if pid is None:
UnboundLocalError: local variable 'pid' referenced before assignment

i wish i could provide more information about the error above other than i was running nfldb-update in cron job and error first occured on 12-01.

Here is last successful update:

 3463 STARTING NFLDB UPDATE AT 2013-11-24 23:59:02.484144
 3464 Connecting to nfldb... done.
 3465 Setting timezone to UTC... done.
 3466 Locking write access to tables... done.
 3467 Updating season phase, year and week... done.
 3468 Updating 1 games in progress...
 3469     Regular 2013 week 12 on 11/25 at 01:30AM, DEN (31) at NE (31)
 3470 done.
 3471 Closing database connection... done.
 3472 FINISHED NFLDB UPDATE AT 2013-11-24 23:59:06.032480 

Here is the first failure:

 3475 STARTING NFLDB UPDATE AT 2013-12-01 00:00:02.316508
 3476 Connecting to nfldb... done.
 3477 Setting timezone to UTC... done.
 3478 Updating player JSON database... (last update was 2013-11-27 18:06:46.054501+00:00)
 3479 Loading games for REG 2013 week 13
 3480 Finding (profile id -> gsis id) mapping for players...
 3481 ^M1/1 complete. (100.00%)Traceback (most recent call last):
 3482   File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
 3483     "__main__", fname, loader, pkg_name)
 3484   File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
 3485     exec code in run_globals
 3486   File "/home/ocean/Envs/fantasyfootball-env/lib/python2.7/site-packages/nflgame/update_players.py", line 442, in <module>
 3487     run()
 3488   File "/home/ocean/Envs/fantasyfootball-env/lib/python2.7/site-packages/nflgame/update_players.py", line 341, in run
 3489     if pid is None:
 3490 UnboundLocalError: local variable 'pid' referenced before assignment
 3491 `/home/ocean/Envs/fantasyfootball-env/bin/python -m nflgame.update_players --no-block` failed (exit status 255)
 3492 Locking write access to tables... done.
 3493 Updating season phase, year and week... done.
 3494 Closing database connection... done.
 3495 FINISHED NFLDB UPDATE AT 2013-12-01 00:00:06.209304
BurntSushi commented 10 years ago

My suspicion is that you're using an old version of nflgame. Could you try updating? Make sure nfldb is up to date too, for good measure.

pip install -U nflgame
pip install -U nfldb
brianwu02 commented 10 years ago

Wow, who woulda thunk updating would fix bugs :D. Thank you thank you!

So is this simply a minor bug where nflgame is in some weird edge case state causing UnboundLocalError or is nfl.com the culprit?

BurntSushi commented 10 years ago

Nope, this is just a good ol' fashion bug that would have been caught by an integration test if they existed for nflgame (or by static analysis in a more civilized language). Note the afflicted piece of code:

for i, t in enumerate(pool.imap(fetch, players.items()), 1):
            gid, name, purl = t
            progress(i, len(players))
            if pid is None:
                errors.append('Could not get profile URL for (%s, %s)'
                              % (gid, name))
                continue

            assert gid not in metas
            pid = profile_id_from_url(purl)
            metas[gid] = {'gsis_id': gid, 'gsis_name': name,
                          'profile_url': purl, 'profile_id': pid}
            reverse[pid] = gid

You can see on line 4 that pid is referenced before it's ever assigned a few lines below it. My guess is that I rearranged the code and didn't test it. And it wasn't triggered because this code is only executed when there is a new player added to the league that hasn't been seen by nflgame before.

So hopefully fixed for good! :-)

(The intention is that weird edge states induced by NFL.com are handled gracefully. The program shouldn't die because of malformed data.)