Skip to content

Instantly share code, notes, and snippets.

@iziang
Created February 7, 2014 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iziang/8859633 to your computer and use it in GitHub Desktop.
Save iziang/8859633 to your computer and use it in GitHub Desktop.
#统计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)
@yurui829
Copy link

yurui829 commented Feb 7, 2014

Good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment