def getMatchesByMatchday(self,matchday=getCurrentMatchDay(), league=getDefaultLeague(),season=getCurrentSeason(),ret_dict=True): ''' @matchday: int representing bundesliga matchday (e.g. max 34) @league: string representing League shortcut (e.g. 'bl1') @season: int representing season year (e.g. 2011) @returns: list of dictionaries representing Match objects or list oft Match objects if caller uses ret_dict=False ''' session = Session() try: matches = session.query(Match).join(League).filter(and_(Match.matchDay==matchday, League.shortcut==league, League.season==season)).all() except: raise else: if ret_dict: matchlist = [] for match in matches: matchlist.append(self.dictifier.dictifyMatch(match)) return matchlist else: return matches finally: session.close()
def GET(self): web.header('Content-Type','application/json') season = web.input(season=None) league = web.input(league=None) matchday = web.input(matchday=None) season = season.season league = league.league matchday = matchday.matchday pointslist = [] if not league: league = getDefaultLeague() if not season: season = getCurrentSeason() if not matchday: matchday = getCurrentMatchDay() try: teams = api.getTeams(league=league,season=season,ret_dict=False) except: return json.dumps({'error':'could not return team data'}) else: for team in teams: ppt = api.getPointsPerTeam(team.id) pointslist.append(ppt) # TODO this is not ideal as it doesn't take goal diff into account plist = sorted(pointslist, key=itemgetter('points')) plist.reverse() return json.dumps(plist)
def test(self,matchday,league=getDefaultLeague(),season=getCurrentSeason()): start=time.time() session=Session() if season > getCurrentSeason(): raise StandardError, "not possible to query future season tables" elif season == getCurrentSeason() and matchday > getCurrentMatchDay(): raise StandardError, "not possible to query future match days" else: teams = {} matches = session.query(Match).join(League).filter(\ and_(League.season==season, League.shortcut==league,Match.matchDay<=matchday)).all() for match in matches: t1,t2 = match.team1.id,match.team2.id if not teams.has_key(t1): teams[t1] = {'gf':0,'ga':0,'points':0,'w':0,'l':0,'d':0,} if not teams.has_key(t2): teams[t2] = {'gf':0,'ga':0,'points':0,'w':0,'l':0,'d':0,} if len(match.team1goals) > len(match.team2goals): teams[t1]['points']+=3 teams[t1]['w']+=1 teams[t2]['l']+=1 elif len(match.team1goals) < len(match.team2goals): teams[t2]['points']+=3 teams[t2]['w']+=1 teams[t1]['l']+=1 else: teams[t1]['points']+=1 teams[t2]['points']+=1 teams[t1]['d']+=1 teams[t2]['d']+=1 teams[t1]['gf']+=len(match.team1goals) teams[t2]['gf']+=len(match.team2goals) teams[t1]['ga']+=len(match.team2goals) teams[t2]['ga']+=len(match.team1goals) end=time.time() took = end-start print "%.3f seconds"%took return teams session.close()
def getPointsPerTeam(self,team_id,league=getDefaultLeague(), season=getCurrentSeason(),matchday=getCurrentMatchDay()): session=Session() matches = session.query(Match).join(League).filter(\ and_(League.shortcut==league, League.season==season,Match.matchDay<=matchday, or_(Match.matchTeam1==team_id, Match.matchTeam2==team_id))).all() goals_for,goals_against,points,won,drew,lost = 0,0,0,0,0,0 for match in matches: if match.matchTeam1 == team_id: goals_for += len(match.team1goals) goals_against += len(match.team2goals) if len(match.team1goals) > len(match.team2goals): points+=3 won+=1 elif len(match.team1goals) == len(match.team2goals): points+=1 drew+=1 else: lost+=1 elif match.matchTeam2 == team_id: goals_for += len(match.team2goals) goals_against += len(match.team1goals) if len(match.team2goals) > len(match.team1goals): points+=3 won+=1 elif len(match.team2goals) == len(match.team1goals): points+=1 drew+=1 else: lost+=1 else: raise StandardError, "wtf?! Cannot be..." session.close() return {'goals_for':goals_for,'goals_against':goals_against, 'points':points,'won':won,'drew':drew,'lost':lost, 'played':len(matches), 'difference':goals_for-goals_against,'team_id':team_id}