
待解决问题
最近由于项目需要, 需要统计一下代码的注释率, 必须要达到30%才算合格, 于是搜呀搜, 想找一个写好的轮子, 直接拿来使用, 确实有好多脚本, 但是呢! 代码要不就是只统计#开头的, 要不就是统计#号开头的和三单双引号开头的, 其实并没有太大的问题, 只是还有一种情况, 如下:
1 2 3
| example_str = """ 这是一个示例的字符串, 并不是注释 """
|
动手解决
这种情况就不应该算注释行, 而这些脚本没有算这种情况, 所以就自己动手写了一个脚本, 可能会有bug, 欢迎指出, 直接上源码:
comment_rate.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| # -*- coding: utf-8 -*- # @Author: Mehaei # @Date: 2023-08-02 17:13:56 # @Last Modified by: Mehaei # @Last Modified time: 2023-08-02 21:05:40 import argparse import os import sys def one_file_total(fpname): """ 统计一个文件的注释率 :param fpname: 文件路径 """ with open(fpname, 'r', encoding='utf-8') as f: code_lines = 0 comment_lines = 0 blank_lines = 0 is_var = False is_comment = False for index, line in enumerate(f, start=1): line = line.strip() if "'''" in line or '"""' in line: if "=" in line: is_var = True else: if is_var: code_lines += 1 is_var = False continue else: if is_comment: comment_lines += 1 is_comment = False continue else: is_comment = True if is_var: code_lines += 1 elif is_comment: comment_lines += 1 elif not line: blank_lines += 1 elif line.startswith("#"): comment_lines += 1 else: code_lines += 1 print("#"*50) print(fpname) print("注释: %d" % comment_lines) print("空行: %d" % blank_lines) print("代码: %d" % code_lines) program_lines = comment_lines + code_lines print("程序行数: %d" % program_lines) comment_rate = comment_lines / program_lines print("注释率: {:.2f}%".format(comment_rate * 100)) print("#"*50) # 全局统计变量 tcomment_lines = tblank_lines = tcode_lines = 0 def main(fpname): """ 统计脚本入口函数 :param fpname: 文件或文件夹路径 """ global tcomment_lines global tblank_lines global tcode_lines if os.path.isdir(fpname): for pname in os. listdir(fpname): if pname.startswith("."): continue fp = f"{fpname}/{pname}" if os.path.isdir(fp): main(fp) if os.path.isfile(fp) and fp.endswith(" .py"): comment_lines, blank_lines, code_lines = one_file_total(fp) tcomment_lines += comment_lines tblank_lines += blank_lines tcode_lines += code_lines elif os.path.isfile(fpname) and fpname.endswith(".py"): return one_file_total(fpname) else: print(f"fpname: {fpname}不是文件夹也不是py文件") if __name__ == "__main__": parser = argparse.ArgumentParser(description="文件注释率统计脚本") parser.add_argument('-p', dest='path', type=str, default="./", help="要检测的文件或文件路径") args = parser.parse_args() fpname = args.path if not os.path.exists(fpname): print(f"文件路径:{fpname}不存在") sys.exit(-1) main(fpname) if tcomment_lines or tblank_lines or tcode_lines: print("*"*50) print(fpname) print("项目总注释:%d" % tcomment_lines) print("顶目总空行:%d" % tblank_lines) print("项目总代码:%d" % tcode_lines) program_lines = tcomment_lines + tcode_lines print("项目总程序行数: %d" % program_lines) comment_rate = tcomment_lines / program_lines print("项目总备注率:{:.2f}%".format(comment_rate * 100)) print("*"*50)
|
测试及使用
测试文件: example.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| # -*- coding: utf-8 -*- # @Author: Mehaei # @Date: 2023-08-02 20:57:42 # @Last Modified by: Mehaei # @Last Modified time: 2023-08-02 20:59:28 # 导入模块 import os # 入口函数 def main(): msg = """ message info """ print(msg) print(os.getcwd()) # 调用函数 main()
|
脚本使用
帮助
python3.7 comment_rate.py –help
1 2 3 4 5 6 7 8
| ~/Downloads/web » python3.7 comment_rate.py --help usage: comment_rate.py [-h] [-p PATH] 文件注释率统计脚本 optional arguments: -h, --help show this help message and exit -p PATH 要检测的文件或文件路径
|
统计单个文件
python3.7 comment_rate.py -p “example.py”
1 2 3 4 5 6 7 8 9
| ~/Downloads/web » python3.7 comment_rate.py -p "example.py" ################################################## example.py 注释: 8 空行: 4 代码: 8 程序行数: 16 注释率: 50.00% ##################################################
|
统计文件夹下所有的py文件
python3.7 comment_rate.py -p “/test”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| ~/Downloads/web » python3.7 comment_rate.py -p "/test" ################################################## /test/mq_learn.py 注释: 5 空行: 3 代码: 1 程序行数: 6 注释率: 83.33% ################################################## ............ ################################################## /test/py_verify_code.py/local_code.py 注释: 5 空行: 3 代码: 10 程序行数: 15 注释率: 33.33% ################################################## ************************************************** /test 项目总注释:235 项目总空行:335 项目总代码:815 项目总程序行数: 1050 项目总备注率:22.38% **************************************************
|
源代码地址
代码我放在github上了, 欢迎star, 如果有bug也麻烦指出来~~
https://github.com/Mehaei/code\_count

点个在看你最好看
