-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreview_code.py
32 lines (24 loc) · 858 Bytes
/
review_code.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
import ast
import astor
import pycodestyle
# Define a simple Python code snippet
code = "def fun(a, b):\n return a + b"
# Parse the code and create an AST
tree = ast.parse(code)
class FunctionNameVisitor(ast.NodeVisitor):
def visit_FunctionDef(self, node):
print(f"Found function: {node.name}")
visitor = FunctionNameVisitor()
visitor.visit(tree)
# Define a simple AST transformer to replace addition with multiplication
class MultiplyTransformer(ast.NodeTransformer):
def visit_BinOp(self, node):
if isinstance(node.op, ast.Add):
return ast.BinOp(left=node.left, op=ast.Mult(), right=node.right)
return node
# Use the transformer to modify the AST
transformer = MultiplyTransformer()
new_tree = transformer.visit(tree)
# Print the modified AST
print(ast.dump(new_tree))
print(astor.to_source(new_tree))