【python】pylint进行代码静态检查
2019-07-02 本文已影响0人
小熊猫笔记
官网地址
https://pylint.readthedocs.io/en/latest/index.html
安装pylint
pip install pylint
基本使用示例
pylint D:\Python\TEST\practise\classes_list\try_collection\try_collection.py
No config file found, using default configuration
************* Module TEST.practise.classes_list.try_collection.try_collection
C: 15, 0: Exactly one space required around assignment
test_daque=list()
^ (bad-whitespace)
C: 25, 0: Exactly one space required around assignment
t1=time.time()
^ (bad-whitespace)
C: 26, 0: Exactly one space required after comma
for i in range(1,1000000):
^ (bad-whitespace)
C: 28, 0: Exactly one space required around assignment
t=time.time()-t1
^ (bad-whitespace)
C: 31, 0: Trailing newlines (trailing-newlines)
C: 1, 0: Missing module docstring (missing-docstring)
C: 6, 0: Invalid constant name "a" (invalid-name)
C: 7, 0: Invalid constant name "list_a" (invalid-name)
C: 9, 0: Invalid class name "grade" (invalid-name)
C: 15, 0: Invalid constant name "test_daque" (invalid-name)
C: 19, 0: Invalid constant name "elems" (invalid-name)
C: 24, 0: Import "import time" should be placed at the top of the module (wrong-import-position)
C: 25, 0: Invalid constant name "t1" (invalid-name)
C: 28, 0: Invalid constant name "t" (invalid-name)
W: 2, 0: Unused OrderedDict imported from collections (unused-import)
W: 3, 0: Unused deque imported from collections (unused-import)
C: 24, 0: standard import "import time" comes before "from gevent.pool import Pool" (wrong-import-order)
Report
======
26 statements analysed.
Statistics by type
------------------
+---------+-------+-----------+-----------+------------+---------+
|type |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module |1 |NC |NC |0.00 |0.00 |
+---------+-------+-----------+-----------+------------+---------+
|class |0 |NC |NC |0 |0 |
+---------+-------+-----------+-----------+------------+---------+
|method |0 |NC |NC |0 |0 |
+---------+-------+-----------+-----------+------------+---------+
|function |0 |NC |NC |0 |0 |
+---------+-------+-----------+-----------+------------+---------+
External dependencies
---------------------
::
gevent (TEST.practise.classes_list.try_collection.try_collection)
\-pool (TEST.practise.classes_list.try_collection.try_collection)
Raw metrics
-----------
+----------+-------+------+---------+-----------+
|type |number |% |previous |difference |
+==========+=======+======+=========+===========+
|code |26 |81.25 |NC |NC |
+----------+-------+------+---------+-----------+
|docstring |0 |0.00 |NC |NC |
+----------+-------+------+---------+-----------+
|comment |0 |0.00 |NC |NC |
+----------+-------+------+---------+-----------+
|empty |6 |18.75 |NC |NC |
+----------+-------+------+---------+-----------+
Duplication
-----------
+-------------------------+------+---------+-----------+
| |now |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines |0 |NC |NC |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |NC |NC |
+-------------------------+------+---------+-----------+
Messages by category
--------------------
+-----------+-------+---------+-----------+
|type |number |previous |difference |
+===========+=======+=========+===========+
|convention |15 |NC |NC |
+-----------+-------+---------+-----------+
|refactor |0 |NC |NC |
+-----------+-------+---------+-----------+
|warning |2 |NC |NC |
+-----------+-------+---------+-----------+
|error |0 |NC |NC |
+-----------+-------+---------+-----------+
Messages
--------
+----------------------+------------+
|message id |occurrences |
+======================+============+
|invalid-name |7 |
+----------------------+------------+
|bad-whitespace |4 |
+----------------------+------------+
|unused-import |2 |
+----------------------+------------+
|wrong-import-position |1 |
+----------------------+------------+
|wrong-import-order |1 |
+----------------------+------------+
|trailing-newlines |1 |
+----------------------+------------+
|missing-docstring |1 |
+----------------------+------------+
Global evaluation
-----------------
Your code has been rated at 3.46/10
生成配置文件参数
pylint --persistent=n --generate-rcfile > pylint.conf
#--persistent=n 非持久化,不存储上次的结果
# > pylint.conf 重定向到conf文件(很长,400多行)
修改配置文件参数示例
1.const-rgx #常量命名方式
# Regular expression matching correct constant names. Overrides const-naming-style
const-rgx=[a-z\_][a-z0-9\_]{2,30}$
2.persistent #持久化
# Pickle collected data for later comparisons.
persistent=no
3.disable #不检查条目,输入检查项Verbose name(如invalid-name)或Project name(如C0103)
# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifiers separated by comma (,) or put this
# option multiple times (only on the command line, not in the configuration
# file where it should appear only once).You can also use "--disable=all" to
# disable everything first and then reenable specific checks. For example, if
# you want to run only the similarities checker, you can use "--disable=all
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifiers separated by comma (,) or put this
# option multiple times (only on the command line, not in the configuration
# file where it should appear only once).You can also use "--disable=all" to
# disable everything first and then reenable specific checks. For example, if
# you want to run only the similarities checker, you can use "--disable=all
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
disable=print-statement~(省略其余默认项)
使用配置文件
pylint --rcfile=pylint.conf
设置默认配置文件
修改pylint.conf文件名为.pylintrc
项目中调用pylint
1.pylint.lint.Run(等同pylint /home/workspace/main.py)
import pylint.lint
pylint_opts = ['/home/workspace/main.py']
pylint.lint.Run(pylint_opts)
2.epylint.py_run(静默方式,检查的结果只有waring级别以上)
from pylint import epylint as lint
(pylint_stdout, pylint_stderr) = lint.py_run('/home/workspace/main.py', return_std=True)
print pylint_stdout #<StringIO.StringIO instance at 0x0000000000A99188>
print pylint_stdout.getvalue() #pylint输出
项目中获取pylint检查后的对象
import pylint.lint
#输出结果为json格式
pylint_opts = ["--output-format", "json",'test/']
#分析源码,找到reporter中messages信息
OBJ = pylint.lint.Run(pylint_opts, exit=False).linter.reporter.messages
print OBJ
结果:
[{'message': 'Line too long (113/100)', 'obj': '', 'column': 0, 'path': 'test\\api.py', 'line': 10, 'type': 'convention', 'symbol': 'line-too-long', 'module': 'test.api'}]