对于特定的pep8问题,解析python代码的更好方法是什么

问题描述

我知道,存在用于解析python代码的库,但是,为了学习它们如何解析错误,我正在创建一个脚本,该脚本仅检查文件中是否存在6个Pep8错误以供参考。

这是我当前的6个Pep8函数的外观(如果发现问题,它们会在问题列表中附加一个问题)

"""
[S001] Line is longer than 79 characters
[S002] Indentation is not a multiple of four
[S003] Unnecessary semicolon after a statement (note,semicolons are admissible in comments)
[S004] At least two spaces before inline comments required
[S005] Todo found (only in comments; the case does not matter)
[S006] More than two blank lines used before this line (must be output for the first non-empty line)
"""
def S001(self,ln_num: int,line: str):
    if len(line) > 79:
        self.issues.append(f"Line {ln_num}: S001 Too Long")


def S002(self,line: str):
    indentation_length = len(line) - len(line.lstrip())
    if indentation_length % 4 != 0:
        self.issues.append(f"Line {ln_num}: S002 Indentation is not a multiple of four")


def S003(self,line: str):
    regex1 = re.compile("(.*)((;(\s)*#)|(;$))")
    regex2 = re.compile("#.*;")
    if regex1.search(line) and not regex2.search(line):
        self.issues.append(f"Line {ln_num}: S003 Unnecessary semicolon")


def S004(self,line: str):
    regex = re.compile("(([^ ]{2})|(\s[^ ])|([^ ]\s))#")
    if regex.search(line):
        self.issues.append(f"Line {ln_num}: S004 At least two spaces before inline comments required")


def S005(self,line: str):
    regex =  re.compile("#(.*)todo",flags=re.IGnorECASE)
    if regex.search(line):
        self.issues.append(f"Line {ln_num}: S005 Todo found")


def S006(self,line: str):
    if self.code[ln_num-4:ln_num-1] == ['','',''] and line != "":
        self.issues.append(f"Line {ln_num}: S006 More than two blank lines used before this line")

测试用例:

""" Test case 1 """
print('What\'s your name?') # reading an input
name = input();
print(f'Hello,{name}');  # here is an obvIoUs comment: this prints greeting with a name


very_big_number = 11_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000
print(very_big_number)



def some_fun():
    print('NO Todo HERE;;')
    pass; # Todo something
""" END """

""" Test Case 2 """
print('hello')
print('hello');
print('hello');;;
print('hello');  # hello
# hello hello hello;
greeting = 'hello;'
print('hello')  # ;
""" END """

""" Test Case 3 """
    print('hello')
    print('hello')  # Todo
    print('hello')  # Todo # Todo
    # todo
    # Todo just do it
    print('todo')
    print('Todo Todo')
    todo()
    todo = 'todo'
""" END """

""" Test Case 4 """
print("hello")


print("bye")



print("check")
""" END """

""" Test Case 5 """
print('hello!')
# just a comment
print('hello!')  #
print('hello!')  # hello

print('hello!') # hello
print('hello!')# hello
""" END """

测试用例1的预期输出

Line 1: S004 At least two spaces before inline comment required
Line 2: S003 Unnecessary semicolon
Line 3: S001 Too long
Line 3: S003 Unnecessary semicolon
Line 6: S001 Too long
Line 11: S006 More than two blank lines used before this line
Line 13: S003 Unnecessary semicolon
Line 13: S004 At least two spaces before inline comment required
Line 13: S005 Todo found

我知道我的代码不是最优的,并且不能满足所有极端情况,但是我想知道它们如何正确解析错误的想法。由于我个人不喜欢我的答案,因此我想对如何分析错误进行改进或更好的主意。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)