Skip to content

Commit 35f2e7c

Browse files
author
ss7536
committed
Added comment density script
1 parent b8dfcba commit 35f2e7c

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
3+
import os
4+
import sys
5+
6+
def comment_density():
7+
8+
9+
# Path to data_structures directory
10+
data_structures_path = os.path.join(os.path.dirname(__file__), '..', '..', 'data_structures')
11+
12+
if not os.path.exists(data_structures_path):
13+
print(f"Error: data_structures directory not found at {data_structures_path}")
14+
return
15+
16+
total_lines = 0
17+
comment_lines = 0
18+
blank_lines = 0
19+
files_processed = 0
20+
21+
22+
for root, dirs, files in os.walk(data_structures_path):
23+
for file in files:
24+
if file.endswith('.py'):
25+
file_path = os.path.join(root, file)
26+
27+
try:
28+
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
29+
lines = f.readlines()
30+
31+
file_total = len(lines)
32+
file_comments = 0
33+
file_blanks = 0
34+
35+
in_multiline_comment = False
36+
multiline_delimiter = None
37+
38+
for line in lines:
39+
stripped_line = line.strip()
40+
original_line = line
41+
42+
if not stripped_line:
43+
file_blanks += 1
44+
continue
45+
46+
# Check single line comments
47+
if stripped_line.startswith('#'):
48+
file_comments += 1
49+
continue
50+
51+
# Check multi-line comments
52+
line_is_comment = False
53+
temp_line = original_line
54+
55+
while True:
56+
if not in_multiline_comment:
57+
# Look for start of multi-line comment
58+
triple_double_pos = temp_line.find('"""')
59+
triple_single_pos = temp_line.find("'''")
60+
61+
if triple_double_pos != -1 and (triple_single_pos == -1 or triple_double_pos < triple_single_pos):
62+
in_multiline_comment = True
63+
multiline_delimiter = '"""'
64+
line_is_comment = True
65+
temp_line = temp_line[triple_double_pos + 3:]
66+
elif triple_single_pos != -1:
67+
in_multiline_comment = True
68+
multiline_delimiter = "'''"
69+
line_is_comment = True
70+
temp_line = temp_line[triple_single_pos + 3:]
71+
else:
72+
break
73+
else:
74+
75+
line_is_comment = True
76+
end_pos = temp_line.find(multiline_delimiter)
77+
if end_pos != -1:
78+
in_multiline_comment = False
79+
multiline_delimiter = None
80+
temp_line = temp_line[end_pos + 3:]
81+
else:
82+
break
83+
84+
if line_is_comment:
85+
file_comments += 1
86+
87+
total_lines += file_total
88+
comment_lines += file_comments
89+
blank_lines += file_blanks
90+
files_processed += 1
91+
92+
except Exception as e:
93+
continue
94+
95+
code_lines = total_lines - blank_lines - comment_lines
96+
97+
# Calculate comment density
98+
non_blank_lines = total_lines - blank_lines
99+
if non_blank_lines > 0:
100+
comment_density = (comment_lines / non_blank_lines) * 100
101+
else:
102+
comment_density = 0.0
103+
104+
print(f"Code lines: {code_lines}")
105+
print(f"Comment lines: {comment_lines}")
106+
print(f"Comment density: {comment_density:.2f}%")
107+
108+
if __name__ == "__main__":
109+
comment_density()

0 commit comments

Comments
 (0)