设为首页 加入收藏

TOP

PyChecker:Python代码静态分析工具
2014-11-23 23:22:50 来源: 作者: 【 】 浏览:8
Tags:PyChecker:Python 代码 静态 分析 工具

1 概述


PyChecker是Python代码的静态分析工具,它能够帮助查找Python代码的bug,而且能够对代码的复杂度和格式等提出警告。


PyChecker可以工作在多种方式之下。首先,PyChecker会导入所检查文件中包含的模块,检查导入是否正确,同时检查文件中的函数、类和方法等。


推荐阅读:


Ubuntu 14.04安装Python 3.3.5


PyChecker可以检查出来的问题有如下几种:


2 使用


--------------------------------------------------------------------------------


从官网下载最新版本的PyChecker之后,解压安装即可:python setup.py install


首先可以在解压后的目录中测试一番:


[root@rango pychecker-0.8.19]# pychecker setup.py


Processing module setup (setup.py)...


Warnings...


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


Warnings...


None


参数和选项说明:pychecker [options] file1.py file2.py ...


--only 只给出命令行的文件的警告,默认为no


-#,--limit 显示的最大警告数,默认为10


--no-shadowbuiltin 检查是否有变量覆盖了内建变量,默认为off


-q,--stdlib 忽略标准库的文件的警告,默认为off


修改默认配置和行为:.pycheckrc文件,该文件放置在$HOME目录下,--rcfile选项可以生成一份默认的配置文件。


要禁止一些模块/函数/类/方法的警告信息,可以在.pycheckrc文件中定义一个禁止字典,键类似:


‘module’,‘module.function’,'module.class'等。


或者直接在代码中定义:


__pychecker__ = 'no-namedargs maxreturns=0 unsednames=foo,bar'


其中__pychecker__格式的值和在禁止字典中的值是一样的


在代码文件中导入PyChecker模块及使用:


import pychecker.checker


这将会检查所有在PyChecker之后导入的模块,之前的不检查。


如果不能传递命令行参数,可以使用:


os.environ['PYCHECKER'] = 'command line options here'


等价于在shell环境中设置PYCHECKER:


PYCHECKER='no-namedargs maxreturns=0' /path/to/your/program


要关闭警告,可以在导入PyChecker之前,加上:


os.environ['PYCHECKER_DISABLED'] = 1


等价于在shell环境中设置PYCHECKER_DISABLED:


PYCHECKER_DISABLED=1 /path/to/your/program


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇二叉树的建立和遍历 下一篇Python代码分析工具:PyChecker、..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: