Advertisement
UniQuet0p1

Untitled

Oct 26th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. """Formula One."""
  2. import re
  3. import csv
  4.  
  5.  
  6. class Driver:
  7. """Driver class."""
  8.  
  9. def __init__(self, name: str, team: str):
  10. """
  11. Driver constructor.
  12.  
  13. Here you should save driver's results as dictionary,
  14. where key is race number and value is points from that race.
  15. You must also save driver's points into a variable "points".
  16.  
  17. :param name: Driver name
  18. :param team: Driver team
  19. """
  20. self.name = name
  21. self.team = team
  22. self.result = []
  23.  
  24. def get_results(self) -> dict:
  25. """
  26. Get all driver's results.
  27.  
  28. :return: Results as dictionary
  29. """
  30. return self.result
  31.  
  32. def get_points(self) -> int:
  33. """
  34. Return calculated driver points.
  35.  
  36. :return: Calculated points
  37. """
  38. return 0
  39.  
  40. def set_points(self):
  41. """Set points for driver."""
  42. pass
  43.  
  44. def add_result(self, race_number: int, points: int):
  45. """
  46. Add new result to dictionary of results.
  47.  
  48. Dictionary is located in the constructor.
  49.  
  50. :param race_number: Race number
  51. :param points: Number of points from the race
  52. """
  53. pass
  54.  
  55.  
  56. class Race:
  57. """Race class."""
  58.  
  59. def __init__(self, file):
  60. """
  61. Race constructor.
  62.  
  63. Here you should keep data collected from file.
  64. You must read file rows to list.
  65.  
  66. :param file: File with race data
  67. """
  68. self.file = file
  69.  
  70. def read_file_to_list(self):
  71. """
  72. Read file data to list in constructor.
  73.  
  74. First line shows number of races in data file.
  75. Rest of the data follows same rules. Each line consists of 'Driver Team Time Race'.
  76. There are 2 or more spaces between each 'category'.
  77. E.g. "Mika Häkkinen McLaren-Mercedes 42069 3"
  78.  
  79. If file does NOT exist, throw FileNotFoundError with message "No file found!".
  80. """
  81. list1 = []
  82. list2 = []
  83. list3 = []
  84. i = 0
  85. try:
  86. f = open(self.file, "r")
  87. for row in f:
  88. if i > 0:
  89. list1.append(self.extract_info(row))
  90. i += 1
  91. else:
  92. i += 1
  93. pass
  94. f.close()
  95. return list1
  96. except FileNotFoundError:
  97. raise FileNotFoundError("No file found!")
  98.  
  99. @staticmethod
  100. def extract_info(line: str) -> dict:
  101. """
  102. Helper method for read_file_to_list.
  103.  
  104. Here you should convert one data line to dictionary.
  105. Dictionary must contain following key-value pairs:
  106. 'Name': driver's name as string
  107. 'Team': driver's team as string
  108. 'Time': driver's time as integer (time is always in milliseconds)
  109. 'Diff': empty string
  110. 'Race': race number as integer
  111.  
  112. :param line: Data string
  113. :return: Converted dictionary
  114. """
  115. list1 = []
  116. splitted = re.split("[ ]{2,}", line)
  117. for each in splitted:
  118. stripped = each.strip("\n")
  119. list1.append(stripped)
  120. d = {"Name": list1[0], "Team": list1[1], "Time": list1[2], "Diff": "",
  121. "Race": list1[3]}
  122. return d
  123.  
  124. def filter_data_by_race(self, race_number: int) -> list:
  125. """
  126. Filter data by race number.
  127.  
  128. :param race_number: Race number
  129. :return: Filtered race data
  130. """
  131.  
  132. return []
  133.  
  134. @staticmethod
  135. def format_time(time: str) -> str:
  136. """
  137. Format time from milliseconds to M:SS.SSS
  138.  
  139. format_time('12') -> 0:00.012
  140. format_time('1234') -> 0:01.234
  141. format_time('123456') -> 2:03.456
  142.  
  143. :param time: Time in milliseconds
  144. :return: Time as M:SS.SSS string
  145. """
  146. time = int(time)
  147. minutes = time // 1000 // 60
  148. seconds = time // 1000 % 60
  149. mseconds = time % 1000
  150. return f'{minutes}:{seconds:02d}.{mseconds:03d}'
  151.  
  152. @staticmethod
  153. def calculate_time_difference(first_time: int, second_time: int) -> str:
  154. """
  155. Calculate difference between two times.
  156.  
  157. First time is always smaller than second time. Both times are in milliseconds.
  158. You have to return difference in format +M:SS.SSS
  159.  
  160. calculate_time_difference(4201, 57411) -> +0:53.210
  161.  
  162. :param first_time: First time in milliseconds
  163. :param second_time: Second time in milliseconds
  164. :return: Time difference as +M:SS.SSS string
  165. """
  166.  
  167. result = second_time - first_time
  168. time = int(result)
  169. minutes = time // 1000 // 60
  170. seconds = time // 1000 % 60
  171. mseconds = time % 1000
  172. return f'+{minutes}:{seconds:02d}.{mseconds:03d}'
  173.  
  174. @staticmethod
  175. def sort_data_by_time(results: list) -> list:
  176. """
  177. Sort results data list of dictionaries by 'Time'.
  178.  
  179. :param results: List of dictionaries
  180. :return: Sorted list of dictionaries
  181. """
  182. return []
  183.  
  184. def get_results_by_race(self, race_number: int) -> list:
  185. """
  186. Final results by race number.
  187.  
  188. This method combines the rest of the methods.
  189. You have to filter data by race number and sort them by time.
  190. You must also fill 'Diff' as time difference with first position.
  191. You must add 'Place' and 'Points' key-value pairs for each dictionary.
  192.  
  193. :param race_number: Race number for filtering
  194. :return: Final dictionary with complete data
  195. """
  196. return []
  197.  
  198.  
  199. class FormulaOne:
  200. """FormulaOne class."""
  201.  
  202. def __init__(self, file):
  203. """
  204. FormulaOne constructor.
  205.  
  206. It is reasonable to create Race instance here to collect all data from file.
  207.  
  208. :param file: File with race data
  209. """
  210. self.file = file
  211.  
  212. def write_race_results_to_file(self, race_number: int):
  213. """
  214. Write one race results to a file.
  215.  
  216. File name is 'results_for_race_{race_number}.txt'.
  217. Exact specifications are described in the text.
  218.  
  219. :param race_number: Race to write to file
  220. """
  221. pass
  222.  
  223. def write_race_results_to_csv(self, race_number: int):
  224. """
  225. Write one race results to a csv file.
  226.  
  227. File name is 'race_{race_number}_results.csv'.
  228. Exact specifications are described in the text.
  229.  
  230. :param race_number: Race to write to file
  231. """
  232. pass
  233.  
  234. def write_championship_to_file(self):
  235. """
  236. Write championship results to file.
  237.  
  238. It is reasonable to create Driver class instance for each unique driver name and collect their points
  239. using methods from Driver class.
  240. Exact specifications are described in the text.
  241. """
  242. pass
  243.  
  244.  
  245. if __name__ == '__main__':
  246. f1 = FormulaOne("ex08_example_data.csv")
  247. f1.write_race_results_to_file(1)
  248. f1.write_race_results_to_csv(2)
  249. f1.write_championship_to_file()
  250. race = Race("ex08_example_data.csv")
  251. list_of_results = race.read_file_to_list()
  252. print(list_of_results)
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement