File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ import ast
2
+
3
+ def analyze_python_code (file_path ):
4
+ with open (file_path , 'r' ) as f :
5
+ lines = f .readlines ()
6
+
7
+ total_lines = len (lines )
8
+ comment_lines = sum (1 for line in lines if line .strip ().startswith ("#" ))
9
+
10
+ with open (file_path , 'r' ) as f :
11
+ tree = ast .parse (f .read ())
12
+
13
+ functions = [node for node in ast .walk (tree ) if isinstance (node , ast .FunctionDef )]
14
+ longest_func = max (functions , key = lambda f : f .end_lineno - f .lineno , default = None )
15
+ longest_name = longest_func .name if longest_func else "N/A"
16
+ longest_len = (longest_func .end_lineno - longest_func .lineno + 1 ) if longest_func else 0
17
+
18
+ print (f"\n Analyzing: { file_path } " )
19
+ print (f"Total lines: { total_lines } " )
20
+ print (f"Comment lines: { comment_lines } " )
21
+ print (f"Number of functions: { len (functions )} " )
22
+ print (f"Code to comment ratio: { round (comment_lines / total_lines * 100 , 2 )} %" )
23
+ print (f"Longest function: { longest_name } ({ longest_len } lines)" )
24
+
25
+ if __name__ == "__main__" :
26
+ import sys
27
+ if len (sys .argv ) != 2 :
28
+ print ("Usage: python analyzer.py <file.py>" )
29
+ else :
30
+ analyze_python_code (sys .argv [1 ])
You can’t perform that action at this time.
0 commit comments