Created
February 7, 2014 09:24
-
-
Save iziang/8859633 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#统计C文件和Python文件中有效代码行数 | |
#Python文件中空行,以"#"开头的注释行,不在统计之列 | |
#C文件中空行,以"//"开头的注释行,以"/* comment*/"包围的注释块,不再统计之列 | |
import os | |
import re | |
from sys import argv | |
filename, name= argv | |
blank_line =re.compile(r"^ *$") | |
python_comment_line = re.compile(r"^[ |\t]*#") | |
c_comment_single_line = re.compile(r"^[ |\t]*//") | |
c_comment_multi_line_start = re.compile(r"[ |\t]*/\*") | |
c_comment_multi_line_end = re.compile(r".*\*/$") | |
def python_count(descriptor): | |
count = 0 | |
for eachline in file_descriptor: | |
if not python_comment_line.match(eachline) and not blank_line.match(eachline): | |
count = count + 1 | |
return count | |
def c_count(file_descriptor): | |
flag = 0 | |
count = 0 | |
for eachline in file_descriptor: | |
if flag == 0: | |
if not blank_line.match(eachline) and not c_comment_single_line.match(eachline): | |
if c_comment_multi_line_start.match(eachline): | |
if c_comment_multi_line_end.match(eachline): | |
continue | |
else: | |
flag = 1 | |
continue | |
else: | |
count = count + 1 | |
else: | |
if not c_comment_multi_line_end.match(eachline): | |
continue | |
else: | |
flag = 0 | |
return count | |
file_descriptor = open(name) | |
print "total good line is %d" % c_count(file_descriptor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good.