def log(self, question, r): logging.info('Updating datastore with question.') # Insert question into datastore response = json.loads(r.content) answers = response['question']['answers'] answer = answers[0]['text'] if len(answers) > 0 else 'No answer given' phone_number = int(self.request.get('p', '0')) q = Question(phone_number=phone_number, question=question, response=response, answer=answer) q.put() # Update user stats # This isn't atomic, I don't think it's a big deal though. user = Stats.query(Stats.phone_number == phone_number).fetch() if len(user) > 0: user = user[0] else: user = Stats(phone_number=phone_number, number_of_questions=0, most_recent_question=datetime.min) user.number_of_questions += 1 user.most_recent_question = q.time user.put()
def test_from_xml_api_data(self): ns = {"ns": "http://fantasysports.yahooapis.com/fantasy/v2/base.rng"} stats_xml = et.parse('./sample-api-responses/stats.xml')\ .find('ns:stats', ns) result = Stats.from_xml_api_data(stats_xml) expected = Stats(**{ 'goals': 16, 'assists': 42, 'penalty_minutes': 37, 'shots_on_goal': 195, 'hits': 89, 'blocks': 63, 'wins': 3, 'goalie_ga': 24, 'goalie_gaa': 2.94, 'goalie_sa': 253, 'goalie_so': 0 }) self.assertEqual(expected.goals, result.goals) self.assertEqual(expected.assists, result.assists) self.assertEqual(expected.penalty_minutes, result.penalty_minutes) self.assertEqual(expected.shots_on_goal, result.shots_on_goal) self.assertEqual(expected.hits, result.hits) self.assertEqual(expected.blocks, result.blocks) self.assertEqual(expected.wins, result.wins) self.assertEqual(expected.goalie_ga, result.goalie_ga) self.assertEqual(expected.goalie_gaa, result.goalie_gaa) self.assertEqual(expected.goalie_sa, result.goalie_sa) self.assertEqual(expected.goalie_so, result.goalie_so)
def test_greater_than(self): stats_1 = Stats(**self.stat_kwargs) self.stat_kwargs.update( {'goalie_gaa' : 1.12}) stats_2 = Stats(**self.stat_kwargs) self.assertTrue( stats_2 > stats_1 ) self.assertFalse( stats_1 > stats_2 )
def test_less_than(self): stats_1 = Stats(**self.stat_kwargs) self.stat_kwargs.update( {'goalie_gaa' : 1.12}) stats_2 = Stats(**self.stat_kwargs) self.assertTrue( stats_1 < stats_2 ) self.assertFalse( stats_2 < stats_1 )
def test_inequality(self): stats_1 = Stats(**self.stat_kwargs) self.stat_kwargs.update( {'goalie_gaa' : 1.12}) stats_2 = Stats(**self.stat_kwargs) self.assertTrue( stats_1 != stats_2 ) self.assertFalse( stats_1 == stats_2 )
def enemy_deck_analyze(): context = DeckContext(request) if not deckController.updateAsEnemyDeck(context): print("No active battle data. Go attack someone!") return jsonify({"result": "no active battle data"}) deckController.findCombos() deck_path = deckController.createLargeDeckImage() combo_path = deckController.createLargeComboImage() stats = Stats("Stats", deckController) stats.calcStatData() stats.getSuggestions() if deck_path is None: print("No deck results to print.") return jsonify({"result": "No deck results to print."}) else: print("Deck image saved to {0}".format(deck_path)) if combo_path is None: print("No combo results to print.") return jsonify({"result": "No combo results to print."}) else: print("Combo image saved to {0}".format(combo_path)) stats.printToTerminal() return jsonify({"result": "no active battle data"})
def scrape(): """ Scrape team stats for the given seasons. """ for season in [2013, 2014, 2015, 2016]: print 'getting season data for {0}'.format(season) html = get_team_stats_html(season) data_by_team = scrape_team_stats(html) for team_name, data in data_by_team.iteritems(): team = Team.get_or_create(team_name) data['season'] = int(season) data['team_id'] = team.id stats = Stats(**data) Stats.add(stats)
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 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 serialize(obj): """JSON serializer for team objects""" if isinstance(obj, Team): return { "name": obj.name, "id": obj.id, "owner": obj.owner, "is_my_team": obj.is_my_team, "waiver_priority": obj.waiver_priority, "move_count": obj.move_count, "trade_count": obj.trade_count, "matchups": None if not obj.matchups else [Matchup.serialize(x) for x in obj.matchups], "average_stats": None if not obj.average_stats else Stats.serialize( obj.average_stats) } else: raise TypeError(obj)
def test_add(self): stats_1 = Stats(**self.stat_kwargs) stats_2 = Stats(**self.stat_kwargs) result = stats_1 + stats_2 self.assertEqual(result.goals, 2) self.assertEqual(result.assists, 2) self.assertEqual(result.penalty_minutes, 2) self.assertEqual(result.shots_on_goal, 2) self.assertEqual(result.hits, 2) self.assertEqual(result.blocks, 2) self.assertEqual(result.wins, 2) self.assertEqual(result.goalie_ga, 2) self.assertEqual(result.goalie_gaa, 2.22) self.assertEqual(result.goalie_sa, 2) self.assertEqual(result.goalie_so, 2)
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 compare_team_averages(api, team_1_name, team_2_name): teams = load_all_team_info(api) team_1_stats = [x for x in teams if x.name == team_1_name][0].average_stats team_2_stats = [x for x in teams if x.name == team_2_name][0].average_stats deviation = team_1_stats.get_differentials(team_2_stats) print("Stat dev between " + team_1_name + " and " + team_2_name + ":") print(Stats(**deviation)) return deviation
def run(cfg): if not started: print("run") StatsManager.instance().set_stats(Stats(cfg)) try: mock = platform.processor == "x86_64" cam_scanner.start_scan(mock) except Exception as ex: print(ex) else: print("already started")
def get(self): query = Stats.query() stats = [] for stat in query.fetch(): stats.append({'phone_number': stat.phone_number, 'last_call_date': str(stat.most_recent_question), 'number_of_questions': stat.number_of_questions }) output = json.dumps(stats) self.render_json(output)
def get_my_team_stat_deviation(api): teams = load_all_team_info(api) league_average_stats = get_league_average_stats(teams) my_team_avg_stats = [x for x in teams if x.is_my_team][0].average_stats deviation = my_team_avg_stats.get_differentials(league_average_stats) print("My team stat dev from league avg:") print(Stats(**deviation)) return deviation
def serialize(obj): """JSON serializer for matchup objects""" if isinstance(obj, Matchup): return { "week": obj.week, "week_start": str(obj.week_start), "week_end": str(obj.week_end), "has_started": obj.has_started, "is_complete": obj.is_complete, "is_tied": obj.is_tied, "won": obj.won, "stats": Stats.serialize(obj.stats) } else: raise TypeError(obj)
def test_div(self): stats_1 = Stats(**self.stat_kwargs) result = stats_1 / 2 self.assertEqual(result.goals, 0.5) self.assertEqual(result.assists, 0.5) self.assertEqual(result.penalty_minutes, 0.5) self.assertEqual(result.shots_on_goal, 0.5) self.assertEqual(result.hits, 0.5) self.assertEqual(result.blocks, 0.5) self.assertEqual(result.wins, 0.5) self.assertEqual(result.goalie_ga, 0.5) self.assertEqual(result.goalie_gaa, 0.555) self.assertEqual(result.goalie_sa, 0.5) self.assertEqual(result.goalie_so, 0.5)
def ts_complete_states(cls) -> pd.DataFrame: """ State-level time series models of confirmed cases, deaths, CC per 100k, deaths per 100k, confirmed infection rate, death rate. :return: df_states_ts dataframe """ # county-level time series confirmed cases and death dataframe df_counties_ts = CovidCounties.ts_complete_counties() df_states_stats = Stats.states_stats() # group confirmed cases by state - rolling up county level to state level df_conf_states_ts = df_counties_ts.groupby( ['State/Territory', 'Date'])['Confirmed Cases'].sum().reset_index() # merge df_states_stats df_conf_states_ts = pd.merge(df_conf_states_ts, df_states_stats, how='left', on='State/Territory') # merge state level deaths models df_deaths_states_ts = df_counties_ts.groupby( ['State/Territory', 'Date'])['Deaths'].sum().reset_index() df_states_ts = pd.merge(df_conf_states_ts, df_deaths_states_ts, how='left', on=['State/Territory', 'Date']) # create metrics: pop_factor, cc per 100k, deaths per 100k, death rate, confirmed infection rate df_states_ts['pop_factor'] = df_states_ts['Population'].div(100000) df_states_ts['Cases per 100k'] = df_states_ts['Confirmed Cases'].div( df_states_ts['pop_factor']).replace((np.inf, -np.inf, np.nan), (0, 0, 0)).round(0) df_states_ts['Deaths per 100k'] = df_states_ts['Deaths'].div( df_states_ts['pop_factor']).replace((np.inf, -np.inf, np.nan), (0, 0, 0)).round(0) df_states_ts['Death Rate (%)'] = 100 * df_states_ts['Deaths'].div( df_states_ts['Confirmed Cases']).replace((np.inf, -np.inf, np.nan), (0, 0, 0)).round(6) return df_states_ts
def __init__(self, name: str, location: List[int] = [56, 10], gold: int = 0, items: List[Item] = []): """Initialize an instance of the playable character.""" player_weapon: Weapon = Weapon(weapon_type=WeaponType.FISTS, prefix=self.get_random_fists_prefix()) player_armor: Armor = Armor(armor_type=ArmorType.RAGS, prefix=self.get_random_rags_prefix()) player_stats: Stats = Stats(level=1, health=100, speed=1) self.fighter = Fighter(name=name, weapon=player_weapon, armor=player_armor, stats=player_stats) self.location = location self.gold = gold self.items = items self.game_state = GameState.WORLDMAP
def from_api_data(cls, raw_matchup_info, team_id): now = datetime.now() week_start = datetime.strptime(raw_matchup_info['week_start'], "%Y-%m-%d") week_end = datetime.strptime(raw_matchup_info['week_end'], "%Y-%m-%d") has_started = (week_start < now) is_complete = (week_end < now) is_tied = None if not is_complete else bool( raw_matchup_info['is_tied']) matchup_kwargs = { 'week' : raw_matchup_info['week'], 'week_start' : week_start, 'week_end' : week_end, 'has_started' : has_started, 'is_complete' : is_complete, 'is_tied' : is_tied, 'won' : None if (not is_complete or is_tied)\ else bool(raw_matchup_info['winner_team_key'][(raw_matchup_info['winner_team_key'].rfind(".") + 1):] == team_id), 'stats' : None if not has_started else Stats.from_api_data(raw_matchup_info['stats']) } return cls(**matchup_kwargs)
def from_xml_api_data(cls, matchup_xml, team_id): ns = {"ns": "http://fantasysports.yahooapis.com/fantasy/v2/base.rng"} now = datetime.now() week_start = datetime.strptime( matchup_xml.find('ns:week_start', ns).text, "%Y-%m-%d") week_end = datetime.strptime( matchup_xml.find('ns:week_end', ns).text, "%Y-%m-%d") has_started = (week_start < now) is_complete = (week_end < now) is_tied = None if not is_complete else bool( matchup_xml.find('ns:is_tied', ns).text) winner_team_key = matchup_xml.find('ns:winner_team_key', ns) # Get stats teams = matchup_xml.find('ns:teams', ns).findall('ns:team', ns) team = [ x for x in teams if int(x.find('ns:team_id', ns).text) == team_id ][0] stats = team\ .find('ns:team_stats', ns)\ .find('ns:stats', ns)\ matchup_kwargs = { 'week' : int(matchup_xml.find('ns:week', ns).text), 'week_start' : week_start, 'week_end' : week_end, 'has_started' : has_started, 'is_complete' : is_complete, 'is_tied' : is_tied, 'won' : None if (not is_complete or is_tied)\ else bool(winner_team_key.text[(winner_team_key.text.rfind(".") + 1):] == team_id), 'stats' : None if not has_started else Stats.from_xml_api_data(stats) } return cls(**matchup_kwargs)
def agg_complete_states(cls) -> pd.DataFrame: """ Cumulative confirmed cases and deaths by state :return: dataframe """ # import and create base dataframes df_counties_agg = CovidCounties.agg_complete_counties() df_states_stats = Stats.states_stats() # sum confirmed by state df_confirmed_states_agg = df_counties_agg.groupby( 'State/Territory')['confirmed'].sum().reset_index() # merge states stats df_confirmed_states_agg = pd.merge(df_confirmed_states_agg, df_states_stats, how='left', on='State/Territory') # format population with separating commas df_confirmed_states_agg['2019 Est Pop'] = df_confirmed_states_agg[ 'Population'].fillna(0).astype(int).apply( lambda x: '{:,}'.format(x)) # create 'Confirmed Cases' column formatted with commas and keep 'confirmed' column as integer df_confirmed_states_agg['Confirmed Cases'] = df_confirmed_states_agg[ 'confirmed'].apply(lambda x: '{:,}'.format(x)) # create dataframe deaths by state df_deaths_states_agg = df_counties_agg.groupby( 'State/Territory')['deaths'].sum().reset_index() # merge df_confirmed_states_agg and df_deaths_states_agg df_states_agg = pd.merge(df_confirmed_states_agg, df_deaths_states_agg, on='State/Territory', how='left') # create formatted string Deaths column df_states_agg['Deaths'] = df_states_agg['deaths'].fillna(0).apply( lambda x: '{:,}'.format(x)) # create population 100k factor to calculate confirmed cases per 100k and deaths per 100k df_states_agg['pop_factor'] = df_states_agg['Population'].div( 100000).replace((np.inf, -np.inf, np.nan), (0, 0, 0)) # calculate 'Confirmed Cases per 100k' people df_states_agg['cc_per_100k'] = df_states_agg['confirmed'].div( df_states_agg['pop_factor']).replace((np.nan, -np.inf, np.inf), (0, 0, 0)).round(0) # calculate 'Deaths per 100k' people df_states_agg['d_per_100k'] = df_states_agg['deaths'].div( df_states_agg['pop_factor']).replace((np.nan, np.inf, -np.inf), (0, 0, 0)).round(0) # create formatted hover models for Plotly US map df_states_agg['Cases per 100k'] = df_states_agg['cc_per_100k'].apply( lambda x: '{:,}'.format(x)) df_states_agg['Deaths per 100k'] = df_states_agg['d_per_100k'].apply( lambda x: '{:,}'.format(x)) df_states_agg['Death Rate (%)'] = round( 100 * df_states_agg['deaths'].div(df_states_agg['confirmed']).replace( (np.nan, np.inf, -np.inf), (0, 0, 0)), 4) remove_list = [ 'American Samoa', 'Federated States of Micronesia', 'Palau', 'Guam', 'Virgin Islands', 'Marshall Islands', 'Northern Mariana Islands' ] df_states_agg = df_states_agg.drop(df_states_agg.index[ df_states_agg['State/Territory'].isin(remove_list)]) return df_states_agg
def push_stats(self, mac_address, date_time, state, debug_message): self.kv.push_stats(mac_address, Stats(date_time, state, debug_message))
if soup.body.find('table', id='stats'): rows = soup.body.find('table', id='stats').tbody.find_all('tr') # Team page teamName = '' # Fantasy Page fantasy_link = website + player_href + '/fantasy/' + year r = requests.get(fantasy_link) fantasy_stats = BeautifulSoup(r.text, 'lxml').find( 'table', id='player_fantasy').tbody # link list link_list = ['game_date', 'team', 'opp', 'game_result'] for row in rows: stat = Stats() raw_stat_list = row.find_all('td') for cell in raw_stat_list: if cell['data-stat'] == 'team': if cell.a.text != teamName: teamName = cell.a.text team_link = website + cell.a['href'] r = requests.get(team_link) team_logs = BeautifulSoup(r.text, 'lxml').find( 'table', id='games').tbody team_stats = BeautifulSoup( r.text, 'lxml').find('table', id='team_stats').tbody team_log = team_logs.find('td', {
def test_greater_than_equal(self): stats_1 = Stats(**self.stat_kwargs) stats_2 = Stats(**self.stat_kwargs) self.assertTrue( stats_1 >= stats_2 ) self.assertTrue( stats_2 >= stats_1)
def create_stats(db: Session, link_id: str): db_stats = Stats(link_id=link_id) db.add(db_stats) db.commit() db.refresh(db_stats) return db_stats
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)
def test_less_than_equal(self): stats_1 = Stats(**self.stat_kwargs) stats_2 = Stats(**self.stat_kwargs) self.assertTrue( stats_1 <= stats_2 ) self.assertTrue( stats_2 <= stats_1)
def run(): StatsManager.instance().set_stats(Stats("testfile", "6AM")) try: cam_scanner.start_scan(True) except Exception as ex: print(ex)
def test_from_api_data(self): stats_dict = {"stats": [ { "stat": { "stat_id": "1", "value": "8" } }, { "stat": { "stat_id": "2", "value": "16" } }, { "stat": { "stat_id": "5", "value": "20" } }, { "stat": { "stat_id": "14", "value": "97" } }, { "stat": { "stat_id": "31", "value": "27" } }, { "stat": { "stat_id": "32", "value": "33" } }, { "stat": { "stat_id": "19", "value": "2" } }, { "stat": { "stat_id": "22", "value": "2" } }, { "stat": { "stat_id": "23", "value": "1.00" } }, { "stat": { "stat_id": "24", "value": "45" } }, { "stat": { "stat_id": "27", "value": "0" } } ]} result = Stats.from_api_data(stats_dict['stats']) self.assertEqual(result.goals, 8) self.assertEqual(result.assists, 16) self.assertEqual(result.penalty_minutes, 20) self.assertEqual(result.shots_on_goal, 97) self.assertEqual(result.hits, 27) self.assertEqual(result.blocks, 33) self.assertEqual(result.wins, 2) self.assertEqual(result.goalie_ga, 2) self.assertEqual(result.goalie_gaa, 1.00) self.assertEqual(result.goalie_sa, 45) self.assertEqual(result.goalie_so, 0)
def test_equality(self): stats_1 = Stats(**self.stat_kwargs) stats_2 = Stats(**self.stat_kwargs) self.assertTrue(stats_1 == stats_2) self.assertFalse(stats_1 != stats_2)