def create_report(self, pilots: [Pilot]): if not pilots: return Left( [Message(category=MessageCategory.WARNING, target='timer_after_winner', key='without_pilots')]) report = [] winner_pilots = list(filter(lambda pilot: pilot.finished, pilots)) winner_pilots.sort(key=lambda pilot: pilot.race_duration) if winner_pilots: winner = winner_pilots[0] for index, pilot in enumerate(winner_pilots): summary = dict() summary['position'] = index + 1 summary['pilot_number'] = pilot.number summary['name'] = pilot.name summary['total_laps'] = pilot.total_laps summary['total_time'] = pilot.race_duration if index + 1 > 1: summary['time_after_winner'] = pilot.race_duration - winner.race_duration report.append(summary) return Right(report) else: return Left([Message(category=MessageCategory.WARNING, target='timer_after_winner', key='without_winner_pilots')])
def process_log(self) -> Either: violations = [] grouped_data = defaultdict(list) with open(SPEED_RACER_FILE, 'r', encoding='utf-8') as file: lines = file.readlines() if len(lines) <= 1: return Left([ Message(MessageCategory.VALIDATION, target='log-file', key='invalid_log') ]) line_pattern_extractor = re.compile(self._make_regex()) for index, line in enumerate(lines[1:]): data = re.search(line_pattern_extractor, line) result = self.validator.validate(data.groups(), index) if result.is_right: tuple_data = data.groups() tuple_data[1].strip().split('–')[0].strip() key = self._extract_pilot_number_from_tuple(tuple_data) if key: grouped_data[key].append(tuple_data) else: violations += [violation for violation in result.value] if violations: return Left(violations) return Right(self.mapper.to_models(grouped_data))
def process_report(self, name: str) -> Either: try: report_to_process = self.reports[name] result = LogParser().process_log().value return report_to_process.create_report(result) except KeyError: valid_report_names = list(map(lambda report_name: str(report_name), self.reports.keys())) return Left([Message(category=MessageCategory.VALIDATION, key='invalid_report_name', args=(name, valid_report_names))])
def create_report(self, pilots: [Pilot]) -> Either: report = list() if pilots: for pilot in pilots: summary = dict() best_lap = max(pilot.laps, key=lambda lap: lap.lap_speed) summary['name'] = pilot.name summary['lap'] = best_lap.number summary['time'] = best_lap.lap_time report.append(summary) return Right(report) else: return Left([Message(category=MessageCategory.WARNING, target='best_lap_pilot_report', key='without_pilots')])
def create_report(self, pilots: [Pilot]) -> Either: report = list() if pilots: for pilot in pilots: summary = dict() summary['name'] = pilot.name summary['number'] = pilot.number summary['averageSpeed'] = pilot.avg_speed report.append(summary) return Right(report) else: return Left([ Message(category=MessageCategory.WARNING, target='average_time_report', key='without_pilots') ])
def validate(self, line_content, line_number) -> Either: violations = [] try: str(line_content[0]) except ValueError: violations.append( Message(category=MessageCategory.VALIDATION, key='invalid_time_format', args=(line_number, line_content['poc_id']))) try: car, pilot_name = str(line_content[1]).strip().split('–') try: int(car) except ValueError: violations.append( Message(category=MessageCategory.VALIDATION, key='invalid_car_number', args=(line_number, line_content[1]))) if not pilot_name: violations.append( Message(category=MessageCategory.VALIDATION, key='invalid_pilot_name', args=(line_number, line_content[1]))) except ValueError: violations.append( Message(category=MessageCategory.VALIDATION, key='invalid_pilot_name', args=(line_number, line_content[1]))) try: int(line_content[2]) except ValueError: violations.append( Message(category=MessageCategory.VALIDATION, key='invalid_lap_times', args=(line_number, line_content[2]))) try: float(line_content[4].replace(',', '.')) except ValueError: violations.append( Message(category=MessageCategory.VALIDATION, key='invalid_lap_speed', args=(line_number, line_content[4]))) if violations: return Left(violations) return Right(None)
def create_report(self, pilots: [Pilot]) -> Either: if pilots: summary = dict() best_lap = None best_lap_speed = 0 best_pilot = None for pilot in pilots: temp_best_lap = max(pilot.laps, key=lambda lap: lap.lap_speed) if temp_best_lap.lap_speed > best_lap_speed: best_lap = temp_best_lap best_pilot = pilot best_lap_speed = temp_best_lap.lap_speed if best_lap: summary['name'] = best_pilot.name summary['lap'] = best_lap.number summary['time'] = best_lap.lap_time return Right(summary) else: return Left([Message(category=MessageCategory.WARNING, target='best_lap_report', key='without_pilots')])