强制执行 python 代码风格/标准的工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1318799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tool to enforce python code style/standards
提问by solarc
I'm trying to find a tool to check for coding style in python.
我试图找到一个工具来检查 python 中的编码风格。
For PHP I've seen there is the Code Sniffer, and a small perl scriptused by Drupal. Is there such a tool for python code?
对于 PHP,我看到有 Code Sniffer 和Drupal 使用的一个小perl 脚本。有没有这样的python代码工具?
回答by dbr
In the past I've mainly use PyLint- it can highlight when you used an undefined variable, when you import things without using them and so on.
过去我主要使用PyLint- 它可以突出显示何时使用未定义的变量,何时导入而不使用它们等等。
It can be a bit verbose, complaining about things like lines being over 80 character long, variable not matching to specific regex's, classes having too few public methods, methods missing docs-trings.
它可能有点冗长,抱怨诸如行超过 80 个字符长、变量与特定正则表达式不匹配、类的公共方法太少、方法缺少文档字符串等问题。
For example, for script..
例如,对于脚本..
import os
import somefakelib
def myfunc(x):
blah = "Something"
print os.listdir( x+blh )
PyLint generates the following messages:
PyLint 生成以下消息:
C: 1: Missing docstring
F: 2: Unable to import 'somefakelib' (No module named somefakelib)
C: 4:myfunc: Missing docstring
C: 4:myfunc: Invalid name "x" (should match [a-z_][a-z0-9_]{2,30}$)
C: 4:myfunc: Invalid name "x" (should match [a-z_][a-z0-9_]{2,30}$)
E: 6:myfunc: Undefined variable 'blh'
W: 5:myfunc: Unused variable 'blah'
W: 2: Unused import somefakelib
They are all valid complaints, but I tend to disable a lot of the convention and refactoring messages. You can disable specific messages, either as comments in your code:
它们都是有效的抱怨,但我倾向于禁用很多约定和重构消息。您可以禁用特定消息,或者作为代码中的注释:
#pylint:disable-msg=R0903,C0103,R0903,F0401,C0301
..or as command line arguments to the PyLint command:
.. 或作为 PyLint 命令的命令行参数:
pylint --disable-msg=R0903,C0103,R0903,F0401,C0301 myfile.py
With the above messages disabled, it generates the following messages for the above code:
禁用上述消息后,它会为上述代码生成以下消息:
C: 1: Missing docstring
C: 4:myfunc: Missing docstring
E: 6:myfunc: Undefined variable 'blh'
W: 5:myfunc: Unused variable 'blah'
W: 2: Unused import somefakelib
PyLint also generates a "code report", including how many lines of code/comments/docstring/whitespace the file has, number of messages per-category, and gives your code a "score" - 10 being no messages, 0 generally being a syntax error
PyLint 还会生成“代码报告”,包括文件有多少行代码/注释/文档字符串/空格、每个类别的消息数,并为您的代码提供“分数”——10 表示没有消息,0 通常表示语法错误
Another option is PyFlakes, which I find a little less excessively-verbose (I've recently started using it in place of PyLint). Again using the above script, PyFlakes gives the following messages:
另一种选择是PyFlakes,我发现它不太冗长(我最近开始使用它代替 PyLint)。再次使用上述脚本,PyFlakes 给出以下消息:
example.py:2: 'somefakelib' imported but unused
example.py:6: undefined name 'blh'
The final option I use is pep8.py
, which as the name suggests enforces PEP8. It is by far the most.. pedantic script, enforcing things like correct blank-lines before/after functions/classes, spacing around code, correct 4-space indentation and so on..
我使用的最后一个选项是pep8.py
,顾名思义,它强制执行PEP8。它是迄今为止最..迂腐的脚本,强制执行诸如在函数/类之前/之后正确的空行、代码周围的间距、正确的 4 空格缩进等等。
Running on the code above, it produces the following:
运行上面的代码,它产生以下结果:
example.py:4:1: E302 expected 2 blank lines, found 1
example.py:6:23: E201 whitespace after '('
example.py:6:32: W292 no newline at end of file
It is mostly enforces stylistic things like correct whitespace, it does not do much static-analysis of the code like PyLint or PyFlakes, so I use pep8.py in conjunction with either PyLint or PyFlakes.
它主要是强制执行诸如正确的空格之类的文体,它不会像 PyLint 或 PyFlakes 那样对代码进行大量静态分析,因此我将 pep8.py 与 PyLint 或 PyFlakes 结合使用。
pep8.py
was originally announced on the python mailing list here, but the download link in this is now dead.. There's a github mirror by cburroughs, with a few minor fixes at github.com/cburroughs/pep8.py, or you can grab the unmodified version from an older revision
pep8.py
最初是在上宣布蟒蛇邮件列表在这里,但在这个下载链接是现在死了..在有通过cburroughs一个github上镜,有一些小的修正github.com/cburroughs/pep8.py,或者你可以抓住未修改从版本的旧版本
PyCheckeris another option, although I haven't use it
PyChecker是另一种选择,虽然我没用过
回答by David Cournapeau
回答by Calvin Cheng
Found this stackoverflow question while searching for a pep8 style enforcement tool when taking over an existing (legacy) project.
在接管现有(遗留)项目时搜索 pep8 样式执行工具时发现了这个 stackoverflow 问题。
https://github.com/hhatto/autopep8
https://github.com/hhatto/autopep8
autopep8 -i yourpythonsourcefile.py
will automagically convert all the source code to confirm with pep8. Tried it on my legacy project and it works great. So I thought I would update this answer here in SO.
将自动转换所有源代码以使用 pep8 进行确认。在我的遗留项目上尝试过它,效果很好。所以我想我会在这里更新这个答案。
回答by priestc
Theres a script called reindent.py
thats sometimes included in your system's python distribution which will go through and re-indent all your code to the recommended 4 spaces indenting.
有一个名为reindent.py
that的脚本有时包含在您的系统的 python 发行版中,它将遍历并将您的所有代码重新缩进到推荐的 4 个空格缩进。
Heres a copy of it in case you can't find it in your distribution: http://www.koders.com/python/fid24D30FCD2CE388C67CB980EF55630D25970CFB96.aspx?s=cdef%3Aparser
这是它的副本,以防您在发行版中找不到它:http: //www.koders.com/python/fid24D30FCD2CE388C67CB980EF55630D25970CFB96.aspx?s=cdef%3Aparser