python代码解读软件_Python代码分析⼯具:PyCheck
1 概述
PyChecker是Python代码的静态分析⼯具,它能够帮助查Python代码的bug,⽽且能够对代码的复杂度和格式等提出警告。
PyChecker可以⼯作在多种⽅式之下。⾸先,PyChecker会导⼊所检查⽂件中包含的模块,检查导⼊是否正确,同时检查⽂件中的函数、类和⽅法等。
PyChecker可以检查出来的问题有如下⼏种:全局量没有到,⽐如没有导⼊模块
传递给函数、⽅法、构造器的参数数⽬错误
传递给内建函数和⽅法的参数数⽬错误
字符串格式化信息不匹配
使⽤不存在的类⽅法和属性
覆盖函数时改变了签名
在同⼀作⽤域中重定义了函数、类、⽅法
使⽤未初始化的变量
⽅法的第⼀个参数不是self
未使⽤的全局量和本地量(模块或变量)
未使⽤的函数/⽅法的参数(不包括self)
模块、类、函数和⽅法中没有docstring
2 使⽤
从官⽹下载最新版本的PyChecker之后,解压安装即可:python setup.py install
⾸先可以在解压后的⽬录中测试⼀番:
[root@rango pychecker-0.8.19]# pychecker setup.py
Processing module setup (setup.py)...
<
[system path]/distutils/command/bdist_wininst.py:271: Statement appears to have no effect
[system path]/distutils/command/build_scripts.py:80: No class attribute (dry_run) found
[system path]/distutils/command/build_scripts.py:97: No class attribute (dry_run) found
[system path]/distutils/command/build_scripts.py:120: (file) shadows builtin
[system path]/distutils/command/build_scripts.py:121: No class attribute (dry_run) found
[system path]/distutils/command/install_data.py:62: (dir) shadows builtin
[system path]/distutils/command/install_data.py:64: (dir) shadows builtin
[system path]/distutils/command/install_data.py:66: (dir) shadows builtin
[system path]/distutils/command/install_scripts.py:52: (file) shadows builtin
[system path]/distutils/command/install_scripts.py:53: No class attribute (dry_run) found
19 errors suppressed, use -#/--limit to increase the number of errors displayed
可以看到,检查的结果将setup.py依赖的⼀些⽂件中的语法错误或者警告都列举出来了,使⽤--only参数可以只检查⾃⾝的语法问题:
[root@rango pychecker-0.8.19]# pychecker --only setup.py
Processing module setup (setup.py)...
<
None
参数和选项说明:pychecker [options] file1.py file2.py ...
--only        只给出命令⾏的⽂件的警告,默认为no
-#,--limit    显⽰的最⼤警告数,默认为10
--no-shadowbuiltin    检查是否有变量覆盖了内建变量,默认为off
-q,--stdlib        忽略标准库的⽂件的警告,默认为off
-T,--argsused    未使⽤的⽅法/函数的关键字,默认为on
修改默认配置和⾏为:.pycheckrc⽂件,该⽂件放置在$HOME⽬录下,--rcfile选项可以⽣成⼀份默认的配置⽂件。
要禁⽌⼀些模块/函数/类/⽅法的警告信息,可以在.pycheckrc⽂件中定义⼀个禁⽌字典,键类似:
‘module’,‘module.function’,'module.class'等。
或者直接在代码中定义:
__pychecker__ = 'no-namedargs maxreturns=0 unsednames=foo,bar'
其中__pychecker__格式的值和在禁⽌字典中的值是⼀样的
在代码⽂件中导⼊PyChecker模块及使⽤:
import pychecker.checker
这将会检查所有在PyChecker之后导⼊的模块,之前的不检查。
如果不能传递命令⾏参数,可以使⽤:
等价于在shell环境中设置PYCHECKER:
PYCHECKER='no-namedargs maxreturns=0' /path/to/your/program
要关闭警告,可以在导⼊PyChecker之前,加上:
等价于在shell环境中设置PYCHECKER_DISABLED:
PYCHECKER_DISABLED=1 /path/to/your/program
3 Pylint
相⽐于PyChecker,Pylint是⼀个⾼阶的Python代码分析⼯具,它分析Python代码中的错误,查不符合代码风格标准(Pylint 默认使⽤的代码风格是 PEP 8)和有潜在问题的代码。⽬前 Pylint 的最新版本是 pylint-1.2.1。可以检查⼀⾏代码的长度、变量名是否符合规范等。运⾏两次可以看出代码是否改进,分数是否有所提⾼,10分满分。
3.1 安装Pylint
从官⽹下载最新版本,解压之后,执⾏:python setup.py install,安装完毕
3.2 使⽤Pylint
命令⾏:pylint [options] module_or_package
图形化:pylint-gui
注解:pylint-gui依赖tkinter,安装tkinter:yum install -y tkinter
3.3 输出信息
Message_Type
(C) convention惯例。违反了编码风格标准
(R) refactor重构。写得⾮常糟糕的代码。
(W) warning警告。某些 Python 特定的问题。
(E) error错误。很可能是代码中的错误。
(F) 致命错误。阻⽌ Pylint 进⼀步运⾏的错误。
Report
report报告⽤来统计⼀些message类型的数量,模块的依赖等。检查module的数量,每个module错误和警告所占百分⽐Global evaluation
代码评分
Global evaluation
-----------------
Your code has been rated at 7.98/10
⽰例:检查pylint-1.2.1⽬录下的setup.py⽂件:pylint setup.py
No config file found, using default configuration
************* Module pylint-1.2.1.setup
I:  3, 0: Locally disabling reimported (W0404) (locally-disabled)
I:  3, 0: Locally disabling redefined-builtin (W0622) (locally-disabled)
I:  3, 0: Locally disabling pointless-except (W0704) (locally-disabled)
I:  3, 0: Locally disabling unused-argument (W0613) (locally-disabled)
I: 42, 0: Locally disabling no-name-in-module (E0611) (locally-disabled)
C: 11, 0: Line too long (81/80) (line-too-long)
C:179, 0: Wrong hanging indentation.
'pylint = pylint:run_pylint',
|  ^ (bad-continuation)
C:180, 0: Wrong hanging indentation.
'pylint-gui = pylint:run_pylint_gui',
|  ^ (bad-continuation)
C:181, 0: Wrong hanging indentation.
'epylint = pylint:run_epylint',
|  ^ (bad-continuation)
C:182, 0: Wrong hanging indentation.
'pyreverse = pylint:run_pyreverse',
|  ^ (bad-continuation)
C:183, 0: Wrong hanging indentation.
'symilar = pylint:run_symilar',
|  ^ (bad-continuation)
C:184, 0: Wrong hanging indentation.
]
}
|  |  ^ (bad-continuation)
C:199, 0: Wrong continued indentation.
'build_py': build_py},
| ^ (bad-continuation)
C:202, 0: No space allowed before :
if __name__ == '__main__' :
^ (bad-whitespace)
F: 54, 0: Unable to import '__pkginfo__' (import-error)
C: 57, 0: Invalid constant name "distname" (invalid-name)
C: 58, 0: Invalid constant name "scripts" (invalid-name)
C: 59, 0: Invalid constant name "data_files" (invalid-name)
C: 60, 0: Invalid constant name "subpackage_of" (invalid-name)
C: 61, 0: Invalid constant name "include_dirs" (invalid-name)
C: 62, 0: Invalid constant name "ext_modules" (invalid-name)
C: 63, 0: Invalid constant name "install_requires" (invalid-name)
C: 64, 0: Invalid constant name "dependency_links" (invalid-name)
C: 71, 4: Invalid constant name "long_description" (invalid-name)
C: 73, 4: Invalid constant name "long_description" (invalid-name)
C:139,24: Invalid variable name "n" (invalid-name)
能运行python的软件W:138,30: Unused variable 'dirnames' (unused-variable)
R:105, 0: Too many public methods (32/20) (too-many-public-methods) Report
======
109 statements analysed.
Statistics by type
------------------
+---------+-------+-----------+-----------+------------+---------+
|type    |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+ |module  |1      |1          |=          |100.00      |0.00    |
+---------+-------+-----------+-----------+------------+---------+
|class    |1      |1          |=          |100.00      |0.00    |
+---------+-------+-----------+-----------+------------+---------+
|method  |2      |2          |=          |100.00      |0.00    |
+---------+-------+-----------+-----------+------------+---------+
|function |3      |3          |=          |100.00      |0.00    |
+---------+-------+-----------+-----------+------------+---------+
Duplication
-----------
+-------------------------+------+---------+-----------+
|                        |now  |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines      |0    |0        |=          |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |0.000    |=          |
+-------------------------+------+---------+-----------+
Messages by category
--------------------
+-----------+-------+---------+-----------+
|type      |number |previous |difference |
+===========+=======+=========+===========+
|convention |20    |20      |=          |
+-----------+-------+---------+-----------+
|refactor  |1      |1        |=          |
+-----------+-------+---------+-----------+
|warning    |1      |1        |=          |
+-----------+-------+---------+-----------+
|error      |0      |0        |=          |
+-----------+-------+---------+-----------+

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。