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

show all winning teams for a season? #28

Closed
szac opened this issue Aug 26, 2013 · 10 comments
Closed

show all winning teams for a season? #28

szac opened this issue Aug 26, 2013 · 10 comments

Comments

@szac
Copy link

szac commented Aug 26, 2013

I can return the winning team if I specify the index of one of the objects, but not all winning from a season. (this api is awesome. thank you!)

@BurntSushi
Copy link
Owner

This does the trick for me:

>>> import nflgame
>>> games = nflgame.games(2012, week=1)
>>> for g in games:
...     print g.winner
...     
... 
DAL
CHI
MIN
NYJ
HOU
NE
DET
WAS
PHI
ATL
SF
ARI
TB
DEN
BAL
SD

That's only for week 1, so you'll need to use nflgame.games(2012) for the whole season.

Unfortunately, the winner attribute is undocumented at the moment...

@ochawkeye
Copy link
Contributor

game.winner and game.loser are pretty powerful! Cool addition.

import nflgame

games = nflgame.games(2010)
teams = {}

for game in games:
    if game.winner not in teams:
        teams[game.winner] = [1, 0]
    else:
        teams[game.winner][0] += 1
    if game.loser not in teams:
        teams[game.loser] = [0, 1]
    else:
        teams[game.loser][1] += 1

for team in teams:
    print team, '%s-%s' % (teams[team][0], teams[team][1])
MIN 6-10
MIA 7-9
CAR 2-14
ATL 13-3
DET 6-10
CIN 4-12
NYJ 11-5
DEN 4-12
BAL 12-4
NYG 10-6
OAK 8-8
TEN 6-10
NO 11-5
DAL 6-10
NE 14-2
SEA 7-9
CHI 11-5
TB 10-6
PIT 12-4
STL 7-9
CLE 5-11
HOU 6-10
GB 10-6
WAS 6-10
JAC 8-8
KC 10-6
PHI 10-6
BUF 4-12
IND 10-6
ARI 5-11
SF 6-10
SD 9-7

@szac
Copy link
Author

szac commented Aug 26, 2013

Sweet! Thanks all.

@BurntSushi
Copy link
Owner

@ochawkeye If you don't mind, I'm going to keep beating you over the head with data structures from the standard library!

from collections import defaultdict
import nflgame

games = nflgame.games(2012)
wins, losses = defaultdict(int), defaultdict(int)
for game in games:
    wins[game.winner] += 1
    losses[game.loser] += 1
for team in (t[0] for t in nflgame.teams):
    print team, '%d-%d' % (wins[team], losses[team])

This abuses the fact that int() == 0. :-)

@ochawkeye
Copy link
Contributor

Mind? You kidding me...This is the only place I ever get any feedback at
all to learn this sort of thing!
On Aug 26, 2013 4:50 PM, "Andrew Gallant" notifications@github.com wrote:

@ochawkeye https://github.com/ochawkeye If you don't mind, I'm going to
keep beating you over the head with data structures from the standard
library. :-)

from collections import defaultdictimport nflgame
games = nflgame.games(2012)wins, losses = defaultdict(int), defaultdict(int)for game in games:
wins[game.winner] += 1
losses[game.loser] += 1for team in (t[0] for t in nflgame.teams):
print team, '%d-%d' % (wins[team], losses[team])

This abuses the fact that int() == 0. :-)


Reply to this email directly or view it on GitHubhttps://github.com//issues/28#issuecomment-23298018
.

@BurntSushi
Copy link
Owner

I'll just leave this here as a taste of things to come with nfldb:

nfldb=# SELECT
nfldb-#     (CASE WHEN home_score > away_score THEN home_team ELSE away_team END) AS winner,
nfldb-#     (CASE WHEN home_score < away_score THEN home_team ELSE away_team END) AS loser
nfldb-# FROM game
nfldb-# WHERE season_type = 'Regular' AND season_year = 2012 AND week = 1;
 winner | loser 
--------+-------
 DAL    | NYG
 CHI    | IND
 MIN    | JAC
 NYJ    | BUF
 HOU    | MIA
 NE     | TEN
 DET    | STL
 WAS    | NO
 PHI    | CLE
 ATL    | KC
 SF     | GB
 ARI    | SEA
 TB     | CAR
 DEN    | PIT
 BAL    | CIN
 SD     | OAK
(16 rows)

@szac
Copy link
Author

szac commented Aug 31, 2013

Thanks for the help, guys. However, is it possible to use nflgame to loop over an entire season and return the winners by week? The only thing I see is gamekey

Backstory: We're running a fantasy league of whole team owners that get points for wins each week. I'd like to run a script once per week to build out a json file of weekly winners and losers. I'll consume this json to output the standings.

Thanks again!

@BurntSushi
Copy link
Owner

Hmm. I thought that was exactly what I gave you. If you give it a week number (in the nflgame.games line), then it should give you the winners for that week. You can get losers, too:

>>> import nflgame
>>> games = nflgame.games(2012, week=1)
>>> for g in games:
...     print g.winner, g.loser
...     
... 
DAL NYG
CHI IND
MIN JAC
NYJ BUF
HOU MIA
NE TEN
DET STL
WAS NO
PHI CLE
ATL KC
SF GB
ARI SEA
TB CAR
DEN PIT
BAL CIN
SD OAK

If you want to do an entire season, then just get rid of the week=1 parameter. Or you could loop explicitly:

import nflgame
for week in range(1, 18):
    games = nflgame.games(2012, week=week)
    for g in games:
        print week, g.winner, g.loser

@szac
Copy link
Author

szac commented Aug 31, 2013

Yep, the week param. setting week var to a range was where I was getting stuck! I was trying to dig out week within the games list. I never thought of just using range. Thanks again, brother. This is awesome.

@BurntSushi
Copy link
Owner

👍

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

3 participants