def get_weekly_info(context): query_weekly = "select * from thisWeekSummary" weekly = query_db(query_weekly, one=True) context['weekly_info']['top_scorer'] = id_to_name(weekly['topScorerId']) context['weekly_info']["top_score"] = weekly['maxScore'] context['weekly_info']['avg_score'] = weekly['averageScore'] context['weekly_info']['low_scorer'] = id_to_name(weekly['lowScorerId']) context['weekly_info']["low_score"] = weekly['minScore'] context['week'] = weekly['week'] if datetime.datetime.now().month < 9: context['year'] = datetime.datetime.now().year - 1 else: context['year'] = datetime.datetime.now().year query_years = "select * from years where year = %d" % context['year'] points = query_db(query_years) pointsDf = pd.DataFrame.from_dict(points) max_points = max(pointsDf['pointsFor']) min_points = min(pointsDf['pointsFor']) id_max = pointsDf.loc[pointsDf['pointsFor'].idxmax()]['teamId'] id_min = pointsDf.loc[pointsDf['pointsFor'].idxmin()]['teamId'] context['weekly_info']['max_score_team'] = id_to_name(id_max) context['weekly_info']['max_score'] = max_points context['weekly_info']['min_score_team'] = id_to_name(id_min) context['weekly_info']['min_score'] = min_points return context
def get_basic_info(context, teamId): query_years = "select * from years where teamId = %d" % teamId teamHistory = query_db(query_years) teamHistoryDf = pd.DataFrame.from_dict(teamHistory) context['totalWins'] = teamHistoryDf['wins'].sum() context['totalLosses'] = teamHistoryDf['losses'].sum() context['totalRotWins'] = teamHistoryDf['rotWins'].sum() context['totalRotLosses'] = teamHistoryDf['rotLosses'].sum() context['championships'] = len( teamHistoryDf[teamHistoryDf['finalStanding'] == 1]) context['rotWins'] = teamHistoryDf['rotWins'].sum() context['rotLosses'] = teamHistoryDf['rotLosses'].sum() currentYear = teamHistoryDf.loc[teamHistoryDf['year'] == max( teamHistoryDf['year'])] context['currentWins'] = int(currentYear.iloc[0]['wins']) context['currentLosses'] = int(currentYear.iloc[0]['losses']) context['teamHistory'] = teamHistoryDf.sort_values(by=['year']) query_years = "select * from years" league_history = pd.DataFrame.from_dict(query_db(query_years)) league_history_wins = league_history.loc[:, ['teamId', 'wins']] all_time_wins = league_history_wins.groupby(['teamId']).sum() all_time_wins.sort_values('wins', ascending=False, inplace=True) wins_place = list(all_time_wins.index).index(teamId) + 1 if wins_place == 1: context['allTimeWinsPlace'] = "1st" elif wins_place == 2: context['allTimeWinsPlace'] = "2nd" elif wins_place == 3: context['allTimeWinsPlace'] = "3rd" else: context['allTimeWinsPlace'] = "%dth" % wins_place
def get_reigning_champ(context): query_champ = "select * from years where year = %d" % ( datetime.datetime.now().year - 1) champ = query_db(query_champ) for team in champ: if team['finalStanding'] == 1: context['current_champion'] = id_to_name(team['teamId'])
def get_standings(context): query_teams = "select * from years where year = %d" % context['year'] teams = pd.DataFrame.from_dict(query_db(query_teams)) standings = teams.sort_values(by=['wins', 'pointsFor'], ascending=False) standings = standings.loc[:, ['teamId', 'wins', 'losses']] teamNameConversion = lambda x: id_to_name(x) standings['teamName'] = standings['teamId'].apply(teamNameConversion) context['standings'] = standings
def get_roto_records(context): query_years = "select * from years where year = %d" % context['year'] year = query_db(query_years) year = pd.DataFrame.from_dict(year) roto = year.sort_values(by=['rotWins'], ascending=False) roto = roto.loc[:, ['teamId', 'rotWins', 'rotLosses']] teamNameConversion = lambda x: id_to_name(x) roto['teamName'] = roto['teamId'].apply(teamNameConversion) context['roto'] = roto
def get_best_year(context, teamId): query_years = "select * from years where teamId = %d" % teamId teamHistory = query_db(query_years) teamHistoryDf = pd.DataFrame.from_dict(teamHistory) bestYear = teamHistoryDf.loc[teamHistoryDf['wins'] == max( teamHistoryDf['wins'])] context['bestYearWins'] = int(bestYear.iloc[0]['wins']) context['bestYearLosses'] = int(bestYear.iloc[0]['losses']) context['bestYearFinalStanding'] = int(bestYear.iloc[0]['finalStanding']) context['bestYear'] = int(bestYear.iloc[0]['year'])
def get_weekly_extras(context): query_weekly = "select * from thisWeekSummary" weekly = query_db(query_weekly, one=True) week = weekly['week'] context['teamOfWeek'] = teamOfWeek(week) context['teamOrder'] = [ 'QB1', 'RB1', 'RB2', 'WR1', 'WR2', 'Flex', 'TE1', 'D/ST1', 'K1' ] bench, start, pointDiff = badManager(week) context['badManBench'] = bench.to_dict() context['badManStart'] = start.to_dict() context['badManDiff'] = pointDiff context['badManManager'] = id_to_name(bench['team'])
def get_historical_rosters(context, teamId): query_rosters = "select * from historicalRosters where teamId = %d" % teamId rosters = pd.DataFrame.from_dict(query_db(query_rosters)) rosters = rosters.sort_values(by=['year'], ascending=False) roster_players = rosters.iloc[:, 2:].set_index(rosters['year']).T context['roster_players'] = {} for year in roster_players.columns: df = pd.DataFrame(columns=['name', 'slot', 'team', 'rank']) row_count = 0 for i in range(0, len(roster_players[year]), 4): df.loc[row_count] = [ roster_players[year][i], roster_players[year][i + 1], roster_players[year][i + 2], roster_players[year][i + 3] ] row_count += 1 new_index = {'QB': 0, 'RB': 1, 'WR': 2, 'TE': 3, 'K': 4, 'D/ST': 5} df['pos_order'] = 6 for i in df.index: if df.loc[i, 'slot'] in new_index: df.loc[i, 'pos_order'] = new_index[df.loc[i, 'slot']] sort = df.sort_values(by=['pos_order', 'rank']) context['roster_players'][year] = sort.T.to_dict()
def createPlayersDf(week): """ Get all the players on a lineup this week. """ query = "select * from players" players = query_db(query) return pd.DataFrame.from_dict(players)