Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / PyChecker:Python代码静态分析工具

1 概述PyChecker是Python代码的静态分析工具,它能够帮助查找Python代码的bug,而且能够对代码的复杂度和格式等提出警告。PyChecker可以工作在多种方式之下。首先,PyChecker会导入所检查文件中包含的模块,检查导入是否正确,同时检查文件中的函数、类和方法等。推荐阅读:《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htmPython脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htmUbuntu 14.04安装Python 3.3.5 PyChecker可以检查出来的问题有如下几种:
  • 全局量没有找到,比如没有导入模块
  • 传递给函数、方法、构造器的参数数目错误
  • 传递给内建函数和方法的参数数目错误
  • 字符串格式化信息不匹配
  • 使用不存在的类方法和属性
  • 覆盖函数时改变了签名
  • 在同一作用域中重定义了函数、类、方法
  • 使用未初始化的变量
  • 方法的第一个参数不是self
  • 未使用的全局量和本地量(模块或变量)
  • 未使用的函数/方法的参数(不包括self)
  • 模块、类、函数和方法中没有docstring
2 使用--------------------------------------------------------------------------------从官网下载最新版本的PyChecker之后,解压安装即可:python setup.py install首先可以在解压后的目录中测试一番:[root@rango pychecker-0.8.19]# pychecker setup.pyProcessing 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) found19 errors suppressed, use -#/--limit to increase the number of errors displayed可以看到,检查的结果将setup.py依赖的一些文件中的语法错误或者警告都列举出来了,使用--only参数可以只检查自身的语法问题:[root@rango pychecker-0.8.19]# pychecker --only setup.pyProcessing module setup (setup.py)...Warnings...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之后导入的模块,之前的不检查。如果不能传递命令行参数,可以使用: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更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-05/101612p2.htm