Python_检查程序规范
'''
检查Python程序的一些基本规范,例如,运算符两测是否有空格,是否每次只导入一个模块,在不同的功能模块之间是否有空行,注释是否够多,等等
'''
import sys
import re def checkFormats(lines,desFileName):
fp=open(desFileName,'w')
for i, line in enumerate(lines):
print('='*30)
print('Line:',i+1)
if line.strip().startwith('#'):
print(' '*10+'Comments.Pass.')
fp.write(line)
continue
flag=True
#check operator symbols
symbols=[',','+','-','*','/','//','**','>>','<<','+=','-=','*=','/=']
temp_line=line
for symbol in symbols:
pattern=re.compile(r'\s*'+re.escape(symbol)+r'\s*')
temp_lie=pattern.split(temp_line)
sep=' '+symbol+' '
temp_line=sep.join(temp_line)
if line !=temp_line:
flag=False
print(' '*10+'You may miss some blank spaces in this line.' )
#check import statement
if line.strip().startwith('import'):
if ',' in line:
flag = False
print(' '*10+"You'd bbetter import one module at a time.")
temp_line=line.strip()
modules=modules.strip()
pattern=re.compile(r'\s*,\s*')
modules=pattern.split(modules)
temp_line=''
for module in modules:
temp_line +=line[:line.index('import')] + 'import '+module+'\n'
line=temp_line
pri_line=lines[i-1].strip()
if pri_line and(not pri_line.startwith('import'))and (not pri_line.startwith('#')):
falg=False
print(' '*10+'You should add a blank line before this line.')
line='\n'+line
after_line=lines[i+1].strip()
if after_line and(not after_line.startwith('import')):
flag=False
print(' '*10+'You should add a blank line after this line.')
line=line+'\n'
#check if there is a blank line before new funtional code block
#including the class/function definition
if line.strip() and not line.startswith(' ')and i>0:
pri_line=lines[i-1]
if pri_line.strip() and pri_line.startwith(' '):
flag=False
print(' '*10 +"You'd better add a blank line before this line.")
line='\n'+line
if flag:
print(' '*10+'Pass.')
fp.write(line)
fp.close() if __name__ == '__main__':
fileName=sys.argv[1] #命令行参数
fileLines=[]
with open(fileName,'r') as fp:
fileLines=fp.readline()
desFileName=fileName[:-3]+'_new.py'
checkFormats(fileLines,desFileName)
#check the ratio of comment lines to all lines
comments=[line for line in fileLines if line.strip().startswith('#')]
ratio=len(comments)/len(fileLines)
if ratio <= 0.3:
print('='*30)
print('Comments in the file is less than 30%')
print('Perhaps you should add some comments at appropriate position.')
Python_检查程序规范的更多相关文章
- python_开发规范
对于python有哪些开发规范? 1. 每行代码不超过80字符 2. 不要在逗号, 分号, 冒号前加空格, 应该之后加空格 3. 列表, 索引,切片的左括号前不加空格 4. 比较运算前后 加一个空格 ...
- python_编程规范
缩进 4个"空格"作为一个缩进层次,永远不要使用"制表位" 空格 运算符两边放置一个空格 命名 模块名:模块应该是不含下画线的.简短的.小写的名字. 类名: ...
- Discuz! 的编码规范
http://open.discuz.net/?ac=document&page=dev_coderule 前言 本规范由编程原则组成,融合并提炼了开发人员长时间积累下来的成熟经验,意在帮助形 ...
- 中兴软件编程规范C/C++
Q/ZX 深圳市中兴通讯股份有限公司企业标准 (设计技术标准) Q/ZX 04.302.1–2003 软件编程规范C/C++ 20 ...
- (转)C++ 编程规范
转载地址:http://www.cnblogs.com/len3d/archive/2008/02/01/1061902.html C/C++编码规范 今天人们越来越明白软件设计更多地是一种工程,而不 ...
- Java初认识--Java语言的书写规范及基本的运算符
一.Java中名称的规范和书写程序的规范. 1.Java中的名称规范: (1)包名全是小写:xxyyzz: (2)类名接口名:首字母大写:XxxYyy: (3)变量名和函数名:变量名不能是关键字:多单 ...
- C语言编程规范
C语言编程规范 6 函数与过程 6.1 函数的功能与规模设计 函数应当短而精美,而且只做一件事.不要设计多用途面面俱到的函数,多功能集于一身的函数,很可能使函数的理解.测试.维护等变得困难. 6.2 ...
- C/C++编码规范
C/C++编码规范 今天人们越来越明白软件设计更多地是一种工程,而不是一种个人艺术.由于大型产品的开发通常由很多的人协同作战,如果不统一编程规范,最终合到一起的程序,其可读性将较差,这不仅给代码的理解 ...
- Discuz代码研究-编码规范
Discuz中的编码规范很值得PHP开发人员借鉴.里面既介绍了编码时代码标记,注释,书写规则,命名原则等方面基础的内容,对代码的安全性,性能,兼容性,代码重用,数据库设计,数据库性能及优化作了阐述,这 ...
随机推荐
- 关于对数组和指针的测试与分析OC
前言: 这个笔试题想必很多小伙伴都很面熟把,差不多10个人有7个人不会做这道笔试题,或许有知道答案的,但是仅仅知道答案,心里还是一头雾水.如果你真的不会那就请认真看完本文学习一下吧! 错误想法: 有的 ...
- 基于Retrofit2.0+RxJava+Dragger2实现不一样的Android网络构架搭建(转载)
转载请注明出处:http://blog.csdn.net/finddreams/article/details/50849385#0-qzone-1-61707-d020d2d2a4e8d1a374a ...
- C++异常处理 - 栈解旋,异常接口声明,异常类型和异常变量的生命周期
栈解旋(unwinding) 异常被抛出后,从进入try块起,到异常被抛掷前,这期间在栈上的构造的所有对象,都会被自动析构.析构的顺序与构造的顺序相反.这一过程称为栈的解旋(unwinding). d ...
- (七)大图展示Demo引出的UIScrollView的使用
UIScrollView是一个能够滚动的视图控件,可以通过滚动查看所有内容. 用途: 1.一张大图屏幕放不下,可以用各个方向的手势来看大图的各个部分. 2.手机的设置页面有很多的选项,需要上下滚动来查 ...
- Using PL/SQL APIs as Web Services
Overview Oracle E-Business Suite Integrated SOA Gateway allows you to use PL/SQL application program ...
- 【Android 应用开发】Android - 时间 日期相关组件
源码下载地址 : -- CSDN : http://download.csdn.net/detail/han1202012/6856737 -- GitHub : https://github.co ...
- 滑动UITableViewCell出现多个按钮
iOS > = 5.0使用第三方效果图 iOS> = 8.0使用系统方法效果图 MGSwipeTableCell(Github上的三方库)- iOS >= 5.0 直接使用比较简单 ...
- cocos2d-x项目与vs2013编译
cocos2d-x项目与vs2013编译 2014-12-17 cheungmine 因为C++11引入了众多开源软件的特性,导致cocos2d-x r3.3项目无法用 vs2010编译. 所以安装了 ...
- 增量会话对象——DeltaSession
在集群环境中为了使集群中各个节点的会话状态都同步,同步操作是集群重点解决的问题,一般来说有两种同步策略,其一是每次同步都把整个会话对象传给集群中其他节点,其他节点更新整个会话对象:其二是对会话中增量修 ...
- #pragma comment(转)
此文转自微软MSDN.注意这是在Windows上才有的,Linux上可没有. #pragma comment( comment-type [,"commentstring"] ) ...