From ac02e0b65cd3aebd93ed3edb18fd2a188281b135 Mon Sep 17 00:00:00 2001 From: Ryan Fitz-Gerald Date: Tue, 15 Oct 2024 12:19:39 -0400 Subject: [PATCH] Fixed overwriting worksheet if name already exists Used power ranking to generate event worksheets --- get_rankings.py | 100 ++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/get_rankings.py b/get_rankings.py index da43824..d6f8440 100644 --- a/get_rankings.py +++ b/get_rankings.py @@ -20,7 +20,7 @@ config = tbaapiv3client.Configuration( config.verify_ssl = False -def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[str]) -> list[dict]: +def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[str]) -> dict[dict]: # Create an instance of the API class api_instance = tbaapiv3client.DistrictApi(api_client) @@ -50,8 +50,8 @@ def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[str]) -> li if len(result.event_points) > 1: event = result.event_points[1] - points_event1 = event.total - points_event1_na = event.qual_points + event.elim_points + event.alliance_points + points_event2 = event.total + points_event2_na = event.qual_points + event.elim_points + event.alliance_points rankings[team][season] = { "rank": result.rank, @@ -100,7 +100,7 @@ def get_event_teams(api_client: tbaapiv3client.ApiClient, season: str) -> dict[d return event_teams -def write_power_rank(path: str | Path, rankings: list[dict], seasons: list[str]) -> None: +def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], seasons: list[str]) -> None: # Create annual rank CSV results_list: list[dict] = [] @@ -172,7 +172,17 @@ def write_power_rank(path: str | Path, rankings: list[dict], seasons: list[str]) results_list.append(result) - write_result(path, results_list,"Power Ranking") + headers = ['team'] + prefix_list = seasons.copy() + prefix_list.append('Avg') + suffix_list = ['Rank', 'Point Total', + 'Points District', 'Points District No Awards'] + + for suffix in suffix_list: + for prefix in prefix_list: + headers.append(f'{prefix} {suffix}') + + write_result(path, results_list, sheet_name, headers=headers) def write_rank(path: str | Path, rankings: list[dict], seasons: list[str]) -> None: @@ -244,7 +254,7 @@ def write_points_events(path: str | Path, rankings: list[dict], seasons: list[st write_result(path, results_list) -def write_result(path: str | Path, results_list: list[dict], sheet_name:Optional[str] = None, sheet_index:Optional[int]=None, headers:Optional[list[str]]=None) -> None: +def write_result(path: str | Path, results_list: list[dict], sheet_name: Optional[str] = None, sheet_index: Optional[int] = None, headers: Optional[list[str]] = None) -> None: # Convert path to Path type if it isn't already if not isinstance(path, Path): path = Path(path) @@ -254,15 +264,19 @@ def write_result(path: str | Path, results_list: list[dict], sheet_name:Optional headers = list(results_list[0].keys()) # Open workbook - wb:Workbook = None + wb: Workbook = None ws = None if path.exists(): wb = load_workbook(path) - ws = wb.create_sheet(sheet_name,sheet_index) + + if sheet_name is None or sheet_name in wb.sheetnames: + wb.remove(wb[sheet_name]) + + ws = wb.create_sheet(sheet_name, sheet_index) else: wb = Workbook() ws = wb.active - + if sheet_name is not None: ws.title = sheet_name @@ -270,7 +284,7 @@ def write_result(path: str | Path, results_list: list[dict], sheet_name:Optional ws.append(headers) # Write Results - + for row in results_list: values = [] @@ -288,14 +302,21 @@ def write_result(path: str | Path, results_list: list[dict], sheet_name:Optional # Save Workbook wb.save(path) + def main(): run_time = dt.now() run_time_str = run_time.strftime('%Y%m%d_%H%M') - seasons = ['2022fim', '2023fim', '2024fim'] + skip_years = [2020,2021] + + seasons = [f'{year}fim' for year in range(2022, 2025) if year not in skip_years] + root_dir = Path('results') + + event_details_path = root_dir / f'{run_time_str} - Event Details.xlsx' + with tbaapiv3client.ApiClient(config) as api_client: rankings = get_rankings(api_client, seasons) event_teams = get_event_teams(api_client, '2025fim') @@ -310,62 +331,39 @@ def main(): entry_count = 0 - event_details: list[dict] = [] + event_rankings: dict[dict] = {} for team in event['teams']: - team_details = { - 'team': team - } + if team in rankings: + event_rankings[team] = rankings[team] + else: + event_rankings[team] = {} + if team in rankings: for season in seasons: - rank = '' - point_total = '' - point_district = '' - point_district_na = '' - if season in rankings[team]: team_season = rankings[team][season] - rank = team_season['rank'] - point_total = team_season['point_total'] - point_district = 0 - point_district_na = 0 + rank_sum += team_season['rank'] + point_total_sum += team_season['point_total'] if team_season['points_event1'] is not None: - point_district += team_season['points_event1'] + point_district_sum += team_season['points_event1'] if team_season['points_event2'] is not None: - point_district += team_season['points_event2'] + point_district_sum += team_season['points_event2'] if team_season['points_event1_na'] is not None: - point_district_na += team_season['points_event1_na'] + point_district_na_sum += team_season['points_event1_na'] if team_season['points_event2_na'] is not None: - point_district_na += team_season['points_event2_na'] + point_district_na_sum += team_season['points_event2_na'] - rank_sum += rank - point_total_sum += point_total - point_district_sum += point_district - point_district_na_sum += point_district_na entry_count += 1 - team_details[f'{season} rank'] = rank - team_details[f'{season} point total'] = point_total - team_details[f'{season} point district'] = point_district - team_details[f'{season} point district no awards'] = point_district - else: - for season in seasons: - team_details[f'{season} rank'] = '' - team_details[f'{season} point total'] = '' - team_details[f'{season} point district'] = '' - team_details[f'{season} point district no awards'] = '' - - event_details.append(team_details) - - if len(event_details): - write_result(root_dir / f'{run_time_str} - Event Details.xlsx', event_details, key) - + write_power_rank(event_details_path, event['key'], event_rankings, seasons) + # Generate Event Summary rank_avg = '' point_total_avg = '' @@ -392,11 +390,13 @@ def main(): }) # Write Event Summary - write_result(root_dir / f'{run_time_str} - Event Details.xlsx', event_summary, "Summary", 0) + print('Writing Event Summary') + write_result(event_details_path, event_summary, "Summary", 0) # Write Power Rankings + print('Writing Power Ranking') write_power_rank( - root_dir / f"Power Ranking.xlsx", rankings, seasons) + root_dir / f"Power Ranking.xlsx", 'Power Ranking', rankings, seasons) if __name__ == '__main__':