代码统计工具-python版本
来源:互联网 发布:电力行业大数据应用 编辑:程序博客网 时间:2024/05/21 17:01
在网上搜了相关代码统计工具,发现统计python代码的工具貌似还没有出现,遂小试一把自己编写一版python代码统计工具。
语言:python
废话少说上代码
#coding=utf-8u"""User: Jerry.FangDate: 14-6-21"""import os# Scan all python files name and path.def _scan_file_by_type(file_path, last_dir, file_type, file_list): if last_dir == '': full_path = file_path else: full_path = file_path + '/' + last_dir if os.path.isdir(full_path): _list_dir = os.listdir(full_path) for _ele in _list_dir: _scan_file_by_type(full_path, _ele, file_type, file_list) else: if ('.' + file_type) == last_dir[-3:]: file_list.append({ 'path': file_path + '/', 'name': last_dir })def stat_py_files(project_path): # Get files list. py_file_list = list() _scan_file_by_type(project_path, '', 'py', py_file_list) # Start to statics files line. comment_num = 0 blank_num = 0 code_num = 0 for py_file in py_file_list: py_file_handle = file(py_file['path'] + '/' + py_file['name']) last_comment_header = None is_comment_start = False while True: _line = py_file_handle.readline() if _line: # comment ''' line. mark_num = _line.count("'''") if mark_num: if mark_num % 2 == 1 and (last_comment_header == "'''" or last_comment_header is None): is_comment_start = False if is_comment_start else True if is_comment_start: last_comment_header = "'''" else: last_comment_header = None comment_num += 1 continue # comment """ line. mark_num = _line.count('"""') if mark_num: if mark_num % 2 == 1 and (last_comment_header == '"""' or last_comment_header is None): is_comment_start = False if is_comment_start else True if is_comment_start: last_comment_header = '"""' else: last_comment_header = None comment_num += 1 continue # comment # line. mark_num = _line.count('#') if mark_num: comment_num += 1 continue # blank line. mark_num = _line.count(' ') + _line.count('\n') if mark_num == len(_line): blank_num += 1 continue # Code Line. if is_comment_start is False: code_num += 1 else: comment_num += 1 else: print '%s...OK' % py_file['name'] break total_line = comment_num + blank_num + code_num print '****** Result ******' print 'Total : %d' % total_line print 'Comment : %d %.2f %%' % (comment_num, float(comment_num)*100/float(total_line)) print 'Blank : %d %.2f %%' % (blank_num, float(blank_num)*100/float(total_line)) print 'Code : %d %.2f %%' % (code_num, float(code_num)*100/float(total_line)) print '********************'if __name__ == '__main__': stat_py_files('F:\personal\python\test_project') pass
其实代码统计不外乎就是2点
1、扫描当前路径下所有相关文件
2、统计时候区分 空白行、注释行和代码行
同理,举一反三可以写出Javascript代码统计方法:
def stat_js_files(project_path): # Get files list. py_file_list = list() _scan_file_by_type(project_path, '', 'js', py_file_list) # Start to statics files line. comment_num = 0 blank_num = 0 code_num = 0 for py_file in py_file_list: py_file_handle = file(py_file['path'] + '/' + py_file['name']) is_comment_start = False while True: _line = py_file_handle.readline() if _line: # comment /* line. mark_num = _line.count("/*") if mark_num: is_comment_start = True comment_num += 1 continue # comment */ line. mark_num = _line.count("*/") if mark_num: is_comment_start = False comment_num += 1 continue # comment # line. mark_num = _line.count('//') if mark_num: comment_num += 1 continue # blank line. mark_num = _line.count(' ') + _line.count('\n') if mark_num == len(_line): blank_num += 1 continue # Code Line. if is_comment_start is False: code_num += 1 else: comment_num += 1 else: print '%s...OK' % py_file['name'] break total_line = comment_num + blank_num + code_num print '****** Result ******' print 'Total : %d' % total_line print 'Comment : %d %.2f %%' % (comment_num, float(comment_num)*100/float(total_line)) print 'Blank : %d %.2f %%' % (blank_num, float(blank_num)*100/float(total_line)) print 'Code : %d %.2f %%' % (code_num, float(code_num)*100/float(total_line)) print '********************'
小结:
工具写得比较简单,有些特殊情况没有进行细分,例如:一行既有代码又有注释的时候统一按照注释行统计了(貌似有点小亏)
0 0
- 代码统计工具-python版本
- 代码行统计工具(python)
- 代码修改统计工具(python)
- 代码统计工具1.1版本技术文档
- Python 代码覆盖率统计工具 coverage.py
- Python 代码覆盖率统计工具 coverage.py
- Python实现代码行数统计工具
- 用Python编写C\C++代码统计工具
- 代码行数统计工具
- java代码统计工具
- 代码统计工具
- 代码统计工具
- java代码统计工具
- 代码行统计工具
- 代码统计工具cloc
- sloccount 代码统计工具
- 代码行数统计工具
- 代码统计工具statsvn
- Direct3D开启光照和使用材质绘制物体
- 16. 前缀码判定
- hdu3157 有源汇上下界最小流
- Mysql服务器设置及技巧
- hibernate 导出数据库成功 在hibernate里面做数据库
- 代码统计工具-python版本
- jquery easy UI 的hello版
- 17. 图的广度优先遍历
- 保护Spring应用之——spring Security(一)
- Iterator模式(C++迭代器模式)
- ExtJS学习之路第一步:对比jQuery,认识ExtJS
- nvocationHandler中invoke()方法的调用问题
- ExtJS学习之路第二步:Ext.Component 和 Ext.dom.Element 的区别
- Linux-0.11内核源码分析系列:进程调度sleep_on()函数分析