Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对于特定的pep8问题,解析python代码的更好方法是什么_Python_Python 3.x_Parsing_Pep8_Re - Fatal编程技术网

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

对于特定的pep8问题,解析python代码的更好方法是什么,python,python-3.x,parsing,pep8,re,Python,Python 3.x,Parsing,Pep8,Re,我知道,存在用于解析python代码的库,但是,为了了解它们如何解析错误,我正在创建一个脚本,该脚本只检查文件中的6个Pep8错误,仅供参考 这就是我当前的6个Pep8函数的外观(如果发现问题,它们会将问题附加到问题列表中) “”“ [S001]行长度超过79个字符 [S002]缩进不是四的倍数 [S003]语句后不必要的分号(注意,分号可用于注释) [S004]内联注释前至少需要两个空格 [S005]发现TODO(仅在评论中;案例无关紧要) [S006]在该行之前使用了两个以上的空行(必须为第

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

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

“”“
[S001]行长度超过79个字符
[S002]缩进不是四的倍数
[S003]语句后不必要的分号(注意,分号可用于注释)
[S004]内联注释前至少需要两个空格
[S005]发现TODO(仅在评论中;案例无关紧要)
[S006]在该行之前使用了两个以上的空行(必须为第一个非空行输出)
"""
def S001(自身,项次:整数,行:str):
如果len(线)>79:
self.issues.append(f“第{ln_num}:S001行太长”)
def S002(自身,项次:整数,行:str):
缩进长度=len(line)-len(line.lstrip())
如果缩进长度为%4!=0:
append(f“行{ln_num}:S002缩进不是四的倍数”)
def S003(自身,项次:整数,行:str):
regex1=re.compile((.*)(;(\s)*#)|(;$))
regex2=re.compile(#.*))
如果是regex1.search(行),而不是regex2.search(行):
self.issues.append(f“Line{ln_num}:S003不必要的分号”)
def S004(自身,项次:整数,行:str):
regex=re.compile(([^]{2})|(\s[^])|([^]\s))|)
如果正则表达式搜索(第行):
self.issues.append(f“行{ln_num}:S004在内联注释之前至少需要两个空格”)
def S005(自身,项次:整数,行:str):
regex=re.compile(“#(.*)todo”,flags=re.IGNORECASE)
如果正则表达式搜索(第行):
self.issues.append(f“行{ln_num}:S005 TODO found”)
def S006(自身,项次:整数,行:str):
如果self.code[ln_num-4:ln_num-1]=['','']和line!="":
self.issues.append(f“第{ln_num}行:S006行前面使用了两个以上的空行”)
测试用例:

""" 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

我知道我的代码不是最优的,也不能满足每一种边缘情况,但我想知道如何正确解析错误。由于我个人不喜欢我的答案,所以我希望改进或更好地分析错误。

可以从查看现有pep8 linter之一的代码开始吗?这可能会让你更好地了解它们的工作原理。