Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 64d40a9

Browse files
committed
review the code as a Debugger
1 parent a9050e7 commit 64d40a9

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import ast
2+
import pycodestyle
3+
4+
5+
class CodeReviewer:
6+
def __init__(self):
7+
self.feedback = []
8+
9+
def analyze_python_file(self, file_path):
10+
with open(file_path, 'r') as file:
11+
code = file.read()
12+
self.analyze_python_code(code)
13+
14+
def analyze_python_code(self, code):
15+
try:
16+
# Parse the Python code into an Abstract Syntax Tree (AST)
17+
tree = ast.parse(code)
18+
except SyntaxError as e:
19+
self.feedback.append(f"Syntax Error: {e}")
20+
return
21+
22+
# Check for indentation errors and undefined variables
23+
self._check_indentation(tree)
24+
self._check_undefined_vars(tree)
25+
26+
# Check code style using pycodestyle
27+
self._check_code_style(code)
28+
29+
# Check code comments
30+
self._check_comments(code)
31+
32+
def _check_indentation(self, tree):
33+
for node in ast.walk(tree):
34+
if isinstance(node, ast.FunctionDef):
35+
if node.body and not isinstance(node.body[0], ast.Expr):
36+
self.feedback.append(
37+
f"Function '{node.name}' should have a docstring or 'pass' statement.")
38+
elif isinstance(node, (ast.For, ast.While, ast.If, ast.With)):
39+
if not isinstance(node.body[0], ast.Expr):
40+
self.feedback.append(
41+
f"Indentation Error: Missing 'pass' statement for '{ast.dump(node)}'.")
42+
43+
def _check_undefined_vars(self, tree):
44+
undefined_vars = set()
45+
for node in ast.walk(tree):
46+
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):
47+
undefined_vars.discard(node.id)
48+
elif isinstance(node, ast.Name) and isinstance(node.ctx, ast.Load):
49+
undefined_vars.add(node.id)
50+
51+
for var in undefined_vars:
52+
self.feedback.append(f"Variable '{var}' is used but not defined.")
53+
54+
def _check_code_style(self, code):
55+
style_guide = pycodestyle.StyleGuide()
56+
result = style_guide.check_files(code)
57+
if result.total_errors:
58+
self.feedback.append(
59+
"Code style issues found. Please check and fix them.")
60+
61+
def _check_comments(self, code):
62+
lines = code.split('\n')
63+
for i, line in enumerate(lines):
64+
if line.strip().startswith('#'):
65+
# Check for empty comments or comments without space after '#'
66+
if len(line.strip()) == 1 or line.strip()[1] != ' ':
67+
self.feedback.append(
68+
f"Improve comment style in line {i + 1}: '{line.strip()}'")
69+
70+
def get_feedback(self):
71+
return self.feedback
72+
73+
74+
if __name__ == "__main__":
75+
# Example Python code file to analyze
76+
python_file = "../Recipe_Generator/recipe_generator.py"
77+
78+
code_reviewer = CodeReviewer()
79+
code_reviewer.analyze_python_file(python_file)
80+
81+
feedback = code_reviewer.get_feedback()
82+
83+
if feedback:
84+
print("Code Review Feedback:")
85+
for msg in feedback:
86+
print(f"- {msg}")
87+
else:
88+
print("No coding errors found. Code looks good!")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ast
2+
pycodestyle

0 commit comments

Comments
 (0)