diff --git a/main.py b/main.py new file mode 100644 index 0000000..b11caa6 --- /dev/null +++ b/main.py @@ -0,0 +1,84 @@ +#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 TeamData(): + #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: + #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']] = 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']) + +#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 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