Skip to content

Commit 620c524

Browse files
committed
multiple comment detected
1 parent 5d2468b commit 620c524

File tree

3 files changed

+92
-18
lines changed

3 files changed

+92
-18
lines changed
24 KB
Binary file not shown.
303 Bytes
Binary file not shown.

zeyue/0007/ComputeCodeLines.py

Lines changed: 92 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
File: ComputeCodeLines.py
5+
Author: Zeyue Liang
6+
7+
Github: https://github.com/zeyue
8+
Description:
9+
A program that can compute code line numbers in a directory.
10+
"""
211

312
import os
413

514

615
file_suffix = "not defined"
716
inline_comment_syntax = "not defined"
8-
multiline_comment_syntax = "not defined"
17+
start_comment_syntax = "not defined"
18+
end_comment_syntax = "not defined"
19+
multilineCommentStartFlag = 0
920
result = {"Code files": 0,
1021
"Total lines": 0,
1122
"Code lines": 0,
@@ -14,6 +25,12 @@
1425

1526

1627
def getFilesList(directory):
28+
"""Get files's list in a directory
29+
30+
:directory: files' directory
31+
:returns: file path list
32+
33+
"""
1734
file_paths = []
1835

1936
for root, directories, files in os.walk(directory):
@@ -25,48 +42,90 @@ def getFilesList(directory):
2542

2643

2744
def getSuffix(full_file_path):
45+
"""Get the suffix of current file.
46+
47+
:full_file_path: path of one file
48+
:returns: suffix
49+
50+
"""
2851
return os.path.splitext(full_file_path)[1][1:]
2952

3053

3154
def identifyFileType(suffix):
55+
"""Identify the file type and return correct syntax.
56+
57+
:suffix: file suffix
58+
:returns: [inline comment syntax, multiple line comment syntax]
59+
60+
"""
3261
if suffix == "py":
33-
return "#", "\"\"\""
62+
return "#", "\"\"\"", "\"\"\""
3463
elif suffix == "c" or suffix == "h" or suffix == "cpp" or suffix == "hpp":
35-
return "//", "/*"
64+
return "//", "/*", "*/"
65+
elif suffix == "java":
66+
return "//", "/*", "*/"
3667
else:
3768
return "not defined"
3869

3970

4071
def isInlineComment(string_line):
41-
"""This function will check if input line is a comment or not.
72+
"""Check if string line is an inline comment or not.
4273
4374
:string_line: input line
4475
:returns: true or false
4576
4677
"""
4778
commentLen = len(inline_comment_syntax)
4879
if string_line[0:commentLen] == inline_comment_syntax:
80+
# print("zero")
4981
return True
5082
else:
5183
return False
5284

5385

5486
def isMultilineComment(string_line):
55-
commentLen = len(str(multiline_comment_syntax))
56-
if string_line[0:commentLen] == multiline_comment_syntax:
87+
"""Check if the string line is a multiple comment or not.
88+
89+
:string_line: one string line in the file
90+
:returns: True or False
91+
92+
"""
93+
global multilineCommentStartFlag
94+
commentLen = len(str(start_comment_syntax))
95+
# print(multilineCommentStartFlag)
96+
if(string_line[0:commentLen] == start_comment_syntax and
97+
multilineCommentStartFlag == 0):
98+
multilineCommentStartFlag = 1
99+
# print("one")
100+
return True
101+
elif(string_line[0:commentLen] != start_comment_syntax and
102+
multilineCommentStartFlag == 1):
103+
# print("two")
104+
return True
105+
elif(string_line[0:commentLen] == end_comment_syntax and
106+
multilineCommentStartFlag == 1):
107+
multilineCommentStartFlag = 0
108+
# print("three")
57109
return True
58110
else:
59111
return False
60112

61113

62114
def countOneFile(full_file_path):
63-
global inline_comment_syntax, multiline_comment_syntax
115+
"""Count code lines in one file.
116+
117+
:full_file_path: full path of the file
118+
:returns: null
119+
120+
"""
121+
global inline_comment_syntax, start_comment_syntax, end_comment_syntax
122+
global multilineCommentStartFlag
64123
file_suffix = getSuffix(full_file_path)
65124
if(identifyFileType(file_suffix) != "not defined"):
66125
result["Code files"] += 1
67-
68126
(inline_comment_syntax,
69-
multiline_comment_syntax) = identifyFileType(file_suffix)
127+
start_comment_syntax,
128+
end_comment_syntax) = identifyFileType(file_suffix)
70129
with open(full_file_path, "r") as lines:
71130
for line in lines:
72131
line = line.strip()
@@ -77,22 +136,37 @@ def countOneFile(full_file_path):
77136
elif isMultilineComment(line):
78137
result["Comment lines"] += 1
79138
else:
139+
# print("Oops...")
80140
result["Code lines"] += 1
81141
else:
82142
result["Blank lines"] += 1
143+
print(str(result))
83144

84145

85146
def countInDirectory(directory):
147+
"""Count code lines in one directory.
148+
149+
:directory: the directory that will be counted
150+
:return: null
151+
152+
"""
86153
full_file_paths = getFilesList(directory)
87154
for f in full_file_paths:
88155
countOneFile(f)
89156

90-
directory = input()
91-
# countOneFile("d:/NOTBACKEDUP/python/zeyue/0007/ComputeCodeLines.py")
92-
countInDirectory(directory)
93-
# countInDirectory("D:/Documents/Zeyue/workspace/Gauge/v1.3.10\
94-
# /Recorder/QuartzDyne/src")
95-
# countOneFile("D:/Documents/Zeyue/workspace/Gauge/v1.3.10\
96-
# /Recorder/QuartzDyne/src/CommandTable.c")
97157

98-
print(str(result))
158+
def main():
159+
"""main function.
160+
161+
"""
162+
print("Please input a full directory: ")
163+
# directory = input()
164+
directory = "d:\PythonProjects\python\zeyue"
165+
countInDirectory(directory)
166+
for key in result:
167+
print(str(key) + ": " + str(result[key]))
168+
169+
170+
main()
171+
# if(isMultilineComment("\"\"\"dfadf")):
172+
# print("yes")

0 commit comments

Comments
 (0)