-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcode_analysis.py
More file actions
36 lines (29 loc) · 1.19 KB
/
code_analysis.py
File metadata and controls
36 lines (29 loc) · 1.19 KB
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
import os
from tabulate import tabulate
skip_files = {"benchmark.py", "benchmark.go", "code_analysis.py", "README.md"} # filenames to skip (case-sensitive)
exts = {".go"} # File extensions to consider
stats = []
def count_lines(filepath):
with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
total = blank = comment = 0
for line in f:
total += 1
stripped = line.strip()
if not stripped:
blank += 1
elif stripped.startswith("//"):
comment += 1
return total, comment, blank
def walk_and_count(root):
for dirpath, _, files in os.walk(root):
for file in files:
if file in skip_files or not any(file.endswith(ext) for ext in exts):
continue
full_path = os.path.join(dirpath, file)
total, comment, blank = count_lines(full_path)
stats.append([file, total, comment, blank, total - comment - blank])
walk_and_count(".")
headers = ["File", "Total", "Comments", "Blanks", "Code"]
print(tabulate(stats, headers=headers, tablefmt="grid"))
total_code = sum(row[4] for row in stats)
print(f"\nTotal Code Lines: {total_code}")