现在的位置: 首页 > 综合 > 正文

python规范代码之pylint

2013年07月26日 ⁄ 综合 ⁄ 共 3777字 ⁄ 字号 评论关闭

今天比较空,找点资料学习学习:

ylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准(Pylint 默认使用的代码风格是 PEP 8,具体信息,请参阅参考资料)和有潜在问题的代码。

python 代码风格参考:

http://www.python.org/dev/peps/pep-0008/

windows上安装:

logilab-astng 的最新包下载:http://www.logilab.org/856/

logilab-common 的最新包下载:http://www.logilab.org/848/

Pylint 的最新包下载:http://www.logilab.org/project/pylint
依次下载上述包,并解压到python的安装目录下,使用 cd依次进入 logilab-astng、logilab-common 和 Pylint 解开的文件夹中,运行命令python setup.py install来安装。

安装完成后,在 Python 的安装路径下出现一个 Scripts 文件夹,里面包含一些 bat 脚本,如 pylint.bat 等。

C:\Python27\Scripts

将pylint加到path中这样直接就在cmd中运行pylint就可以使用了

利用pylint进行代码检查:

MESSAGE_TYPE 有如下几种:

(C) 惯例。违反了编码风格标准

(R) 重构。写得非常糟糕的代码。

(W) 警告。某些 Python 特定的问题。

(E) 错误。很可能是代码中的错误。

(F) 致命错误。阻止 Pylint 进一步运行的错误。

如:

C:\Documents and Settings\Administrator>pylint D:\Python\test.py
No config file found, using default configuration
************* Module test
C: 13,0: Line too long (95/80)
C: 48,0: Line too long (84/80)
W: 68,0: Redefining built-in 'format'
C:  1,0: Missing docstring
W: 62,-1: String statement has no effect
C: 68,0: Operator not preceded by a space

Report
======
23 statements analysed.

External dependencies
---------------------
::

    xml
      \-dom
        \-minidom (test)
          \-Node (test)

Duplication
-----------

+-------------------------+------+---------+-----------+
|                         |now   |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines      |0     |0        |=          |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |0.000    |=          |
+-------------------------+------+---------+-----------+

Global evaluation
-----------------
Your code has been rated at -2.61/10 (previous run: -2.61/10)

Raw metrics
-----------

+----------+-------+------+---------+-----------+
|type      |number |%     |previous |difference |
+==========+=======+======+=========+===========+
|code      |21     |84.00 |21       |=          |
+----------+-------+------+---------+-----------+
|docstring |0      |0.00  |0        |=          |
+----------+-------+------+---------+-----------+
|comment   |2      |8.00  |2        |=          |
+----------+-------+------+---------+-----------+
|empty     |2      |8.00  |2        |=          |
+----------+-------+------+---------+-----------+

Statistics by type
------------------

+---------+-------+-----------+-----------+------------+---------+
|type     |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module   |1      |1          |=          |0.00        |0.00     |
+---------+-------+-----------+-----------+------------+---------+
|class    |0      |0          |=          |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|method   |0      |0          |=          |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|function |0      |0          |=          |0           |0        |
+---------+-------+-----------+-----------+------------+---------+

附上:

Pylint 的常用命令行参数

    * -h,--help

      显示所有帮助信息。
    * --generate-rcfile

      可以使用 pylint --generate-rcfile 来生成一个配置文件示例。可以使用重定向把这个配置文件保存下来用做以后使用。也可以在前面加上其它选项,使这些选项的值被包含在这个产生的配置文件里。如:pylint --persistent=n --generate-rcfile > pylint.conf,查看 pylint.conf,可以看到 persistent=no,而不再是其默认值 yes。
    * --rcfile=<file>

      指定一个配置文件。把使用的配置放在配置文件中,这样不仅规范了自己代码,也可以方便地和别人共享这些规范。
    * -i <y_or_n>, --include-ids=<y_or_n>

      在输出中包含 message 的 id, 然后通过 pylint --help-msg=<msg-id>来查看这个错误的详细信息,这样可以具体地定位错误。
    * -r <y_or_n>, --reports=<y_or_n>

      默认是 y, 表示 Pylint 的输出中除了包含源代码分析部分,也包含报告部分。
    * --files-output=<y_or_n>

      将每个 module /package 的 message 输出到一个以 pylint_module/package. [txt|html] 命名的文件中,如果有 report 的话,输出到名为 pylint_global.[txt|html] 的文件中。默认是输出到屏幕上不输出到文件里。
    * -f <format>, --output-format=<format>

      设置输出格式。可以选择的格式有 text, parseable, colorized, msvs (visual studio) 和 html, 默认的输出格式是 text。
    * --disable-msg= <msg ids>

      禁止指定 id 的 message. 比如说输出中包含了 W0402 这个 warning 的 message, 如果不希望它在输出中出现,可以使用 --disable-msg= W0402

抱歉!评论已关闭.