From cb937aa29605e3695048c49a2619ec269809d489 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Mon, 18 Jul 2022 09:10:56 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- tools/lou_maketable.d/export_chunked_words.py | 10 ++++++---- tools/lou_maketable.d/make_suggestions.py | 19 ++++++++----------- tools/lou_maketable.d/submit_rows.py | 13 ++++++------- tools/lou_maketable.d/submit_rules.py | 16 +++++++++++----- tools/lou_maketable.d/utils.py | 18 ++++++++++-------- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/tools/lou_maketable.d/export_chunked_words.py b/tools/lou_maketable.d/export_chunked_words.py index f1f7e86b1d..89d4f64b16 100644 --- a/tools/lou_maketable.d/export_chunked_words.py +++ b/tools/lou_maketable.d/export_chunked_words.py @@ -35,13 +35,15 @@ def main(): for text, braille in c.fetchall(): # ignore long words for now if len(text) > 48: - printerrln('WARNING: word is too long to be processed, may be translated incorrectly: %s' % text) + printerrln( + f'WARNING: word is too long to be processed, may be translated incorrectly: {text}' + ) + continue if not is_letter(text): - printerrln('WARNING: word has non-letters: %s' % text) + printerrln(f'WARNING: word has non-letters: {text}') continue - suggested_hyphen_string = suggest_chunks(text, braille) - if suggested_hyphen_string: + if suggested_hyphen_string := suggest_chunks(text, braille): if hyphenation_enabled: actual_hyphen_string = hyphenate(text) else: diff --git a/tools/lou_maketable.d/make_suggestions.py b/tools/lou_maketable.d/make_suggestions.py index a201f520b4..3edf5c2f05 100644 --- a/tools/lou_maketable.d/make_suggestions.py +++ b/tools/lou_maketable.d/make_suggestions.py @@ -56,24 +56,21 @@ def main(): if actual_braille != braille: problem = True if problems < args.MAX_PROBLEMS: - comments = [] - comments.append("wrong braille\t\t" + actual_braille) + comments = ["wrong braille\t\t" + actual_braille] suggest_rows = [] - suggest_rules = [] non_letters = False if not is_letter(text): non_letters = True comments.append("word has non-letters") comments.append("applied rules") applied_rules = [get_rule(x) for x in applied_rules if x is not None] - for rule in applied_rules: - comments.append("> " + "\t".join(rule)) - other_relevant_rules = set(find_relevant_rules(text)) - set(applied_rules) - if other_relevant_rules: + comments.extend("> " + "\t".join(rule) for rule in applied_rules) + if other_relevant_rules := set( + find_relevant_rules(text) + ) - set(applied_rules): comments.append("other relevant rules") - for rule in other_relevant_rules: - comments.append("> " + "\t".join(rule)) - suggest_rules.append({"opcode": "word", "text": text, "braille": braille}) + comments.extend("> " + "\t".join(rule) for rule in other_relevant_rules) + suggest_rules = [{"opcode": "word", "text": text, "braille": braille}] if problem and problems < args.MAX_PROBLEMS: if args.NON_INTERACTIVE and suggest_rules: for rule in suggest_rules: @@ -81,7 +78,7 @@ def main(): else: println("# >>>\t%s\t%s" % (text, braille or "")) for comment in comments: - println("# | " + comment) + println(f"# | {comment}") println("# |__") for row in suggest_rows: println("#\t%(text)s\t%(braille)s" % row) diff --git a/tools/lou_maketable.d/submit_rows.py b/tools/lou_maketable.d/submit_rows.py index febde5753a..ef0ae57a96 100644 --- a/tools/lou_maketable.d/submit_rows.py +++ b/tools/lou_maketable.d/submit_rows.py @@ -33,20 +33,19 @@ def __next__(self): row = next(self.reader) if not row or row[0].startswith("#"): return next(self) - if not len(row) == 3: - printerrln('expected 3 columns, got %s: %s' % (len(row),row)) + if len(row) != 3: + printerrln(f'expected 3 columns, got {len(row)}: {row}') exit(1) - if not row[0] == "": + if row[0] != "": printerrln("expected first column to be empty, got '%s'" % (row[0],)) exit(1) maybe_chunked_text, braille = row[1:3] maybe_chunked_text = to_lowercase(maybe_chunked_text) text, chunked_text = read_text(maybe_chunked_text) braille = braille if braille != "" else None - if braille != None: - if '0' in to_dot_pattern(braille).split('-'): - printerrln('invalid braille: %s' % (braille,)) - exit(1) + if braille != None and '0' in to_dot_pattern(braille).split('-'): + printerrln(f'invalid braille: {braille}') + exit(1) exit_if_not(not chunked_text or validate_chunks(chunked_text)) return {'text': text, 'braille': braille, diff --git a/tools/lou_maketable.d/submit_rules.py b/tools/lou_maketable.d/submit_rules.py index 9ae3e52765..dad7b87d9f 100644 --- a/tools/lou_maketable.d/submit_rules.py +++ b/tools/lou_maketable.d/submit_rules.py @@ -36,7 +36,7 @@ def main(): rules = [] for line in fileinput.FileInput(args.CONTRACTION_TABLE, openhook=fileinput.hook_encoded("utf-8")): m = p.match(line) - exit_if_not(m and not m.group(1)) + exit_if_not(m and not m[1]) _, opcode, text, braille, _, comment = m.groups() if opcode: rule = {"opcode": opcode, "text": text, "braille": braille, "comment": comment} @@ -44,14 +44,14 @@ def main(): for line in fileinput.FileInput(args.FILE, openhook=fileinput.hook_encoded("utf-8")): m = p.match(line) if not m: - printerrln("%s: rule is not valid" % (line,)) + printerrln(f"{line}: rule is not valid") exit(1) exit_if_not(m) add_or_delete, opcode, text, braille, _, _ = m.groups() if opcode: comment = braille if comment.endswith('\\'): - comment = comment + " " + comment = f"{comment} " braille = to_dot_pattern(braille) rules, rule = partition(lambda rule: rule["opcode"] == opcode and rule["text"] == text and @@ -60,11 +60,17 @@ def main(): rule = list(rule) if add_or_delete == '-': if not rule: - printerrln("%s %s %s: rule can not be deleted because not in %s" % (opcode,text,braille,args.CONTRACTION_TABLE)) + printerrln( + f"{opcode} {text} {braille}: rule can not be deleted because not in {args.CONTRACTION_TABLE}" + ) + exit(1) else: if rule: - printerrln("%s %s %s: rule can not be added because already in %s" % (opcode,text,braille,args.CONTRACTION_TABLE)) + printerrln( + f"{opcode} {text} {braille}: rule can not be added because already in {args.CONTRACTION_TABLE}" + ) + exit(1) rule = {"opcode": opcode, "text": text, "braille": braille, "comment": comment} rules.append(rule) diff --git a/tools/lou_maketable.d/utils.py b/tools/lou_maketable.d/utils.py index 892e2a7328..280c385a50 100644 --- a/tools/lou_maketable.d/utils.py +++ b/tools/lou_maketable.d/utils.py @@ -112,7 +112,7 @@ def split_into_words(text, hyphen_string): word_hyphen_strings = [] word = [] word_hyphen_string = [] - for c,(h1,h2) in izip(text, pairwise('1' + hyphen_string + '1')): + for c,(h1,h2) in izip(text, pairwise(f'1{hyphen_string}1')): if is_letter(c): word.append(c) word_hyphen_string.append(h1) @@ -138,7 +138,7 @@ def load_table(new_table): liblouis.loadTable(table); def is_letter(text): - return all([liblouis.isLetter(c) for c in text]) + return all(liblouis.isLetter(c) for c in text) def to_lowercase(text): return "".join([liblouis.toLowercase(c) for c in text]) @@ -167,13 +167,15 @@ def translate(text): c_rules_len = c_int(max_rules) exit_if_not(liblouis._lou_translateWithTracing(table, c_text, byref(c_text_len), c_braille, byref(c_braille_len), None, None, None, None, None, 0, c_rules, byref(c_rules_len))) - return c_braille.value, c_rules[0:c_rules_len.value] + return c_braille.value, c_rules[:c_rules_len.value] def get_rule(c_rule_pointer): c_rule_string = create_unicode_buffer(u"", 128) - if not liblouis.printRule(cast(c_rule_pointer, c_void_p), c_rule_string): - return None - return tuple(c_rule_string.value.split(" ")) + return ( + tuple(c_rule_string.value.split(" ")) + if liblouis.printRule(cast(c_rule_pointer, c_void_p), c_rule_string) + else None + ) def suggest_chunks(text, braille): c_text = create_unicode_buffer(text) @@ -182,7 +184,7 @@ def suggest_chunks(text, braille): if not liblouis.suggestChunks(c_text, c_braille, c_hyphen_string): return None; hyphen_string = c_hyphen_string.value.decode('ascii') - hyphen_string = hyphen_string[1:len(hyphen_string)-1] + hyphen_string = hyphen_string[1:-1] assert len(hyphen_string) == len(text) - 1 and re.search("^[01x]+$", hyphen_string) return hyphen_string @@ -190,7 +192,7 @@ def find_relevant_rules(text): c_text = create_unicode_buffer(text) max_rules = 16 c_rules = [u""] * max_rules + [None] - for i in range(0, max_rules): + for i in range(max_rules): c_rules[i] = create_unicode_buffer(c_rules[i], 128) c_rules[i] = cast(c_rules[i], c_wchar_p) c_rules = (c_wchar_p * (max_rules + 1))(*c_rules)