一份好的代码一定要有很好的可读性,所以在实际开发过程中,注释要占源码一定的比例,才是一份合格的代码,我这里写了一个脚本,可以用来检测同一目录下,所有python源码的注释比例(将下面的脚本放到源码目录下执行即可)
import os
import sys
import re
"""
author: zhaozhao
"""
"""
本程序通过计算python源码中"注释的行数",检查源码是否合格
1.本程序可对"#"开头的注释,以及 成对的"三双引号"源码进行计数
2.本程序使用了正则匹配
3.注释占源码比例 = 源码内注释行数/源码总行数
"""
# 获取代码文件所在路径
dir_paths = os.listdir(os.getcwd())
#print(dir_paths)
#dir_path = os.getcwd() + "/" +sys.argv[0]
dir_path = ""
for dir in dir_paths:
#print("=="*10)
#print (dir)
if re.match(r".*py$", dir):
dir_path_pure = dir
#print ("--->",dir_path)
dir_path = os.getcwd() + "/" +dir_path_pure
code_sum = 0
with open(dir_path, "r") as my_code:
my_lines = my_code.readlines()
serial_num_list = [[],[]]
for serial_num, line in enumerate(my_lines):
serial_num = serial_num + 1
if re.match(r"\s*#+.*",line):
serial_num_list[0].append(serial_num)
if re.match(r".*\"\"\".*", line):
serial_num_list[1].append(serial_num)
code_sum += 1
serial_num_sum1 = 0
serial_num_sum2 = 0
for ser in serial_num_list[0]:
serial_num_sum1 +=1
try:
for ser_num, value in enumerate(serial_num_list[1]):
if ser_num%2 == 0:
top_num = value
else:
end_num = value
serial_num_sum2 += (int(end_num) - int(top_num) + 1)
except:
print("%s源码注释不规范!自动跳过!"%dir_path_pure)
continue
serial_num_sum = serial_num_sum1 + serial_num_sum2
exp_rate = 100*(serial_num_sum/code_sum)
print ("%s| 注释的行数为:%d,总行数为%d | 注释率为%d%%"%(dir_path_pure,serial_num_sum, code_sum,exp_rate))