'''
检查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_检查程序规范的更多相关文章

  1. python_开发规范

    对于python有哪些开发规范? 1. 每行代码不超过80字符 2. 不要在逗号, 分号, 冒号前加空格, 应该之后加空格 3. 列表, 索引,切片的左括号前不加空格 4. 比较运算前后 加一个空格 ...

  2. python_编程规范

    缩进 4个"空格"作为一个缩进层次,永远不要使用"制表位" 空格 运算符两边放置一个空格 命名  模块名:模块应该是不含下画线的.简短的.小写的名字.  类名: ...

  3. Discuz! 的编码规范

    http://open.discuz.net/?ac=document&page=dev_coderule 前言 本规范由编程原则组成,融合并提炼了开发人员长时间积累下来的成熟经验,意在帮助形 ...

  4. 中兴软件编程规范C/C++

    Q/ZX 深圳市中兴通讯股份有限公司企业标准 (设计技术标准) Q/ZX 04.302.1–2003      软件编程规范C/C++                               20 ...

  5. (转)C++ 编程规范

    转载地址:http://www.cnblogs.com/len3d/archive/2008/02/01/1061902.html C/C++编码规范 今天人们越来越明白软件设计更多地是一种工程,而不 ...

  6. Java初认识--Java语言的书写规范及基本的运算符

    一.Java中名称的规范和书写程序的规范. 1.Java中的名称规范: (1)包名全是小写:xxyyzz: (2)类名接口名:首字母大写:XxxYyy: (3)变量名和函数名:变量名不能是关键字:多单 ...

  7. C语言编程规范

    C语言编程规范 6 函数与过程 6.1 函数的功能与规模设计 函数应当短而精美,而且只做一件事.不要设计多用途面面俱到的函数,多功能集于一身的函数,很可能使函数的理解.测试.维护等变得困难. 6.2 ...

  8. C/C++编码规范

    C/C++编码规范 今天人们越来越明白软件设计更多地是一种工程,而不是一种个人艺术.由于大型产品的开发通常由很多的人协同作战,如果不统一编程规范,最终合到一起的程序,其可读性将较差,这不仅给代码的理解 ...

  9. Discuz代码研究-编码规范

    Discuz中的编码规范很值得PHP开发人员借鉴.里面既介绍了编码时代码标记,注释,书写规则,命名原则等方面基础的内容,对代码的安全性,性能,兼容性,代码重用,数据库设计,数据库性能及优化作了阐述,这 ...

随机推荐

  1. python异常处理和断言

    http://blog.csdn.net/pipisorry/article/details/21841883 关于异常处理有必要么的讨论 最重要的问题是你在开发过程中隐藏了bug,如果当时你没加这个 ...

  2. Gradient Descent 梯度下降法-R实现

    梯度下降法: [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt 作为一个初学者,水平有限,欢迎交流指正. 应用:求线性回归方程的系数 目标:最小化损失 ...

  3. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

  4. centos6.5 rsync+inotify实现服务器之间文件实时同步

    1. rsync的优点与不足 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据 ...

  5. 写的还不错的专题,android性能优化

    http://www.trinea.cn/android/android-performance-demo/

  6. Android电话拦截实现以及TelephonyManager监听的取消

    由于毕业设计题目涉及到电话拦截这一块.所以鼓捣了一下.遇到了一些问题,总结一下,以免忘记,也希望能帮助其他新人们. 本篇博客目的:实现电话的拦截 会遇到的问题:android studio下AIDL的 ...

  7. AngularJS进阶(二十)HTML5实现获取地理位置信息并定位功能

    HTML5实现获取地理位置信息并定位功能 注:请点击此处进行充电! 前言 这篇文章主要介绍了HTML5实现获取地理位置信息并定位功能,本文讲解了原生HTML5.百度地图.谷歌地图等三种获取理位置信息并 ...

  8. Unix/Linux中的read和write函数

    文件描述符 对于内核而言,所有打开的文件都通过文件描述符引用.文件描述符是一个非负整数.当打开一个现有文件或创建一个新文件时,内核向进程返回一个文件描述符.当读或写一个文件时,使用open或creat ...

  9. ffdshow 源代码分析 3: 位图覆盖滤镜(设置部分Settings)

    ===================================================== ffdshow源代码分析系列文章列表: ffdshow 源代码分析 1: 整体结构 ffds ...

  10. Android ViewPager和Slidingmenu手势冲突问题

    尊重原创:  http://blog.csdn.net/sk719887916/article/details/40043961 skay 想必大家都遇到过手势和焦点的问题   对于安卓初学者或者初次 ...