Created
August 1, 2018 01:51
-
-
Save JustinSDK/6eeb14200bf7194ee4feb23e029e849d to your computer and use it in GitHub Desktop.
modify_ast.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ast | |
code = ''' | |
def add(n1, n2): | |
return n1 + n2 | |
print(add(3, 4)) | |
''' | |
class CrazyTransformer(ast.NodeTransformer): | |
def visit_BinOp(self, node): | |
node.op = ast.Mult() | |
return node | |
node = ast.parse(code) | |
modified = CrazyTransformer().visit(node) | |
exec(compile(modified, '<string>', 'exec')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment