From c125c4c81bbadd6606e6f8f5614cd51fedcdb446 Mon Sep 17 00:00:00 2001 From: 81123 Dolev Dror Date: Wed, 15 Oct 2025 19:56:35 -0700 Subject: [PATCH 1/2] ahsahhaha --- main.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..7704d87 --- /dev/null +++ b/main.py @@ -0,0 +1,63 @@ +#If you are reading this you know what this does (Alision forced me to write comments) +import json + +#Load tim data json file +with open("example_tim_data.json", 'r') as tim_json: + tim_data = json.load(tim_json) + +#Class for team data +class team_data: + #Initilizes team number and other variables + def __init__(self, team_number) -> None: + self.team_number = team_number + self.match_numbers = [] + self.times_climbed = 0 + self.balls_per_match = [] + + #Adds data for a tim to the total team data + def add_data(self, match_num, climbed, num_balls) -> None: + self.match_numbers.append(match_num) + self.times_climbed += (1 * climbed) + self.balls_per_match.append(num_balls) + + #Calculates output team data + def calculate_team_data(self) -> dict: + number_matches_played = len(self.match_numbers) + average_balls_scored = sum(self.balls_per_match)/number_matches_played + least_balls_scored = min(self.balls_per_match) + most_balls_scored = max(self.balls_per_match) + percent_climb_success = self.times_climbed/number_matches_played + + #Creates and returns dict with output team data + data = { + 'team_number': self.team_number, + 'average_balls_scored': average_balls_scored, + 'least_balls_scored': least_balls_scored, + 'most_balls_scored': most_balls_scored, + 'number_matches_played': number_matches_played, + 'percent_climb_success': percent_climb_success + } + + return data + +#Initilizes variable to hold all teams data +loaded_teams = {} + +#Loops through tim data +for tim in tim_data: + #Checks if team is already in team data. If not it makes a new team_data object to hold it + if tim['team_num'] not in loaded_teams.keys(): + loaded_teams[tim['team_num']] = team_data(tim['team_num']) + + #Adds tim data to team data + loaded_teams[tim['team_num']].add_data(tim['match_num'], tim['climbed'], tim['num_balls']) + +#Initilizes variable for calculated team data +calculated_teams_data = [] + +#Calculates team data and adds it all to culculated_teams_data +for team in loaded_teams.values(): + calculated_teams_data.append(team.calculate_team_data()) + +#No MongoDB so just prints out the data +print(calculated_teams_data) \ No newline at end of file From 7b6337ab96df1421d49f1d074584c6637e574dd2 Mon Sep 17 00:00:00 2001 From: 81123 Dolev Dror Date: Thu, 16 Oct 2025 19:46:13 -0700 Subject: [PATCH 2/2] Add schema --- main.py | 29 +++++++++++++++++++++++++---- tim_data_schema.yaml | 4 ++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 tim_data_schema.yaml diff --git a/main.py b/main.py index 7704d87..b11caa6 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,27 @@ #If you are reading this you know what this does (Alision forced me to write comments) import json +import yaml #Load tim data json file with open("example_tim_data.json", 'r') as tim_json: tim_data = json.load(tim_json) +#Load schema yaml file +with open("tim_data_schema.yaml", "r") as tim_schema_yaml: + tim_schema = yaml.load(tim_schema_yaml, yaml.Loader) + +class_types = { + 'int': int, + 'float': float, + 'str': str, + 'bool': bool, + 'set': set, + 'list': list, + 'dict': dict +} + #Class for team data -class team_data: +class TeamData(): #Initilizes team number and other variables def __init__(self, team_number) -> None: self.team_number = team_number @@ -23,10 +38,10 @@ def add_data(self, match_num, climbed, num_balls) -> None: #Calculates output team data def calculate_team_data(self) -> dict: number_matches_played = len(self.match_numbers) - average_balls_scored = sum(self.balls_per_match)/number_matches_played + average_balls_scored = sum(self.balls_per_match) / number_matches_played least_balls_scored = min(self.balls_per_match) most_balls_scored = max(self.balls_per_match) - percent_climb_success = self.times_climbed/number_matches_played + percent_climb_success = self.times_climbed / number_matches_played #Creates and returns dict with output team data data = { @@ -45,9 +60,15 @@ def calculate_team_data(self) -> dict: #Loops through tim data for tim in tim_data: + #Validate tim + for data in tim: + if type(tim[data]) != class_types[tim_schema[data]]: + print("Tim data is invalid") + exit() + #Checks if team is already in team data. If not it makes a new team_data object to hold it if tim['team_num'] not in loaded_teams.keys(): - loaded_teams[tim['team_num']] = team_data(tim['team_num']) + loaded_teams[tim['team_num']] = TeamData(tim['team_num']) #Adds tim data to team data loaded_teams[tim['team_num']].add_data(tim['match_num'], tim['climbed'], tim['num_balls']) diff --git a/tim_data_schema.yaml b/tim_data_schema.yaml new file mode 100644 index 0000000..378eacf --- /dev/null +++ b/tim_data_schema.yaml @@ -0,0 +1,4 @@ +team_num: int +match_num: int +climbed: bool +num_balls: int \ No newline at end of file