Python DeBug工具和静态语法检查工具:
先附上Google代码风格:
https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/
一、Debug工具
-
PySnooper:
一个极简DeBug工具
平时python Debug大家一般都是print的方法来获取某个位置的输出或者某些变量的属性等。
而 PySnooper 让你能快速地获得这些信息,且相比之下它不需要细致地写 print 函数,只需要向感兴趣的函数增加一个装饰器就行了。我们会得到该函数的详细 log,包含哪行代码能运行、什么时候运行以及本地变量变化的确切时间。 -
测试代码:
import pysnooper
@pysnooper.snoop()
def number_to_bits(number):
if number:
bits = []
while number:
number, remainder = divmod(number, 2)
bits.insert(0, remainder)
return bits
else:
return [0]
number_to_bits(6)
- 使用方法:
>$ python debug.py
Starting var:.. number = 6
16:28:18.100535 call 4 def number_tobit(number):
16:28:18.161417 line 5 if number:
16:28:18.161486 line 6 bits=[]
New var:....... bits = []
16:28:18.161531 line 7 number,remainder=divmod(number,2)
Modified var:.. number = 3
New var:....... remainder = 0
16:28:18.162816 line 8 bits.insert(0,remainder)
Modified var:.. bits = [0]
16:28:18.162859 line 9 return bits
16:28:18.162886 return 9 return bits
Return value:.. [0]
- 其他特性:
标准错误输出难以获得,或者太长可以将输出定位到本地文件:
@pysnooper.snoop(’/my/log/file.log’)
查看一些非本地变量的值:
@pysnooper.snoop(variables=(‘foo.bar’, ‘self.whatever’))
展示函数中调用函数的 snoop 行:
@pysnooper.snoop(depth=2)
将所有 snoop 行以某个前缀开始,更容易定位和找到:
@pysnooper.snoop(prefix='ZZZ ')
二、代码检查工具
- 测试代码 debug.py :
import pysnooper
import cv2
@pysnooper.snoop()
def number_tobit(number):
if number:
bits=[]
number,remainder=divmod(number,2)
bits.insert(0,remainder)
return bits
else:
return [0]
c=1
number_tobit(6)
1.pycodestyle
为了帮助开发者统一代码风格,Python 社区提出了 PEP8 代码编码风格,它并没有强制要求大家必须遵循,Python 官方同时推出了一个检查代码风格是否符合 PEP8 的工具,名字也叫 pep8,后来被重命名为了 pycodestyle.
- 安装:
$ pip install pep8
- 错误代码:
错误码含义
E…:错误
W…:警告
100 型:缩进问题
200 型:空格问题
300 型:空行问题
400 型:导入问题
500 型:行长度问题
600 型:已弃用
700 型:声明问题
900 型:语法错误
- 使用方式:
$ pycodestyle debug.py
debug.py:1:7: E271 multiple spaces after keyword
debug.py:6:13: E225 missing whitespace around operator
debug.py:7:15: E231 missing whitespace after ','
debug.py:7:25: E225 missing whitespace around operator
debug.py:7:39: E231 missing whitespace after ','
debug.py:8:22: E231 missing whitespace after ','
debug.py:12:2: E901 IndentationError: unindent does not match any outer indentation level
- 一些其他参数:
--statistics -qq :对结果进行汇总
--show-source:更详细的输出
--ignore:忽略指定输出
使用方法:
$ pycodestyle debug.py --statistics -qq
$ pycodestyle debug.py --show-source
$ pycodestyle debug.py --ignore=E225,E501
2.Pylint
Pylint是一个Python代码分析工具,它分析Python代码中的错误,查找不符合代码风格标准和有潜在问题代码,是一个可以用于验证多个文件的模块和包的工具,运行后会给代码打分。
- 安装:
$ pip install pylint
- 错误代码:
C:惯例,违反了编码风格标准
R:重构,代码很差,需要重构
W:警告,某些 Python 特定的问题
E:错误,很可能是代码中的错误
F:致命错误,阻止 Pylint 进一步运行的错误
- 使用方式:
$ pylint debug.py
************* Module debug
debug.py:12:0: E0001: unindent does not match any outer indentation level (<unknown>, line 12) (syntax-error)
3.Pyflakes
分析程序并且检查各种错误。通过解析源文件实现,无需导入,因此在模块中使用是安全的,没有任何的副作用。
不会检查代码风格,并且它会单独检查各个文件,比较快,但是检测范围会有一定的局限。
- 安装:
$ pip install pyflakes
- 使用
$ pyflakes debug.py
debug.py:12:5: unindent does not match any outer indentation level
c=1
^
4.flake8
由Python 官方发布的一款辅助检测 Python 代码是否规范的工具。
Flake8 是对下面三个工具的封装:
1)PyFlakes:静态检查 Python 代码逻辑错误的工具。
2)Pep8: 静态检查 PEP8 编码风格的工具。
3)NedBatchelder’s McCabe :静态分析 Python 代码复杂度的工具。
所以Flake8 检查规则更灵活,而且还支持集成额外插件,扩展性更强。
- 安装:
$ pip install flake8
- 使用:
$ flake8 debug.py
# 由于和自己python环境有冲突,所以我没有安装成功,大家可以自己试一下...