Fixed overwriting worksheet if name already exists
Used power ranking to generate event worksheets
This commit is contained in:
@@ -20,7 +20,7 @@ config = tbaapiv3client.Configuration(
|
|||||||
config.verify_ssl = False
|
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
|
# Create an instance of the API class
|
||||||
api_instance = tbaapiv3client.DistrictApi(api_client)
|
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:
|
if len(result.event_points) > 1:
|
||||||
event = result.event_points[1]
|
event = result.event_points[1]
|
||||||
points_event1 = event.total
|
points_event2 = event.total
|
||||||
points_event1_na = event.qual_points + event.elim_points + event.alliance_points
|
points_event2_na = event.qual_points + event.elim_points + event.alliance_points
|
||||||
|
|
||||||
rankings[team][season] = {
|
rankings[team][season] = {
|
||||||
"rank": result.rank,
|
"rank": result.rank,
|
||||||
@@ -100,7 +100,7 @@ def get_event_teams(api_client: tbaapiv3client.ApiClient, season: str) -> dict[d
|
|||||||
return event_teams
|
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
|
# Create annual rank CSV
|
||||||
results_list: list[dict] = []
|
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)
|
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:
|
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)
|
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
|
# Convert path to Path type if it isn't already
|
||||||
if not isinstance(path, Path):
|
if not isinstance(path, Path):
|
||||||
path = Path(path)
|
path = Path(path)
|
||||||
@@ -254,11 +264,15 @@ def write_result(path: str | Path, results_list: list[dict], sheet_name:Optional
|
|||||||
headers = list(results_list[0].keys())
|
headers = list(results_list[0].keys())
|
||||||
|
|
||||||
# Open workbook
|
# Open workbook
|
||||||
wb:Workbook = None
|
wb: Workbook = None
|
||||||
ws = None
|
ws = None
|
||||||
if path.exists():
|
if path.exists():
|
||||||
wb = load_workbook(path)
|
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:
|
else:
|
||||||
wb = Workbook()
|
wb = Workbook()
|
||||||
ws = wb.active
|
ws = wb.active
|
||||||
@@ -288,14 +302,21 @@ def write_result(path: str | Path, results_list: list[dict], sheet_name:Optional
|
|||||||
# Save Workbook
|
# Save Workbook
|
||||||
wb.save(path)
|
wb.save(path)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
run_time = dt.now()
|
run_time = dt.now()
|
||||||
run_time_str = run_time.strftime('%Y%m%d_%H%M')
|
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')
|
root_dir = Path('results')
|
||||||
|
|
||||||
|
|
||||||
|
event_details_path = root_dir / f'{run_time_str} - Event Details.xlsx'
|
||||||
|
|
||||||
with tbaapiv3client.ApiClient(config) as api_client:
|
with tbaapiv3client.ApiClient(config) as api_client:
|
||||||
rankings = get_rankings(api_client, seasons)
|
rankings = get_rankings(api_client, seasons)
|
||||||
event_teams = get_event_teams(api_client, '2025fim')
|
event_teams = get_event_teams(api_client, '2025fim')
|
||||||
@@ -310,61 +331,38 @@ def main():
|
|||||||
|
|
||||||
entry_count = 0
|
entry_count = 0
|
||||||
|
|
||||||
event_details: list[dict] = []
|
event_rankings: dict[dict] = {}
|
||||||
|
|
||||||
for team in event['teams']:
|
for team in event['teams']:
|
||||||
team_details = {
|
if team in rankings:
|
||||||
'team': team
|
event_rankings[team] = rankings[team]
|
||||||
}
|
else:
|
||||||
|
event_rankings[team] = {}
|
||||||
|
|
||||||
|
|
||||||
if team in rankings:
|
if team in rankings:
|
||||||
for season in seasons:
|
for season in seasons:
|
||||||
rank = ''
|
|
||||||
point_total = ''
|
|
||||||
point_district = ''
|
|
||||||
point_district_na = ''
|
|
||||||
|
|
||||||
if season in rankings[team]:
|
if season in rankings[team]:
|
||||||
team_season = rankings[team][season]
|
team_season = rankings[team][season]
|
||||||
|
|
||||||
rank = team_season['rank']
|
rank_sum += team_season['rank']
|
||||||
point_total = team_season['point_total']
|
point_total_sum += team_season['point_total']
|
||||||
point_district = 0
|
|
||||||
point_district_na = 0
|
|
||||||
|
|
||||||
if team_season['points_event1'] is not None:
|
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:
|
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:
|
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:
|
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
|
entry_count += 1
|
||||||
|
|
||||||
team_details[f'{season} rank'] = rank
|
write_power_rank(event_details_path, event['key'], event_rankings, seasons)
|
||||||
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)
|
|
||||||
|
|
||||||
# Generate Event Summary
|
# Generate Event Summary
|
||||||
rank_avg = ''
|
rank_avg = ''
|
||||||
@@ -392,11 +390,13 @@ def main():
|
|||||||
})
|
})
|
||||||
|
|
||||||
# Write Event Summary
|
# 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
|
# Write Power Rankings
|
||||||
|
print('Writing Power Ranking')
|
||||||
write_power_rank(
|
write_power_rank(
|
||||||
root_dir / f"Power Ranking.xlsx", rankings, seasons)
|
root_dir / f"Power Ranking.xlsx", 'Power Ranking', rankings, seasons)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user