Converted output to Excel file
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
import tbaapiv3client
|
||||
from tbaapiv3client.rest import ApiException
|
||||
from pprint import pprint
|
||||
from datetime import datetime as dt
|
||||
|
||||
from openpyxl import Workbook, worksheet, load_workbook
|
||||
from openpyxl.worksheet.table import Table, TableStyleInfo
|
||||
|
||||
from csv import DictWriter
|
||||
|
||||
# Setup Config
|
||||
@@ -44,7 +48,6 @@ def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[str]) -> li
|
||||
points_event1 = event.total
|
||||
points_event1_na = event.qual_points + event.elim_points + event.alliance_points
|
||||
|
||||
|
||||
if len(result.event_points) > 1:
|
||||
event = result.event_points[1]
|
||||
points_event1 = event.total
|
||||
@@ -61,6 +64,7 @@ def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[str]) -> li
|
||||
|
||||
return rankings
|
||||
|
||||
|
||||
def get_event_teams(api_client: tbaapiv3client.ApiClient, season: str) -> dict[dict]:
|
||||
|
||||
api_district = tbaapiv3client.DistrictApi(api_client)
|
||||
@@ -82,7 +86,8 @@ def get_event_teams(api_client: tbaapiv3client.ApiClient, season:str) -> dict[di
|
||||
team_results = api_event.get_event_teams_keys(key)
|
||||
|
||||
except ApiException as e:
|
||||
print("Exception when calling EventApi->get_event_teams_keys: %s\n" % e)
|
||||
print(
|
||||
"Exception when calling EventApi->get_event_teams_keys: %s\n" % e)
|
||||
else:
|
||||
event_teams[key] = {
|
||||
'key': key,
|
||||
@@ -94,6 +99,7 @@ def get_event_teams(api_client: tbaapiv3client.ApiClient, season:str) -> dict[di
|
||||
|
||||
return event_teams
|
||||
|
||||
|
||||
def write_power_rank(path: str | Path, rankings: list[dict], seasons: list[str]) -> None:
|
||||
# Create annual rank CSV
|
||||
results_list: list[dict] = []
|
||||
@@ -148,7 +154,6 @@ def write_power_rank(path: str | Path, rankings: list[dict], seasons:list[str])
|
||||
result[f'{season} Points District'] = point_district
|
||||
result[f'{season} Points District No Awards'] = point_district_na
|
||||
|
||||
|
||||
rank_avg = ''
|
||||
point_total_avg = ''
|
||||
point_district_avg = ''
|
||||
@@ -165,10 +170,10 @@ def write_power_rank(path: str | Path, rankings: list[dict], seasons:list[str])
|
||||
result[f'Avg Points District'] = point_district_avg
|
||||
result[f'Avg Points District No Awards'] = point_district_na_avg
|
||||
|
||||
|
||||
results_list.append(result)
|
||||
|
||||
write_result(path, results_list)
|
||||
write_result(path, results_list,"Power Ranking")
|
||||
|
||||
|
||||
def write_rank(path: str | Path, rankings: list[dict], seasons: list[str]) -> None:
|
||||
# Create annual rank CSV
|
||||
@@ -190,6 +195,7 @@ def write_rank(path: str | Path, rankings: list[dict], seasons:list[str]) -> Non
|
||||
|
||||
write_result(path, results_list)
|
||||
|
||||
|
||||
def write_point_total(path: str | Path, rankings: list[dict], seasons: list[str]) -> None:
|
||||
# Create annual rank CSV
|
||||
results_list: list[dict] = []
|
||||
@@ -210,6 +216,7 @@ def write_point_total(path: str | Path, rankings: list[dict], seasons:list[str])
|
||||
|
||||
write_result(path, results_list)
|
||||
|
||||
|
||||
def write_points_events(path: str | Path, rankings: list[dict], seasons: list[str]) -> None:
|
||||
# Create annual rank CSV
|
||||
results_list: list[dict] = []
|
||||
@@ -236,16 +243,50 @@ def write_points_events(path: str | Path, rankings: list[dict], seasons:list[str
|
||||
|
||||
write_result(path, results_list)
|
||||
|
||||
def write_result(path: str| Path, results_list:list[dict]) -> 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)
|
||||
|
||||
with open(path, 'w', newline='') as f:
|
||||
writer = DictWriter(f, results_list[0].keys())
|
||||
# Get headers
|
||||
if headers is None:
|
||||
headers = list(results_list[0].keys())
|
||||
|
||||
writer.writeheader()
|
||||
writer.writerows(results_list)
|
||||
# Open workbook
|
||||
wb:Workbook = None
|
||||
ws = None
|
||||
if path.exists():
|
||||
wb = load_workbook(path)
|
||||
ws = wb.create_sheet(sheet_name,sheet_index)
|
||||
else:
|
||||
wb = Workbook()
|
||||
ws = wb.active
|
||||
|
||||
if sheet_name is not None:
|
||||
ws.title = sheet_name
|
||||
|
||||
# Write Header
|
||||
ws.append(headers)
|
||||
|
||||
# Write Results
|
||||
|
||||
for row in results_list:
|
||||
values = []
|
||||
|
||||
for header in headers:
|
||||
if header in row:
|
||||
values.append(row[header])
|
||||
else:
|
||||
values.append(None)
|
||||
|
||||
ws.append(values)
|
||||
|
||||
# Enable Filter
|
||||
ws.auto_filter.ref = ws.dimensions
|
||||
|
||||
# Save Workbook
|
||||
wb.save(path)
|
||||
|
||||
def main():
|
||||
|
||||
@@ -255,7 +296,6 @@ def main():
|
||||
seasons = ['2022fim', '2023fim', '2024fim']
|
||||
root_dir = Path('results')
|
||||
|
||||
|
||||
with tbaapiv3client.ApiClient(config) as api_client:
|
||||
rankings = get_rankings(api_client, seasons)
|
||||
event_teams = get_event_teams(api_client, '2025fim')
|
||||
@@ -323,14 +363,8 @@ def main():
|
||||
|
||||
event_details.append(team_details)
|
||||
|
||||
|
||||
if len(event_details):
|
||||
# Write Event Details
|
||||
with open(root_dir / f'{run_time_str} - {key} details.csv', 'w', newline='') as f:
|
||||
writer = DictWriter(f, event_details[0])
|
||||
|
||||
writer.writeheader()
|
||||
writer.writerows(event_details)
|
||||
write_result(root_dir / f'{run_time_str} - Event Details.xlsx', event_details, key)
|
||||
|
||||
# Generate Event Summary
|
||||
rank_avg = ''
|
||||
@@ -357,15 +391,12 @@ def main():
|
||||
'team count': len(event['teams'])
|
||||
})
|
||||
|
||||
|
||||
# Write Event Summary
|
||||
with open(root_dir / f'{run_time_str} - event summary.csv', 'w', newline='') as f:
|
||||
writer = DictWriter(f, event_summary[0].keys())
|
||||
write_result(root_dir / f'{run_time_str} - Event Details.xlsx', event_summary, "Summary", 0)
|
||||
|
||||
writer.writeheader()
|
||||
writer.writerows(event_summary)
|
||||
|
||||
write_power_rank(root_dir / f"{run_time_str} - season_power_rank.csv", rankings, seasons)
|
||||
# Write Power Rankings
|
||||
write_power_rank(
|
||||
root_dir / f"Power Ranking.xlsx", rankings, seasons)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user