def testEmptyLines(self): empties = """\t \t\t \t \t \t * * * \t ab abc * * *+*""".split( "\n" ) for line in empties: self.assertEqual(parse_line(line), None) bad_empties = """ * bad comment \t* bad comment \t\tbad comment * * bad comment""".split( "\n" ) for line in bad_empties: self.assertRaises(AssemblyError, parse_line, line)
def testLabels(self): # auxillary function def lines(labels): return [(l + ' nop', l) for l in labels.split(' ')] correct_labels = 'blah a z 123y321 1a1 a1 1a 123456789a 9H labellabel 40F 40B F B' # l - source line, label - label of this line for src_line, label in lines(correct_labels): self.assertEqual(parse_line(src_line).label, label.upper()) incorrect_labels = '4F 4B 0F 0B 9F 9B 123 1 2 label* # % label,' for l, _ in lines(incorrect_labels): self.assertRaises(InvalidLabelError, parse_line, l) self.assertRaises(TooLongLabelError, parse_line, 'VERYLONGLABEL NOP')
def testLabels(self): # auxillary function def lines(labels): return [(l + " nop", l) for l in labels.split(" ")] correct_labels = "blah a z 123y321 1a1 a1 1a 123456789a 9H labellabel 40F 40B F B" # l - source line, label - label of this line for src_line, label in lines(correct_labels): self.assertEqual(parse_line(src_line).label, label.upper()) incorrect_labels = "4F 4B 0F 0B 9F 9B 123 1 2 label* # % label," for l, _ in lines(incorrect_labels): self.assertRaises(InvalidLabelError, parse_line, l) self.assertRaises(TooLongLabelError, parse_line, "VERYLONGLABEL NOP")
def parse_file(f): print 'processing ' + f + '...' global totranslate copyfile(f, f.split('.srt')[0] + 'OLD.srt') subs = pysrt.open(f, encoding='utf-8') for i in range(len(subs)): print i sen = subs[i].text parsed_line = parse_line(sen, len(totranslate)) subs[i].text = parsed_line[0] totranslate += parsed_line[1] subs.save(f, encoding='utf-8') def worker(): global totranslate global translated while True: index = q.get() print 'working with ' + str(index) totranslate[index] = get(totranslate[index][0], totranslate[index][1]) q.task_done() q = Queue() for i in range(30): t = Thread(target=worker) t.daemon = True t.start() for index in range(len(totranslate)): q.put(index) q.join() fl = codecs.open(f, mode='r+', encoding='utf-8') s = fl.read() fl.close() fl = codecs.open(f, mode='w+', encoding='utf-8') fl.truncate() fl.write(s.format(*totranslate)) fl.close()
def parse_file(f): print 'processing ' + f + '...' global totranslate copyfile(f, f.split('.srt')[0] + 'OLD.srt') subs = pysrt.open(f, encoding='utf-8') for i in range(len(subs)): print i sen = subs[i].text parsed_line = parse_line(sen, len(totranslate)) subs[i].text = parsed_line[0] totranslate += parsed_line[1] subs.save(f, encoding='utf-8') def worker(): global totranslate global translated while True: index = q.get() print 'working with ' + str(index) totranslate[index] = get( totranslate[index][0], totranslate[index][1]) q.task_done() q = Queue() for i in range(30): t = Thread(target=worker) t.daemon = True t.start() for index in range(len(totranslate)): q.put(index) q.join() fl = codecs.open(f, mode='r+', encoding='utf-8') s = fl.read() fl.close() fl = codecs.open(f, mode='w+', encoding='utf-8') fl.truncate() fl.write(s.format(*totranslate)) fl.close()
def testEmptyLines(self): empties = \ """\t \t\t \t \t \t * * * \t ab abc * * *+*""".split("\n") for line in empties: self.assertEqual(parse_line(line), None) bad_empties = \ """ * bad comment \t* bad comment \t\tbad comment * * bad comment""".split("\n") for line in bad_empties: self.assertRaises(AssemblyError, parse_line, line)
import sys sys.path.append('src') from dataframe import DataFrame from parse_line import * print('Does parse_line work?') # line_1 = "1,0,3,'Braund, Mr. Owen Harris',male,22,1,0,A/5 21171,7.25,,S" assert parse_line(line_1) == [ '1', '0', '3', "'Braund, Mr. Owen Harris'", 'male', '22', '1', '0', 'A/5 21171', '7.25', '', 'S' ] line_2 = '102,0,3,"Petroff, Mr. Pastcho (""Pentcho"")",male,,0,0,349215,7.8958,,S' assert parse_line(line_2) == [ '102', '0', '3', '"Petroff, Mr. Pastcho (""Pentcho"")"', 'male', '', '0', '0', '349215', '7.8958', '', 'S' ] line_3 = '187,1,3,"O\'Brien, Mrs. Thomas (Johanna ""Hannah"" Godfrey)",female,,1,0,370365,15.5,,Q' assert parse_line(line_3) == [ '187', '1', '3', '"O\'Brien, Mrs. Thomas (Johanna ""Hannah"" Godfrey)"', 'female', '', '1', '0', '370365', '15.5', '', 'Q' ] print('Yes it does!', "\n") print('Does the dataframe implementation work?') #
def testFullLines(self): self.checkLine(parse_line('label nop'), 'LABEL', 'NOP', None) self.checkLine(parse_line('9H nop'), '9H', 'NOP', None) self.checkLine(parse_line(' nop\n'), None, 'NOP', None) self.checkLine(parse_line("\tnop"), None, 'NOP', None) self.checkLine(parse_line("\tnop arg"), None, 'NOP', 'ARG') self.checkLine(parse_line("\tnop arg comment"), None, 'NOP', 'ARG') self.checkLine(parse_line("label nop arg comment"), 'LABEL', 'NOP', 'ARG') self.checkLine(parse_line("label\tmove\t123\t* comment"), 'LABEL', 'MOVE', '123') self.checkLine(parse_line(" equ arg"), None, 'EQU', 'ARG') self.checkLine(parse_line(" equ arg comment"), None, 'EQU', 'ARG') self.checkLine(parse_line('msg alf "ABC "'), 'MSG', 'ALF', '"ABC "') self.checkLine(parse_line('msg alf "ABC " aa'), 'MSG', 'ALF', '"ABC " AA') self.checkLine(parse_line('msg alf aa"ABC "'), 'MSG', 'ALF', 'AA"ABC "') self.checkLine(parse_line('msg alf "ABC '), 'MSG', 'ALF', '"ABC ') self.checkLine(parse_line('msg alf A B C comment'), 'MSG', 'ALF', 'A B C COMMENT') self.assertRaises(MissingOperationError, parse_line, "labelonly") self.assertRaises(UnknownOperationError, parse_line, "label $^%$") self.assertRaises(UnknownOperationError, parse_line, "label nopp") self.assertRaises(UnknownOperationError, parse_line, "\tqqq") self.assertRaises(UnknownOperationError, parse_line, "\tqq* arg") self.assertRaises(UnknownOperationError, parse_line, "\tqqq arg") self.assertRaises(UnknownOperationError, parse_line, " mov arg comment") self.assertRaises(ArgumentRequiredError, parse_line, " equ") self.assertRaises(ArgumentRequiredError, parse_line, " orig")
def testFullLines(self): self.checkLine(parse_line("label nop"), "LABEL", "NOP", None) self.checkLine(parse_line("9H nop"), "9H", "NOP", None) self.checkLine(parse_line(" nop\n"), None, "NOP", None) self.checkLine(parse_line("\tnop"), None, "NOP", None) self.checkLine(parse_line("\tnop arg"), None, "NOP", "ARG") self.checkLine(parse_line("\tnop arg comment"), None, "NOP", "ARG") self.checkLine(parse_line("label nop arg comment"), "LABEL", "NOP", "ARG") self.checkLine(parse_line("label\tmove\t123\t* comment"), "LABEL", "MOVE", "123") self.checkLine(parse_line(" equ arg"), None, "EQU", "ARG") self.checkLine(parse_line(" equ arg comment"), None, "EQU", "ARG") self.checkLine(parse_line('msg alf "ABC "'), "MSG", "ALF", '"ABC "') self.checkLine(parse_line('msg alf "ABC " aa'), "MSG", "ALF", '"ABC " AA') self.checkLine(parse_line('msg alf aa"ABC "'), "MSG", "ALF", 'AA"ABC "') self.checkLine(parse_line('msg alf "ABC '), "MSG", "ALF", '"ABC ') self.checkLine(parse_line("msg alf A B C comment"), "MSG", "ALF", "A B C COMMENT") self.assertRaises(MissingOperationError, parse_line, "labelonly") self.assertRaises(UnknownOperationError, parse_line, "label $^%$") self.assertRaises(UnknownOperationError, parse_line, "label nopp") self.assertRaises(UnknownOperationError, parse_line, "\tqqq") self.assertRaises(UnknownOperationError, parse_line, "\tqq* arg") self.assertRaises(UnknownOperationError, parse_line, "\tqqq arg") self.assertRaises(UnknownOperationError, parse_line, " mov arg comment") self.assertRaises(ArgumentRequiredError, parse_line, " equ") self.assertRaises(ArgumentRequiredError, parse_line, " orig")