def from_xml_api_data(cls, team_info_xml): ns = {"ns": "http://fantasysports.yahooapis.com/fantasy/v2/base.rng"} team_id = int(team_info_xml.find('ns:team_id', ns).text) is_my_team = team_info_xml.find('ns:is_owned_by_current_login', ns) # Convert matchups if they were provided matchups = [] average_stats = None matchup_xml = team_info_xml.find('ns:matchups', ns) if matchup_xml: matchups = [ Matchup.from_xml_api_data(x, team_id) for x in matchup_xml.findall('ns:matchup', ns) ] average_stats = Stats.mean( [x.stats for x in matchups if x.is_complete]) team_kwargs = { 'name' : team_info_xml.find('ns:name', ns).text, 'id' : team_id, 'owner' : team_info_xml\ .find('ns:managers', ns)\ .find('ns:manager', ns)\ .find('ns:nickname', ns).text, 'is_my_team' : bool(is_my_team.text) if isinstance(is_my_team, et.Element) else None, 'waiver_priority' : int(team_info_xml.find('ns:waiver_priority', ns).text), 'move_count' : int(team_info_xml.find('ns:number_of_moves', ns).text), 'trade_count' : int(team_info_xml.find('ns:number_of_trades', ns).text), 'matchups' : matchups, 'average_stats' : average_stats or None } return cls(**team_kwargs)
def from_api_data_with_matchups(cls, raw_team_info, raw_matchup_info): team_id = raw_team_info['team_id'] # Parse matchup info using the Matchup class # Skip any matchups that occur past today's date matchups = [] for raw_matchup in raw_matchup_info: if (datetime.strptime(raw_matchup['week_start'], "%Y-%m-%d") < datetime.now()): matchups.append(Matchup.from_api_data(raw_matchup, team_id)) # Get average stats average_stats = Stats.mean( [x.stats for x in matchups if x.is_complete]) # Setup team info for __init__ team_kwargs = { 'name': raw_team_info['name'], 'id': team_id, 'owner': raw_team_info['managers'][0]['manager']['nickname'], 'is_my_team': 'is_owned_by_current_login' in raw_team_info.keys(), 'waiver_priority': raw_team_info['waiver_priority'], 'move_count': raw_team_info['number_of_moves'], 'trade_count': raw_team_info['number_of_trades'], 'matchups': matchups, 'average_stats': average_stats } return cls(**team_kwargs)
def test_mean(self): stat_kwargs_1 = { 'goals' : 2, 'assists' : 2, 'penalty_minutes' : 2, 'shots_on_goal' : 2, 'hits' : 2, 'blocks' : 2, 'wins' : 2, 'goalie_ga' : 2, 'goalie_gaa' : 2.00, 'goalie_sa' : 2, 'goalie_so' : 2 } stat_kwargs_2 = { 'goals' : 4, 'assists' : 4, 'penalty_minutes' : 4, 'shots_on_goal' : 4, 'hits' : 4, 'blocks' : 4, 'wins' : 4, 'goalie_ga' : 4, 'goalie_gaa' : 4.00, 'goalie_sa' : 4, 'goalie_so' : 4 } stats_1 = Stats(**stat_kwargs_1) stats_2 = Stats(**stat_kwargs_2) result = Stats.mean([stats_1, stats_2]) self.assertEqual(result.goals, 3) self.assertEqual(result.assists, 3) self.assertEqual(result.penalty_minutes, 3) self.assertEqual(result.shots_on_goal, 3) self.assertEqual(result.hits, 3) self.assertEqual(result.blocks, 3) self.assertEqual(result.wins, 3) self.assertEqual(result.goalie_ga, 3) self.assertEqual(result.goalie_gaa, 3.00) self.assertEqual(result.goalie_sa, 3) self.assertEqual(result.goalie_so, 3)
def _get_league_average_stats(teams): """Calculates an average of the average stats for each team""" list_of_team_averages = [x.average_stats for x in teams] return Stats.mean(list_of_team_averages)