您的位置:首页 > 编程语言 > Python开发

代码统计工具-python版本

2014-12-10 23:34 316 查看
在网上搜了相关代码统计工具,发现统计python代码的工具貌似还没有出现,遂小试一把自己编写一版python代码统计工具。

语言: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 js 统计工具