Skip to content

Instantly share code, notes, and snippets.

@JoaoFelipe
Created May 4, 2018 16:31
Show Gist options
  • Select an option

  • Save JoaoFelipe/bdeb43c754d4c955cbbcaf79d83bd3d7 to your computer and use it in GitHub Desktop.

Select an option

Save JoaoFelipe/bdeb43c754d4c955cbbcaf79d83bd3d7 to your computer and use it in GitHub Desktop.
AST Transformer
import ast
import astor
class Hitchhiker(ast.NodeTransformer):
def visit_FunctionDef(self, node):
node.body = [ast.parse('return 42')]
return node
expr='''
def resposta():
print ('Alô Mundo')
def outra_resposta():
return 0
'''
p = Hitchhiker().visit(ast.parse(expr))
print(astor.to_source(p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment