28. Python脚本学习笔记二十八代码检测和分析
本篇名言:“人生就象睡觉时盖了一条太短的毛毯,你往上拉,脚趾头抗议,往下拽,肩膀冻得哆嗦.只有乐观的人才会蜷起膝,美美地睡一晚.愿你天天快乐,夜夜好梦!”
除了直接测试外,其实还有其他工具来探索程序,用于代码检测和分析等。
代码检测是寻找代码中普通错误或者问题的方法,有点像编译器处理静态语言。
分析则是查明程序到底跑多快的方法。
单元测试让程序工作,源代码检查可以让程序更好,分析会让程序更快。
1. PyChecker和PyLint
两个工具都是检查源代码。
1.1 Pylint下载
Pylint 处理pylint包之外,还需要astroid和logilab-common
http://www.logilab.org/projects/common
https://bitbucket.org/logilab/astroid
https://pypi.python.org/pypi/pylint/#downloads
蛤蟆已打包一并传到网上,链接如下:
http://download.csdn.net/detail/notbaron/8928061
解压后直接通过命令:python setup.py install
安装。
1.2 Pychecker下载安装
下载地址如下:http://pychecker.sourceforge.net/
执行如下检测:
E:\>pychecker my_math
E:\python_zhizuo\Pythonlearn>D:\python27\python.exeD:\python27\Lib\site-package
s\pychecker\checker.py my_math
Processing module my_math (my_math)...
Warnings...
None
E:\>pylint my_math
No config file found, using defaultconfiguration
************* Module my_math
C: 4, 0: Exactly one space required around assignment
__revision__='0.1'
^ (bad-whitespace)
C: 5, 0: Exactly one space required after comma
def product(factor1,factor2):
^ (bad-whitespace)
C: 7, 0: Final newline missing (missing-final-newline)
Report
======
4 statements analysed.
Statistics by type
------------------
+---------+-------+-----------+-----------+------------+---------+
|type |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module |1 |1 |= |100.00 |0.00 |
+---------+-------+-----------+-----------+------------+---------+
|class |0 |0 |= |0 |0 |
+---------+-------+-----------+-----------+------------+---------+
|method |0 |0 |= |0 |0 |
+---------+-------+-----------+-----------+------------+---------+
|function |1 |1 |= |100.00 |0.00 |
+---------+-------+-----------+-----------+------------+---------+
Raw metrics
-----------
+----------+-------+------+---------+-----------+
|type |number |% |previous|difference |
+==========+=======+======+=========+===========+
|code |4 |44.44 |NC |NC |
+----------+-------+------+---------+-----------+
|docstring |4 |44.44 |NC |NC |
+----------+-------+------+---------+-----------+
|comment |0 |0.00 |NC |NC |
+----------+-------+------+---------+-----------+
|empty |1 |11.11 |NC |NC |
+----------+-------+------+---------+-----------+
Duplication
-----------
+-------------------------+------+---------+-----------+
| |now |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines |0 |0 |= |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000|0.000 |= |
+-------------------------+------+---------+-----------+
Messages by category
--------------------
+-----------+-------+---------+-----------+
|type |number |previous |difference |
+===========+=======+=========+===========+
|convention |3 |3 |= |
+-----------+-------+---------+-----------+
|refactor |0 |0 |= |
+-----------+-------+---------+-----------+
|warning |0 |0 |= |
+-----------+-------+---------+-----------+
|error |0 |0 |= |
+-----------+-------+---------+-----------+
Messages
--------
+----------------------+------------+
|message id |occurrences |
+======================+============+
|bad-whitespace |2 |
+----------------------+------------+
|missing-final-newline |1 |
+----------------------+------------+
Global evaluation
-----------------
Your code has been rated at 2.50/10
其中my_math.py文件如下:
""""
A simple math module.
"""
__revision__='0.1'
def product(factor1,factor2):
'The product of two numbers'
return factor1 * factor2
当然就算有pychecker和pylint我们也需要进行手动调式,手动调试是建立在从程序的特殊情况中总结出的知识的基础上的。
2. 分析
在试图让代码提速前,有个非常重要的规则需要注意:
不成熟的优化是万恶之源。--Donald Knuth
此外还有KISS原则,KISS=KeepIt Small and Simple.
如果必须进行优化,那么绝对要在优化之前进行分析。
标准库包含了一个叫做profile的分析模块。
例如代码如下:
import profile
from my_math import product
profile.run('product(1,2)')
其中my_math.py文件内容如下:
""""
A simple math module.
"""
__revision__='0.1'
def product(factor1,factor2):
'The product of two numbers'
return factor1 *factor2
最后输出如下:
4 function calls in 0.031 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.031 0.031 0.031 0.031 :0(setprofile)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 my_math.py:5(product)
1 0.000 0.000 0.031 0.031 profile:0(product(1,2))
0 0.000 0.000 profile:0(profiler)