Python编码规范的工具----pylint
?
?
pylint是一个检测编码规范的工具.它的规范来自于某PEP文档.
[guozw@centos5 bin]$ pylint Server.py
然后它就会输出一份文档
C:154: Line too long (87/80)
?
前面的C表示提示信息的level,就像python的logging模块分为ERROR,WARNING,INFO,DEBUG等等.同样pylint将提示分为了5个level
[R] 可重构
[C] 不合乎PEP文档标准
[W] 警告
[E] 错误
[F] 致命错误
C:154: Line too long (87/80)
就表示不合乎PEP文档标准.该行有87个字符,标准规定不超过80个字符
C:? 1: Missing docstring
没有写docstring
F: 18: Unable to import 'XmlConfig'
致命错误,不能导入XmlConfig模块,当然这个就不要相信它了.它没有环境变量,自然找不到那个模块了.你自己知道导入该模块没有问题就行了.
W: 51:MyImplementation: Class has no __init__ method
警告 类没有写__init__方法
C: 53:MyImplementation.loadProjects: Invalid name "loadProjects" (should match [a-z_][a-z0-9_]{2,30}$)
方法的命名不合乎规范.按照它后面给的正则表达式来看.标准的函数(方法)命名应该”都是小写字母,至少要3个字符”
W: 53:MyImplementation.loadProjects: Unused argument 'current'
警告 第53行 没有使用的参数current.? 当然这个也不要信它. 代码中这个函数的参数为默认参数.
W: 63:MyImplementation.doGuoguan: Unused variable 'ret'
警告 没有使用过的变量
C: 52: Operator not preceded by a space
??? tasks=[]
???????? ^
操作符后面要空格 标准写法是
tasks = []
R: 59:MyImplementation.doGuoguan: Method could be a function
W:? 7: Unused import threading
W:? 4: Unused import time
警告 你导入了threading和time模块 但是没有使用它们
C: 65:MyImplementation.sendMsg: Comma not followed by a space
def sendMsg(self,lotid,expect,openCode,current=None):
逗号后面要跟空格 标准写法应该是
def sendMsg(self, lotid, expect, openCode, current=None):
Statistics by type
------------------
?
+---------+-------+-----------+-----------+------------+---------+
|type???? |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module?? |1????? |NC???????? |NC???????? |0.00??????? |0.00???? |
+---------+-------+-----------+-----------+------------+---------+
|class??? |2????? |NC???????? |NC???????? |0.00??????? |0.00???? |
+---------+-------+-----------+-----------+------------+---------+
|method?? |10???? |NC???????? |NC???????? |10.00?????? |80.00??? |
+---------+-------+-----------+-----------+------------+---------+
|function |3????? |NC???????? |NC???????? |0.00??????? |0.00???? |
+---------+-------+-----------+-----------+------------+---------+
统计:你的这个py文件中有
1个module,
2个class,
10个method ,
3个function
Raw metrics
-----------
?
+----------+-------+------+---------+-----------+
|type????? |number |%???? |previous |difference |
+==========+=======+======+=========+===========+
|code????? |135??? |95.74 |NC?????? |NC???????? |
+----------+-------+------+---------+-----------+
|docstring |0????? |0.00? |NC?????? |NC???????? |
+----------+-------+------+---------+-----------+
|comment?? |3????? |2.13? |NC?????? |NC???????? |
+----------+-------+------+---------+-----------+
|empty???? |3????? |2.13? |NC?????? |NC???????? |
+----------+-------+------+---------+-----------+
代码行数:135行,占所有行数的95.74%(这个比例太高了,说明这份py文件的注释做的不够好)
Docstring行数 0行.
注释行数: 13行? ?占所有行数的2.13%
空行行数: 3行? ?占所有护士的2.13%
Global evaluation
-----------------
Your code has been rated at 5.09/10
最后有个总体评分:
满分10分,这份py文件拿了5.09分.
最后给出一份得9.31分的py文件(ps:这是一个用多线程Condition信号量实现的生产者消费者问题)
#coding:gbk
'''
条件变量测试代码
'''
import threading
import time
?
PRODUCT = None
?
_CONDITION_ = threading.Condition()
?
def produce():
??? '''生产者'''
??? global PRODUCT
??? if _CONDITION_.acquire():
??????? while True:
??????????? if PRODUCT is None:
??? ????????????print 'produce...'
??????????????? PRODUCT = 'anything'
?
??????????????? #通知消费者,商品已经生产了
??????????????? _CONDITION_.notify()
?
??????????? #等待通知
??????????? _CONDITION_.wait()
??????????? time.sleep(2)
?
def consume():
??? '''消费者'''
??? global PRODUCT
??? if _CONDITION_.acquire():
??????? while True:
??????????? if PRODUCT is not None:
??????????????? print 'consume...'
??????????????? PRODUCT = None
?
??????????????? #通知生产者,商品已经被我吃了
??????????????? _CONDITION_.notify()
?
??????????? #等待通知
???????? ???_CONDITION_.wait()
??????????? time.sleep(2)
?
THREAD_ = threading.Thread(target = produce)
THREAD_2 = threading.Thread(target = consume)
?
THREAD_.start()
THREAD_2.start()
1 楼 simomo 2011-02-02 下来试试~