代码统计工具-python版本
2014-12-10 23:34
316 查看
在网上搜了相关代码统计工具,发现统计python代码的工具貌似还没有出现,遂小试一把自己编写一版python代码统计工具。
语言:python
废话少说上代码
其实代码统计不外乎就是2点
1、扫描当前路径下所有相关文件
2、统计时候区分 空白行、注释行和代码行
同理,举一反三可以写出Javascript代码统计方法:
小结:
工具写得比较简单,有些特殊情况没有进行细分,例如:一行既有代码又有注释的时候统一按照注释行统计了(貌似有点小亏)
语言:python
废话少说上代码
#coding=utf-8 u""" User: Jerry.Fang Date: 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 '********************'
小结:
工具写得比较简单,有些特殊情况没有进行细分,例如:一行既有代码又有注释的时候统一按照注释行统计了(貌似有点小亏)
相关文章推荐
- 工欲善其事必先利其器-简单几步打造顺手的python开发工具(windows,Linux多版本)
- python版本wifi共享工具
- Python多版本共存管理工具之pyenv
- Python多版本管理工具pyenv及环境隔离工具virtualenv
- python 版本管理工具 pyenv 使用备忘
- 代码统计工具1.1版本技术文档
- Mac OSX python多版本管理工具:pyenv 和 virtualenv搭建
- 【Python】03、python多版本管理工具pyenv
- Python多版本共存管理工具之pyenv
- Python Data Science的多版本多环境管理工具Anaconda
- python版本wifi共享工具
- 自动发布工具版本从python2升级成python3后遇到的种种问题(涉及paramiko,Crypto,zipfile等等)
- 自动发布工具版本从python2升级成python3后遇到的种种问题(涉及paramiko,Crypto,zipfile等等)
- Mac OSX python多版本管理工具:pyenv 和 virtualenv搭建
- Python多版本切换工具-Pyenv\virtualenv及Anaconda科学计算环境的配置
- 工欲善其事必先利其器-简单几步打造顺手的python开发工具(windows,Linux多版本)
- [批量主机存活扫描工具scanhost]扫描主机存活[python版本,非nmap版本]
- python版本的curl工具pycurl学习
- 39 Anaconda - Python版本管理工具
- 升级 python 2.6.6 到 2.7.14 版本(pip工具安装)