File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -17,22 +17,38 @@ def count_lines(self):
1717 print (self .file_name + ' is not a .py file!' )
1818 return
1919 mc_flag = False
20+ mc_start = ''
21+ """
22+ 最高优先级检测由三连引号包围的多行注释,也可能是单行注释;
23+ 其次检测空行;
24+ 再次检测由#开头的单行注释,由单引号或者双引号包围的单行注释;
25+ """
2026 try :
2127 with open (self .file_name ) as code :
2228 for each_line in code :
2329 self .total_line_num += 1
2430 temp = each_line .strip ()
25- if temp == '' :
26- self .blank_line_num += 1
27- elif temp [0 ] == '#' :
31+ if mc_flag is False :
32+ if temp [0 :3 ] in ["'''" , '"""' ]:
33+ mc_start = temp [0 :3 ]
34+ mc_flag = True
35+ if len (temp ) >= 6 and temp [- 3 :] == mc_start :
36+ self .comment_line_num += 1
37+ mc_start = ''
38+ mc_flag = False
39+ else :
40+ if len (temp ) == 0 :
41+ self .blank_line_num += 1
42+ else :
43+ if temp [0 ] == '#' :
44+ self .comment_line_num += 1
45+ elif temp [0 ] in ['"' , "'" ] and temp [0 ] == temp [- 1 :]:
46+ self .comment_line_num += 1
47+ elif temp [- 3 :] == mc_start :
48+ #found the end of the multiple-line comment
2849 self .comment_line_num += 1
29- else :
30- if False == mc_flag :
31- if temp [0 :3 ] == '"""' :
32- mc_flag = True
33- elif temp [- 3 :] == '"""' :
34- mc_flag = False
35- self .comment_line_num += 1
50+ mc_start = ''
51+ mc_flag = False
3652 if mc_flag :
3753 self .comment_line_num += 1
3854 except IOError as err :
You can’t perform that action at this time.
0 commit comments