Skip to content

Instantly share code, notes, and snippets.

@turbaszek
Last active July 15, 2019 21:00
Show Gist options
  • Save turbaszek/c9d6c72aa4e29786700df7e88d51b129 to your computer and use it in GitHub Desktop.
Save turbaszek/c9d6c72aa4e29786700df7e88d51b129 to your computer and use it in GitHub Desktop.
import re
# TODO:
# - in last step fix all errors like to many lines etc
# - unused argument needs unused argument so those have to be taken out durgin parsing
def remove_unused(line, unused):
""" Removes unused arguments """
for arg in unused:
line = line.replace(arg, "")
line = line.replace(",,", "")
line = line.replace(", ,", "")
line = line.replace(" )", ")")
return line
def fix_no_member(line):
""" Fixes no-member error """
add_newline = False
if line.endswith("\n"):
line = line[:-1]
add_newline = True
line = line + " #pylint:disable=no=-member"
if len(line) >= 110:
line += ",line-too-long #noqa"
return line + "\n" if add_newline else line
def read_pylint_errors(filename = "pylint_error.txt"):
with open(filename, "r+") as file:
errors = [line for line in file.readlines() if ".py:" in line]
todo = dict()
for error in errors:
splitted = error.split(": ")
position, msg = splitted[0], ": ".join(splitted[1:])
file, line_number, _ = position.split(":")
local_todo = todo.get(file, [])
error_name = msg.split("(")[-1][:-2]
local_todo.append((line_number, error_name))
todo[file] = local_todo
return todo
def fix_line(line: str, inline_errors: list):
""" Fix errors in line """
for error in set(inline_errors):
if error == "no-member":
line = fix_no_member(line)
return line
def fix_file(filename: str, errors: list):
""" Fix pylint errors in file """
new_lines = []
with open(filename, "r+") as file:
lines = file.readlines()
for line_no, line in enumerate(lines):
inline_errors = [t[1] for t in errors if t[0] == str(line_no + 1)]
new_lines.append(fix_line(line, inline_errors))
print("".join(new_lines))
file.write(new_lines)
def fix_errors(todos: dict):
""" Fix errors in all input files """
for filename, errors in todos.items():
print(errors)
fix_file(filename, errors)
def pylint_awsome(pylint_errors_file: str):
todos = read_pylint_errors(pylint_errors_file)
fix_errors(todos)
print("Awesomness applied !")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment