def get_bat_stats_for_lineup_spot_by_player_for_team( self, bat_order_list: List[int], team_id_bbref: str, year: int) -> List[BatStatsMetrics]: team_stats = db.Team_BatStats_By_BatOrder_By_Player_By_Year.get_bat_stats_for_lineup_spot_by_player_for_team( self.db_engine, bat_order_list, team_id_bbref, year) grouped = group_and_sort_list(team_stats, "bat_order", "total_games", sort_all_desc=True) return flatten_list2d(iter(grouped.values()))
def get_bat_stats_for_defpos_by_player_for_team( self, def_position_list: List[DefensePosition], team_id_bbref: str, year: int) -> List[BatStatsMetrics]: def_pos_num_list = [int(def_pos) for def_pos in def_position_list] team_stats = db.Team_BatStats_By_DefPosition_By_Player_By_Year.get_bat_stats_for_defpos_by_player_for_team( self.db_engine, def_pos_num_list, team_id_bbref, year) grouped = group_and_sort_list(team_stats, "def_position", "total_games", sort_all_desc=True) return flatten_list2d(iter(grouped.values()))
def get_bat_stats_for_lineup_spot_by_year_for_team( self, bat_order_list: List[int], team_id_bbref: str) -> Dict[int, BatStatsMetrics]: team_stats = db.Team_BatStats_By_BatOrder_By_Year.get_bat_stats_for_lineup_spot_by_year_for_team( self.db_engine, bat_order_list, team_id_bbref) grouped = group_and_sort_list(team_stats, "bat_order", "total_games", sort_all_desc=True) team_stats_sorted = flatten_list2d(iter(grouped.values())) return {s.year: s for s in team_stats_sorted}
def get_bat_stats_for_defpos_by_year_for_team( self, def_position_list: List[DefensePosition], team_id_bbref: str) -> Dict[int, BatStatsMetrics]: def_pos_num_list = [int(def_pos) for def_pos in def_position_list] team_stats = db.Team_BatStats_By_DefPosition_By_Year.get_bat_stats_for_defpos_by_year_for_team( self.db_engine, def_pos_num_list, team_id_bbref) grouped = group_and_sort_list(team_stats, "def_position", "total_games", sort_all_desc=True) team_stats_sorted = flatten_list2d(iter(grouped.values())) return {s.year: s for s in team_stats_sorted}
def get_all_bbref_game_ids_all_pitchfx_logs_are_valid( cls, db_engine, year, season_type=SeasonType.REGULAR_SEASON): s = select([cls.bbref_game_id]).where( and_( cls.year == year, cls.season_type == season_type, cls.total_pitchfx == cls.total_combined_pitchfx_bbref_data, cls.total_pitchfx_error == 0, cls.total_invalid_pitchfx == 0, )) results = db_engine.execute(s).fetchall() return flatten_list2d( [d.values() for d in [dict(row) for row in results]])
def _get_missing_pfx_ids_for_date(db_session, date_status): if date_status.scraped_all_pitchfx_logs: return ["All PitchFX logs have been scraped"] if not date_status.scraped_all_brooks_pitch_logs: return [ "Pitch Appearance IDs without PitchFx data cannot be reported until all pitch logs have been scraped." ] missing_ids = db.DateScrapeStatus.get_unscraped_pitch_app_ids_for_date( db_session, date_status.game_date) return flatten_list2d([ _format_id_list(game_id, id_list) for game_id, id_list in missing_ids.items() ])
def get_all_pfx_within_date_range_for_player( request: Request, response: Response, mlb_id: int, date_range: tuple = Depends(get_date_range), app: Vigorish = Depends(get_vig_app), ): start_date, end_date = date_range player_data = crud.get_player_data(mlb_id, app) all_pfx = [ p.pitchfx for p in player_data.pitch_app_map.values() if p.game_date >= start_date and p.game_date <= end_date ] return flatten_list2d(all_pfx)
def get_all_bbref_game_ids_eligible_for_audit(cls, db_engine, year, season_type=SeasonType. REGULAR_SEASON): s = select([cls.bbref_game_id]).where( and_( cls.year == year, cls.season_type == season_type, cls.total_pitchfx == cls.total_pitchfx_scraped, cls.total_pitchfx != cls.total_combined_pitchfx_bbref_data, )) results = db_engine.execute(s).fetchall() return flatten_list2d( [d.values() for d in [dict(row) for row in results]])
def find_position_changes(player_bat_stats, position_start): if not player_bat_stats["substitutions"]: return position_start search_results = [ PLAYER_SUB_POS_CHANGE_REGEX.search(sub_event["sub_description"]) for sub_event in player_bat_stats["substitutions"] ] is_starter = player_bat_stats["is_starter"] position_changes = flatten_list2d( [list(match.groups()) for match in search_results if match]) incoming_player_pos = player_bat_stats["substitutions"][0][ "incoming_player_pos"] outgoing_player_pos = player_bat_stats["substitutions"][0][ "outgoing_player_pos"] return ("-".join(position_changes) if position_changes else outgoing_player_pos if is_starter else incoming_player_pos)
def total_urls(self): return len(flatten_list2d(list(self.all_urls.values()))) if self.all_urls else 0
def all_pitch_app_ids_for_date(self): return sorted(flatten_list2d([g.all_pitch_app_ids for g in self.games]))
def at_bats_invalid_pitchfx(self): return flatten_list2d(pitch_app_dict.values() for pitch_app_dict in self.invalid_pfx.values())
def at_bats_pitchfx_error(self): return flatten_list2d( pitch_app_dict.values() for pitch_app_dict in self.all_pfx_errors.values())
def pitch_apps_invalid_pitchfx(self): return flatten_list2d( list(pitch_app_dict.keys()) for pitch_app_dict in self.invalid_pfx.values())
def pitch_apps_pitchfx_error(self): return flatten_list2d( list(pitch_app_dict.keys()) for pitch_app_dict in self.pfx_errors.values())
def all_eligible_games_in_season(self): return sorted(flatten_list2d(list(self.date_game_id_map.values())))
def wrap_text(input_str, max_len): input_strings = [s for s in input_str.split("\n") if s] wrapped = flatten_list2d([_wrap_string(s, max_len) for s in input_strings]) wrapped = _replace_newlines(input_str, wrapped) return "\n".join(wrapped)