-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery refactored master branch #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,32 +56,29 @@ 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}] | ||
Comment on lines
-59
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if problem and problems < args.MAX_PROBLEMS: | ||
if args.NON_INTERACTIVE and suggest_rules: | ||
for rule in suggest_rules: | ||
println("%(opcode)s\t%(text)s\t%(braille)s" % rule) | ||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
Comment on lines
-36
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
exit_if_not(not chunked_text or validate_chunks(chunked_text)) | ||
return {'text': text, | ||
'braille': braille, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,22 +36,22 @@ 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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
_, opcode, text, braille, _, comment = m.groups() | ||
if opcode: | ||
rule = {"opcode": opcode, "text": text, "braille": braille, "comment": comment} | ||
rules.append(rule) | ||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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')): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
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 | ||
) | ||
Comment on lines
-174
to
+178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def suggest_chunks(text, braille): | ||
c_text = create_unicode_buffer(text) | ||
|
@@ -182,15 +184,15 @@ 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] | ||
Comment on lines
-185
to
+187
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
assert len(hyphen_string) == len(text) - 1 and re.search("^[01x]+$", hyphen_string) | ||
return hyphen_string | ||
|
||
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): | ||
Comment on lines
-193
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
main
refactored with the following changes:replace-interpolation-with-fstring
)use-named-expression
)