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

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

-T,--argsused    未使用的方法/函数的关键字,默认为on

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

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

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

或者直接在代码中定义:

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值