工具来强制Python代码风格/标准 [英] Tool to enforce python code style/standards

查看:470
本文介绍了工具来强制Python代码风格/标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个工具来检查编码风格在python。

I'm trying to find a tool to check for coding style in python.

对于PHP我看到有代码嗅探器, Drupal使用的一个href =http://drupal.org/coding-standards#helper =nofollow noreferrer> perl脚本。有没有这样的工具的python代码?

For PHP I've seen there is the Code Sniffer, and a small perl script used by Drupal. Is there such a tool for python code?

推荐答案

在过去我主要使用 PyLint - 它可以突出显示当您使用未定义的变量,当您导入的东西,而不使用它们等。

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.

它可能有点冗长,抱怨的行像超过80个字符长,变量不匹配特定的正则表达式,类的公共方法太少,方法缺少docs trings。

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.

例如,对于脚本..

import os
import somefakelib

def myfunc(x):
    blah = "Something"
    print os.listdir( x+blh )

PyLint生成以下消息:

PyLint generates the following messages:

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

..或作为PyLint命令的命令行参数:

..or as command line arguments to the PyLint command:

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还生成一个报告,包括文件有多少行代码/评论/ docstring /空格,每个类别的消息数,并给你的代码一个分数 - 10是没有消息,0通常是语法错误

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

另一个选项是 PyFlakes ,我发现一点也不过分冗长(我最近开始使用它代替PyLint)。再次使用上述脚本,PyFlakes给出以下消息:

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:

example.py:2: 'somefakelib' imported but unused
example.py:6: undefined name 'blh'

我使用的最后一个选项是 pep8.py ,顾名思义, PEP8 。它是迄今为止最多的脚本,强制执行正确的空行之前/之后的函数/类,间隔的代码,正确的4空格缩进等。

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

运行上面的代码,它会产生以下结果:

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

它主要强制样式的东西,如正确的空格,它没有做许多静态分析的代码像PyLint或PyFlakes, pep8.py结合PyLint或PyFlakes。

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.

pep8.py 最初在 python mailing list here ,but the download link in this现在已经死了。有一个github镜像由cburroughs,有一些小的修复,在 github。 com / cburroughs / pep8.py ,或者您可以从旧版本

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

PyChecker 是另一个选项,虽然我没有使用它

PyChecker is another option, although I haven't use it

这篇关于工具来强制Python代码风格/标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆