Skip to content

Instantly share code, notes, and snippets.

@felipecruz
Created February 16, 2013 02:29
Show Gist options
  • Save felipecruz/4965216 to your computer and use it in GitHub Desktop.
Save felipecruz/4965216 to your computer and use it in GitHub Desktop.
import ast
# just for convenience
Assign = ast.Assign
Store = ast.Store
Module = ast.Module
Name = ast.Name
Str = ast.Str
# creating an assignment statement -> magic = "loogica"
_ast_assignment = Module(body=[Assign(targets=[Name(id="magic",
ctx=Store())],
value=Str(s="loogica"))])
# ask for help on line stuff generation
fixed_ast_assignment = ast.fix_missing_locations(_ast_assignment)
# compile ast to code objects
code = compile(fixed_ast_assignment,
"repl.it",
"exec")
# executing
exec code
assert magic == "loogica"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment