Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Formula One."""
- import re
- import csv
- class Driver:
- """Driver class."""
- def __init__(self, name: str, team: str):
- """
- Driver constructor.
- Here you should save driver's results as dictionary,
- where key is race number and value is points from that race.
- You must also save driver's points into a variable "points".
- :param name: Driver name
- :param team: Driver team
- """
- self.name = name
- self.team = team
- self.result = []
- def get_results(self) -> dict:
- """
- Get all driver's results.
- :return: Results as dictionary
- """
- return self.result
- def get_points(self) -> int:
- """
- Return calculated driver points.
- :return: Calculated points
- """
- return 0
- def set_points(self):
- """Set points for driver."""
- pass
- def add_result(self, race_number: int, points: int):
- """
- Add new result to dictionary of results.
- Dictionary is located in the constructor.
- :param race_number: Race number
- :param points: Number of points from the race
- """
- pass
- class Race:
- """Race class."""
- def __init__(self, file):
- """
- Race constructor.
- Here you should keep data collected from file.
- You must read file rows to list.
- :param file: File with race data
- """
- self.file = file
- def read_file_to_list(self):
- """
- Read file data to list in constructor.
- First line shows number of races in data file.
- Rest of the data follows same rules. Each line consists of 'Driver Team Time Race'.
- There are 2 or more spaces between each 'category'.
- E.g. "Mika Häkkinen McLaren-Mercedes 42069 3"
- If file does NOT exist, throw FileNotFoundError with message "No file found!".
- """
- list1 = []
- list2 = []
- list3 = []
- i = 0
- try:
- f = open(self.file, "r")
- for row in f:
- if i > 0:
- list1.append(self.extract_info(row))
- i += 1
- else:
- i += 1
- pass
- f.close()
- return list1
- except FileNotFoundError:
- raise FileNotFoundError("No file found!")
- @staticmethod
- def extract_info(line: str) -> dict:
- """
- Helper method for read_file_to_list.
- Here you should convert one data line to dictionary.
- Dictionary must contain following key-value pairs:
- 'Name': driver's name as string
- 'Team': driver's team as string
- 'Time': driver's time as integer (time is always in milliseconds)
- 'Diff': empty string
- 'Race': race number as integer
- :param line: Data string
- :return: Converted dictionary
- """
- list1 = []
- splitted = re.split("[ ]{2,}", line)
- for each in splitted:
- stripped = each.strip("\n")
- list1.append(stripped)
- d = {"Name": list1[0], "Team": list1[1], "Time": list1[2], "Diff": "",
- "Race": list1[3]}
- return d
- def filter_data_by_race(self, race_number: int) -> list:
- """
- Filter data by race number.
- :param race_number: Race number
- :return: Filtered race data
- """
- return []
- @staticmethod
- def format_time(time: str) -> str:
- """
- Format time from milliseconds to M:SS.SSS
- format_time('12') -> 0:00.012
- format_time('1234') -> 0:01.234
- format_time('123456') -> 2:03.456
- :param time: Time in milliseconds
- :return: Time as M:SS.SSS string
- """
- time = int(time)
- minutes = time // 1000 // 60
- seconds = time // 1000 % 60
- mseconds = time % 1000
- return f'{minutes}:{seconds:02d}.{mseconds:03d}'
- @staticmethod
- def calculate_time_difference(first_time: int, second_time: int) -> str:
- """
- Calculate difference between two times.
- First time is always smaller than second time. Both times are in milliseconds.
- You have to return difference in format +M:SS.SSS
- calculate_time_difference(4201, 57411) -> +0:53.210
- :param first_time: First time in milliseconds
- :param second_time: Second time in milliseconds
- :return: Time difference as +M:SS.SSS string
- """
- result = second_time - first_time
- time = int(result)
- minutes = time // 1000 // 60
- seconds = time // 1000 % 60
- mseconds = time % 1000
- return f'+{minutes}:{seconds:02d}.{mseconds:03d}'
- @staticmethod
- def sort_data_by_time(results: list) -> list:
- """
- Sort results data list of dictionaries by 'Time'.
- :param results: List of dictionaries
- :return: Sorted list of dictionaries
- """
- return []
- def get_results_by_race(self, race_number: int) -> list:
- """
- Final results by race number.
- This method combines the rest of the methods.
- You have to filter data by race number and sort them by time.
- You must also fill 'Diff' as time difference with first position.
- You must add 'Place' and 'Points' key-value pairs for each dictionary.
- :param race_number: Race number for filtering
- :return: Final dictionary with complete data
- """
- return []
- class FormulaOne:
- """FormulaOne class."""
- def __init__(self, file):
- """
- FormulaOne constructor.
- It is reasonable to create Race instance here to collect all data from file.
- :param file: File with race data
- """
- self.file = file
- def write_race_results_to_file(self, race_number: int):
- """
- Write one race results to a file.
- File name is 'results_for_race_{race_number}.txt'.
- Exact specifications are described in the text.
- :param race_number: Race to write to file
- """
- pass
- def write_race_results_to_csv(self, race_number: int):
- """
- Write one race results to a csv file.
- File name is 'race_{race_number}_results.csv'.
- Exact specifications are described in the text.
- :param race_number: Race to write to file
- """
- pass
- def write_championship_to_file(self):
- """
- Write championship results to file.
- It is reasonable to create Driver class instance for each unique driver name and collect their points
- using methods from Driver class.
- Exact specifications are described in the text.
- """
- pass
- if __name__ == '__main__':
- f1 = FormulaOne("ex08_example_data.csv")
- f1.write_race_results_to_file(1)
- f1.write_race_results_to_csv(2)
- f1.write_championship_to_file()
- race = Race("ex08_example_data.csv")
- list_of_results = race.read_file_to_list()
- print(list_of_results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement