def multi_season_prompt(db_session, prompt=None, heading=None): if not prompt: prompt = "Select one or multiple seasons from the list below:" all_seasons = db.Season.get_all_regular_seasons(db_session) choices = {f"{season.year}": season.year for season in all_seasons} instructions = "(use SPACE BAR to select each file type, ENTER to confirm your selections)" seasons_prompt = Check( prompt=instructions, choices=list(choices.keys()), check=EMOJIS.get("CHECK", ""), shift=1, indent=0, margin=2, check_color=colors.foreground["default"], check_on_switch=colors.foreground["default"], word_color=colors.foreground["default"], word_on_switch=colors.bright(colors.foreground["cyan"]), background_color=colors.background["default"], background_on_switch=colors.background["default"], ) selected_years = [] while not selected_years: subprocess.run(["clear"]) if heading: print_heading(heading, fg="bright_yellow") print_message(prompt, wrap=True) result = seasons_prompt.launch() if not result: print_error("\nYou must select at least one season!") pause(message="Press any key to continue...") continue selected_years = [choices[sel] for sel in result] return selected_years
def select_team_game_id(self): while True: result = self.select_season_prompt() if result.failure: return result self.mlb_season = result.value while True: result = self.select_team_prompt() if result.failure: self.team_id = None break game_ids = result.value while True: subprocess.run(["clear"]) heading = ( "Scraped Data Viewer - Select Game (MLB Season: {self.mlb_season}, db.Team: {self.team_id})" ) print_heading(heading, fg="bright_yellow") result = select_game_prompt(game_ids, use_numbers=False, clear_screen=False) if result.failure: break self.game_id = result.value view_game_data = ViewGameData(self.app, self.game_id) view_game_data.launch()
def print_matchup_and_linescore(self): subprocess.run(["clear"]) matchup = self.game_data.get_matchup_details() linescore = self.game_data.get_tui_linescore() print_heading(f"Scraped Data Viewer for Game ID: {self.bbref_game_id}", fg="bright_yellow") print_message(matchup, fg="bright_cyan", bold=True, wrap=False) print_message(linescore, fg="bright_cyan", wrap=False)
def prompt_user_run_task(self): subprocess.run(["clear"]) prompt = "Would you like to run this task and backup the database?" pages = [ DisplayPage(page, None) for page in self.get_task_description_pages() ] page_viewer = PageViewer(pages, prompt=prompt, text_color="bright_yellow") print_heading("Backup Database to CSV", fg="bright_yellow") return page_viewer.launch()
def prompt_user_choose_method_to_select_game(self): subprocess.run(["clear"]) print_heading("Scraped Data Viewer - Please choose an option below", fg="bright_yellow") prompt = "How would you like to locate the game you wish to view?" choices = { f"{MENU_NUMBERS.get(1)} Enter Game ID Manually": "MANUAL", f"{MENU_NUMBERS.get(2)} Browse by Team": "TEAM", f"{MENU_NUMBERS.get(3)} Browse by Season": "SEASON", f"{EMOJIS.get('BACK')} Return to Previous Menu": None, } return user_options_prompt(choices, prompt, clear_screen=False)
def data_sets_prompt( heading: str = None, prompt: str = None, valid_data_sets: Optional[List[DataSet]] = None, checked_data_sets: Optional[List[DataSet]] = None, ): if not prompt: prompt = "Select one or multiple data sets from the list below:" if not valid_data_sets: valid_data_sets = [DataSet.ALL] instructions = "(use SPACE BAR to select each data set, ENTER to confirm your selections)" valid_data_sets_int = sum(int(ds) for ds in valid_data_sets) choices = { f"{DATA_SET_TO_NAME_MAP[ds]}": ds for ds in DataSet if valid_data_sets_int & ds == ds } if checked_data_sets: checked_int = sum(int(ds) for ds in checked_data_sets) checked_data_sets = [ f"{DATA_SET_TO_NAME_MAP[ds]}" for ds in DataSet if checked_int & ds == ds ] ds_prompt = DataSetCheck( prompt=instructions, choices=list(choices.keys()), checked_data_sets=checked_data_sets, check=EMOJIS.get("CHECK", ""), shift=1, indent=0, margin=2, check_color=colors.foreground["default"], check_on_switch=colors.foreground["default"], background_color=colors.foreground["default"], background_on_switch=colors.foreground["default"], word_color=colors.foreground["default"], word_on_switch=colors.bright(colors.foreground["cyan"]), ) data_sets = [] while not data_sets: subprocess.run(["clear"]) if heading: print_heading(heading, fg="bright_yellow") print_message(prompt, wrap=True) result = ds_prompt.launch() if not result: print_error("\nYou must select at least one data set!") pause(message="Press any key to continue...") continue data_sets = [DATA_SET_FROM_NAME_MAP[sel] for sel in result] return data_sets
def launch(self): while True: subprocess.run(["clear"]) if self.heading: print_heading(self.heading, fg=self.heading_color) result = super().launch() if not result: continue try: return date_parser.parse(result) except ValueError: error = f'\nError: "{result}" could not be parsed as a valid date.' print_heading(error, fg="bright_red") print_message(ERROR_HELP_MESSAGE, fg="bright_red") print_message(EXAMPLES, fg="bright_red") pause(message="Press any key to continue...")
def select_season_prompt(self): subprocess.run(["clear"]) print_heading("Scraped Data Viewer - Select MLB Season", fg="bright_yellow") prompt = "Select an MLB season from the list below:" choices = { f"{MENU_NUMBERS.get(num, str(num))} {year}": year for num, ( year, results) in enumerate(self.app.audit_report.items(), start=1) if results["successful"] } choices[f"{EMOJIS.get('BACK')} Return to Previous Menu"] = None return user_options_prompt(choices, prompt, clear_screen=False, first_item_is_exit_menu=True)
def launch(self): while True: subprocess.run(["clear"]) if self.heading: print_heading(self.heading, fg=self.heading_color) if self.message: print_message(f"{self.message}\n") result = super().launch() if result == DEFAULT_JOB_NAME: return None if JOB_NAME_REGEX.match(result): return result error = ( f"\n'{result}' contains one or more invalid characters. Job name must contain " "only letters, numbers, hyphen and underscore characters.\n") print_error(error, fg="bright_red") pause(message="Press any key to continue...")
def select_batter_prompt(self, bat_boxscore): max_name_length = self.get_name_max_length(bat_boxscore) choices = { f"{self.select_batter_text(order_num, box, max_name_length)}": box["mlb_id"] for order_num, box in bat_boxscore.items() } choices[f"{EMOJIS.get('BACK')} Return to Previous Menu"] = None prompt = ( "Starting lineup and all substitutes who made at least one plate appearance are " "listed below. Select a player and press ENTER to view details of each at bat:" ) print_heading(f"Scraped Data Viewer for Game ID: {self.bbref_game_id}", fg="bright_yellow") return user_options_prompt(choices, prompt, auto_scroll=False, clear_screen=False)
def select_pitcher_prompt(self, pitch_boxscore): max_name_length = self.get_name_max_length(pitch_boxscore) choices = { f"{self.select_pitcher_text(num, box, max_name_length)}": box["mlb_id"] for num, box in enumerate(pitch_boxscore.values(), start=1) } choices[f"{EMOJIS.get('BACK')} Return to Previous Menu"] = None prompt = ( "All pitchers that apppeared in the game are listed below. Select a player and " "press ENTER to access information for this start and various career stats:" ) print_heading(f"Scraped Data Viewer for Game ID: {self.bbref_game_id}", fg="bright_yellow") return user_options_prompt(choices, prompt, auto_scroll=False, clear_screen=False)
def select_season_game_id(self): while True: result = self.select_season_prompt() if result.failure: return result self.mlb_season = result.value game_ids = self.app.audit_report[self.mlb_season]["successful"] while True: subprocess.run(["clear"]) heading = f"Scraped Data Viewer - Select Game (MLB Season: {self.mlb_season})" print_heading(heading, fg="bright_yellow") result = select_game_prompt(game_ids, use_numbers=False, clear_screen=False) if result.failure: break self.game_id = result.value view_game_data = ViewGameData(self.app, self.game_id) view_game_data.launch()
def select_team_prompt(self): team_choices_dict = { t.team_id_br: t.name for t in db.Team.get_all_teams_for_season(self.db_session, year=self.mlb_season) } subprocess.run(["clear"]) heading = f"Scraped Data Viewer - Select db.Team (MLB Season: {self.mlb_season})" print_heading(heading, fg="bright_yellow") prompt = "Select a team from the list below::" choices = { f"{EMOJIS.get('BLUE_DIAMOND')} {name} ({team_id})": team_id for team_id, name in team_choices_dict.items() } choices[f"{EMOJIS.get('BACK')} Return to Previous Menu"] = None result = user_options_prompt(choices, prompt, clear_screen=False, first_item_is_exit_menu=True) if result.failure: return result self.team_id = result.value return self.get_team_game_ids()
def select_pitcher_data_prompt(self, mlb_id): pitcher_name = self.game_data.get_player_id_map(mlb_id=mlb_id).mlb_name prompt = ( f"Data for {pitcher_name} can be viewed in several different ways, " "please choose an option from the list below:") choices = { f"{MENU_NUMBERS.get(1)} Play-by-Play Data for All At Bats": "AT_BATS", f"{MENU_NUMBERS.get(2)} Batting Stats By Pitch Type": "BAT_STATS", f"{MENU_NUMBERS.get(3)} Pitch Mix Split by Batter Stance": "PITCH_MIX_BY_STANCE", f"{MENU_NUMBERS.get(4)} Pitch Mix Split by Season": "PITCH_MIX_BY_SEASON", f"{MENU_NUMBERS.get(5)} Plate Discipline Metrics Split by Pitch Type": "PLATE_DISCIPLINE", f"{MENU_NUMBERS.get(6)} Batted Ball Metrics Split by Pitch Type": "BATTED_BALL", f"{EMOJIS.get('BACK')} Return to Previous Menu": None, } print_heading(f"Scraped Data Viewer for Game ID: {self.bbref_game_id}", fg="bright_yellow") return user_options_prompt(choices, prompt, clear_screen=False)
def enter_game_id(self): subprocess.run(["clear"]) print_heading("Scraped Data Viewer - Enter a BBRef Game ID", fg="bright_yellow") check_game_id = Input("Enter a BBRef Game ID: ").launch() try: result = validate_bbref_game_id(check_game_id) except ValueError: error = f"\n'{check_game_id}' is NOT a valid BBRef Game ID, please try again." print_error(error) pause(message="Press any key to continue...") return Result.Fail("") self.game_id = result.value["game_id"] game_date = result.value["game_date"] all_valid_game_ids = self.app.audit_report[ game_date.year]["successful"] if self.game_id not in all_valid_game_ids: error = f"\nRequirements to show data for {check_game_id} have not been met" print_error(error) pause(message="Press any key to continue...") return Result.Fail("") view_game_data = ViewGameData(self.app, self.game_id) view_game_data.launch() return Result.Fail("")
def backup_database_start(self, total_tables): subprocess.run(["clear"]) print_heading("Backup Database to CSV", fg="bright_yellow") self.total_tables = total_tables self.spinner.start()
def display(self, heading_color=None, text_color=None): if self.heading: print_heading(self.heading, fg=heading_color) for s in self.text: print_message(s, fg=text_color, wrap=self.wrap)
def update_menu_heading(self, status, heading_color="bright_yellow"): subprocess.run(["clear"]) print_heading(self.get_menu_heading(status), fg=heading_color)
def display_pitch_metrics(self, metrics): heading = f"Avg. Time Between Pitches for {len(self.game_ids)} MLB Games" table = self.construct_stats_table(metrics) print_heading(heading, fg="bright_yellow") print_message(table, wrap=False, fg="bright_cyan")