moy*_*260 10 python lint static-code-analysis
我不想在Python模块中使用"print"语句,因为我们将使用记录器.
我正在尝试生成一个脚本来检查带有pylint的模块.但是,pylint当前不会将此检测为警告或错误.
我想根据我们的内部Python编程标准将"打印"调用检测为错误或警告.
我怎样才能做到这一点?
ale*_*cxe 12
flake8有一个flake8-print专门用于任务的插件:
flake8打印
检查python文件中的Print语句.
DEMO:
$ cat test.py
s = "test"
print s
$ flake8 test.py
test.py:2:1: T001 print statement found.
Run Code Online (Sandbox Code Playgroud)
如果出于某种原因你不想flake8-print按照@alecxe的建议使用你可以使用ast模块自己动手- 它利用Python的编译器来解析文件,这样你就可以可靠地找到print(而不是刚刚开始的行print):
代码:
import ast
with open('blah.py') as fin:
parsed = ast.parse(fin.read())
for node in ast.walk(parsed):
if isinstance(node, ast.Print):
print 'print at line {} col {}'.format(node.lineno, node.col_offset)
Run Code Online (Sandbox Code Playgroud)
blah.py:
def print_test():
print 'hello'
print 'goodbye'
Run Code Online (Sandbox Code Playgroud)
输出:
print at line 4 col 0
print at line 2 col 1
Run Code Online (Sandbox Code Playgroud)
如果您希望导航文件夹或子文件夹,可以使用os.walk或者os.listdir最适合的任何内容.
| 归档时间: |
|
| 查看次数: |
2670 次 |
| 最近记录: |