def tournament_results(self): """ Функция считает очки из файла протокола турнира и записывает результаты в выходной файл """ self.winners_table = {} with open(self.source, 'r', encoding='utf8') as sourcefile: with open(self.good_log, 'a', encoding='utf8') as goodlog: with open(self.bad_log, 'a', encoding='utf8') as badlog: lines = sourcefile.readlines() for line in lines: line = line.replace('\n', '') if 'Tour' in line: goodlog.write(f"{line}\n") badlog.write(f"{line}\n") continue if 'winner' in line: if self.scores: self.scores = sorted(self.scores.items(), key=lambda x: x[1], reverse=True) goodlog.write( f"winner is {self.scores[0][0]}\n") self.winners_table[self.scores[0] [0]]['wins'] += 1 else: goodlog.write('Can not calculate the score\n') goodlog.write('\n') badlog.write('\n') self.scores.clear() self.scores = {} continue if not line: continue player, score = line.split('\t') try: if self.mode == '1': result = ScoreCalculator().get_score(score) elif self.mode == '2': result = ScoreCalculatorNew().get_score(score) self.scores[player] = result goodlog.write(f"{player} {score} {result}\n") if player in self.winners_table: self.winners_table[player]['matches'] += 1 else: self.winners_table[player] = { 'matches': 1, 'wins': 0 } except (ValueError, FramesQuantityError) as exc: badlog.write(f"{player} {score} {exc}\n") self.winners_table = sorted(self.winners_table.items(), key=lambda res: (res[1]['wins'], res[1]['matches']), reverse=True) print( f'\n\nПроизведена запись в файл {self.good_log}\nПроизведена запись в файл {self.bad_log}\n\n' )
def test_7(self): result = ScoreCalculatorNew().get_score('--8-X3/4/1/-12651X') self.assertEqual(result, 88)
def test_6(self): result = ScoreCalculatorNew().get_score('811/X--3/XX171/43') self.assertEqual(result, 127)
def test_5(self): result = ScoreCalculatorNew().get_score('4-3/7/3/8/X711627-5') self.assertEqual(result, 119)
def test_4(self): result = ScoreCalculatorNew().get_score('8/--35-47/371/518-4/') self.assertEqual(result, 84)
def test_3(self): result = ScoreCalculatorNew().get_score('725518X--8/--543152') self.assertEqual(result, 68)
def test_2(self): result = ScoreCalculatorNew().get_score('3532X332/3/62--62X') self.assertEqual(result, 90)
def test_1(self): result = ScoreCalculatorNew().get_score('1/6/1/--327-18812382') self.assertEqual(result, 82)