-
Notifications
You must be signed in to change notification settings - Fork 0
/
missing_comment.py
69 lines (53 loc) · 1.95 KB
/
missing_comment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def missing_comment(file):
"""
This function takes the name of file as input argument
and print the file name, name of function and line number
of any function definition that is not preceeded by a comment.
@param: String - file
@return: None
"""
with open(file) as f:
# Starting counter to keep track of line number
counter = 1
for line in f:
# Checking if line starts with #
if line.lstrip().startswith('#'):
comment = True
# Checking if line a function definition but not preceeded by comment line
elif comment is False and line.startswith('def '):
# Extract function name from line
func_name = line[line.index(' ')+1:line.index('(')]
# Print output
print(f'\nFunction name: {func_name}')
print(f'File name: {file}')
print(f'Line number: {counter}')
else:
comment = False
counter += 1
def main():
# Initialize variables
files = list()
invalid_files = list()
while True:
# Request input from user until blank line is entered.
file = input("Enter file name or blank space to end: ")
if file == "":
break
# Append all file names in files
files.append(file)
# Check the content of all the files in files for functions
# that are not immediately preceeded by a comment.
for file in files:
try:
missing_comment(file)
except:
# If a file name is invalid, add the name into invalid_files.
invalid_files.append(file)
# If there were invalid files name entry by the user,
# print them all out.
if len(invalid_files) > 0:
print("\nThese file(s) could not be accessed!:")
for file in invalid_files:
print(file)
if __name__ == '__main__':
main()