Skip to content

Instantly share code, notes, and snippets.

@ideasman42
Created September 5, 2015 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideasman42/efe531fc0dd4bc1686f0 to your computer and use it in GitHub Desktop.
Save ideasman42/efe531fc0dd4bc1686f0 to your computer and use it in GitHub Desktop.
Example of using py_to_ast.py. see: http://stackoverflow.com/a/28598913/432509
Module(
body=[
Import(
names=[
alias(
name='ast',
asname=None,
),
],
),
Import(
names=[
alias(
name='sys',
asname=None,
),
],
),
FunctionDef(
name='dump',
args=arguments(
args=[
arg(
arg='node',
annotation=None,
),
arg(
arg='annotate_fields',
annotation=None,
),
arg(
arg='include_attributes',
annotation=None,
),
arg(
arg='indent',
annotation=None,
),
],
vararg=None,
kwonlyargs=[],
kw_defaults=[],
kwarg=None,
defaults=[
NameConstant(
value=True,
),
NameConstant(
value=False,
),
Num(
n=0,
),
],
),
body=[
Expr(
value=Str(
s='\n ast.dump from Python3.4 modified for pretty printing.\n ',
),
),
ImportFrom(
module='ast',
names=[
alias(
name='AST',
asname=None,
),
alias(
name='iter_fields',
asname=None,
),
],
level=0,
),
FunctionDef(
name='_format',
args=arguments(
args=[
arg(
arg='node',
annotation=None,
),
arg(
arg='level',
annotation=None,
),
],
vararg=None,
kwonlyargs=[],
kw_defaults=[],
kwarg=None,
defaults=[],
),
body=[
If(
test=Name(
id='indent',
ctx=Load(),
),
body=[
Assign(
targets=[
Name(
id='level_next',
ctx=Store(),
),
],
value=BinOp(
left=Name(
id='level',
ctx=Load(),
),
op=Add(),
right=Num(
n=1,
),
),
),
Assign(
targets=[
Name(
id='indent_next',
ctx=Store(),
),
],
value=BinOp(
left=BinOp(
left=Name(
id='level_next',
ctx=Load(),
),
op=Mult(),
right=Name(
id='indent',
ctx=Load(),
),
),
op=Mult(),
right=Str(
s=' ',
),
),
),
Assign(
targets=[
Name(
id='indent_next_line',
ctx=Store(),
),
],
value=BinOp(
left=Str(
s='\n',
),
op=Add(),
right=Name(
id='indent_next',
ctx=Load(),
),
),
),
Assign(
targets=[
Name(
id='comma_delim',
ctx=Store(),
),
],
value=Str(
s=',\n',
),
),
],
orelse=[
Assign(
targets=[
Name(
id='level_next',
ctx=Store(),
),
],
value=Num(
n=0,
),
),
Assign(
targets=[
Name(
id='indent_next',
ctx=Store(),
),
],
value=Str(
s='',
),
),
Assign(
targets=[
Name(
id='indent_next_line',
ctx=Store(),
),
],
value=Str(
s='',
),
),
Assign(
targets=[
Name(
id='comma_delim',
ctx=Store(),
),
],
value=Str(
s=', ',
),
),
],
),
If(
test=Call(
func=Name(
id='isinstance',
ctx=Load(),
),
args=[
Name(
id='node',
ctx=Load(),
),
Name(
id='AST',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
body=[
Assign(
targets=[
Name(
id='fields',
ctx=Store(),
),
],
value=ListComp(
elt=Tuple(
elts=[
Name(
id='a',
ctx=Load(),
),
Call(
func=Name(
id='_format',
ctx=Load(),
),
args=[
Name(
id='b',
ctx=Load(),
),
Name(
id='level_next',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
],
ctx=Load(),
),
generators=[
comprehension(
target=Tuple(
elts=[
Name(
id='a',
ctx=Store(),
),
Name(
id='b',
ctx=Store(),
),
],
ctx=Store(),
),
iter=Call(
func=Name(
id='iter_fields',
ctx=Load(),
),
args=[
Name(
id='node',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
ifs=[],
),
],
),
),
Assign(
targets=[
Name(
id='rv',
ctx=Store(),
),
],
value=BinOp(
left=Str(
s='%s(%s%s',
),
op=Mod(),
right=Tuple(
elts=[
Attribute(
value=Attribute(
value=Name(
id='node',
ctx=Load(),
),
attr='__class__',
ctx=Load(),
),
attr='__name__',
ctx=Load(),
),
IfExp(
test=Compare(
left=Call(
func=Name(
id='len',
ctx=Load(),
),
args=[
Name(
id='fields',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
ops=[
Gt(),
],
comparators=[
Num(
n=0,
),
],
),
body=Name(
id='indent_next_line',
ctx=Load(),
),
orelse=Str(
s='',
),
),
Call(
func=Attribute(
value=BinOp(
left=Name(
id='comma_delim',
ctx=Load(),
),
op=Add(),
right=Name(
id='indent_next',
ctx=Load(),
),
),
attr='join',
ctx=Load(),
),
args=[
IfExp(
test=Name(
id='annotate_fields',
ctx=Load(),
),
body=BinOp(
left=ListComp(
elt=BinOp(
left=Str(
s='%s=%s',
),
op=Mod(),
right=Name(
id='field',
ctx=Load(),
),
),
generators=[
comprehension(
target=Name(
id='field',
ctx=Store(),
),
iter=Name(
id='fields',
ctx=Load(),
),
ifs=[],
),
],
),
op=Add(),
right=IfExp(
test=Name(
id='fields',
ctx=Load(),
),
body=List(
elts=[
Str(
s='',
),
],
ctx=Load(),
),
orelse=List(
elts=[],
ctx=Load(),
),
),
),
orelse=GeneratorExp(
elt=Name(
id='b',
ctx=Load(),
),
generators=[
comprehension(
target=Tuple(
elts=[
Name(
id='a',
ctx=Store(),
),
Name(
id='b',
ctx=Store(),
),
],
ctx=Store(),
),
iter=Name(
id='fields',
ctx=Load(),
),
ifs=[],
),
],
),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
],
ctx=Load(),
),
),
),
If(
test=BoolOp(
op=And(),
values=[
Name(
id='include_attributes',
ctx=Load(),
),
Attribute(
value=Name(
id='node',
ctx=Load(),
),
attr='_attributes',
ctx=Load(),
),
],
),
body=[
AugAssign(
target=Name(
id='rv',
ctx=Store(),
),
op=Add(),
value=BoolOp(
op=Or(),
values=[
BoolOp(
op=And(),
values=[
Name(
id='fields',
ctx=Load(),
),
Str(
s=', ',
),
],
),
Str(
s=' ',
),
],
),
),
AugAssign(
target=Name(
id='rv',
ctx=Store(),
),
op=Add(),
value=Call(
func=Attribute(
value=BinOp(
left=Name(
id='comma_delim',
ctx=Load(),
),
op=Add(),
right=Name(
id='indent_next',
ctx=Load(),
),
),
attr='join',
ctx=Load(),
),
args=[
GeneratorExp(
elt=BinOp(
left=Str(
s='%s=%s',
),
op=Mod(),
right=Tuple(
elts=[
Name(
id='a',
ctx=Load(),
),
Call(
func=Name(
id='_format',
ctx=Load(),
),
args=[
Call(
func=Name(
id='getattr',
ctx=Load(),
),
args=[
Name(
id='node',
ctx=Load(),
),
Name(
id='a',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
Name(
id='level_next',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
],
ctx=Load(),
),
),
generators=[
comprehension(
target=Name(
id='a',
ctx=Store(),
),
iter=Attribute(
value=Name(
id='node',
ctx=Load(),
),
attr='_attributes',
ctx=Load(),
),
ifs=[],
),
],
),
],
keywords=[],
starargs=None,
kwargs=None,
),
),
],
orelse=[],
),
Return(
value=BinOp(
left=Name(
id='rv',
ctx=Load(),
),
op=Add(),
right=Str(
s=')',
),
),
),
],
orelse=[
If(
test=Call(
func=Name(
id='isinstance',
ctx=Load(),
),
args=[
Name(
id='node',
ctx=Load(),
),
Name(
id='list',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
body=[
Return(
value=BinOp(
left=Str(
s='[%s%s]',
),
op=Mod(),
right=Tuple(
elts=[
IfExp(
test=Compare(
left=Call(
func=Name(
id='len',
ctx=Load(),
),
args=[
Name(
id='node',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
ops=[
Gt(),
],
comparators=[
Num(
n=0,
),
],
),
body=Name(
id='indent_next_line',
ctx=Load(),
),
orelse=Str(
s='',
),
),
Call(
func=Attribute(
value=BinOp(
left=Name(
id='comma_delim',
ctx=Load(),
),
op=Add(),
right=Name(
id='indent_next',
ctx=Load(),
),
),
attr='join',
ctx=Load(),
),
args=[
BinOp(
left=ListComp(
elt=Call(
func=Name(
id='_format',
ctx=Load(),
),
args=[
Name(
id='x',
ctx=Load(),
),
Name(
id='level_next',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
generators=[
comprehension(
target=Name(
id='x',
ctx=Store(),
),
iter=Name(
id='node',
ctx=Load(),
),
ifs=[],
),
],
),
op=Add(),
right=IfExp(
test=Name(
id='node',
ctx=Load(),
),
body=List(
elts=[
Str(
s='',
),
],
ctx=Load(),
),
orelse=List(
elts=[],
ctx=Load(),
),
),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
],
ctx=Load(),
),
),
),
],
orelse=[],
),
],
),
Return(
value=Call(
func=Name(
id='repr',
ctx=Load(),
),
args=[
Name(
id='node',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
),
],
decorator_list=[],
returns=None,
),
If(
test=UnaryOp(
op=Not(),
operand=Call(
func=Name(
id='isinstance',
ctx=Load(),
),
args=[
Name(
id='node',
ctx=Load(),
),
Name(
id='AST',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
),
body=[
Raise(
exc=Call(
func=Name(
id='TypeError',
ctx=Load(),
),
args=[
BinOp(
left=Str(
s='expected AST, got %r',
),
op=Mod(),
right=Attribute(
value=Attribute(
value=Name(
id='node',
ctx=Load(),
),
attr='__class__',
ctx=Load(),
),
attr='__name__',
ctx=Load(),
),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
cause=None,
),
],
orelse=[],
),
Assign(
targets=[
Name(
id='indent',
ctx=Store(),
),
],
value=Call(
func=Name(
id='int',
ctx=Load(),
),
args=[
Name(
id='indent',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
),
If(
test=Compare(
left=Name(
id='indent',
ctx=Load(),
),
ops=[
Lt(),
],
comparators=[
Num(
n=0,
),
],
),
body=[
Raise(
exc=Call(
func=Name(
id='ValueError',
ctx=Load(),
),
args=[
Str(
s='indent must be >= 0',
),
],
keywords=[],
starargs=None,
kwargs=None,
),
cause=None,
),
],
orelse=[],
),
Return(
value=Call(
func=Name(
id='_format',
ctx=Load(),
),
args=[
Name(
id='node',
ctx=Load(),
),
Num(
n=0,
),
],
keywords=[],
starargs=None,
kwargs=None,
),
),
],
decorator_list=[],
returns=None,
),
Import(
names=[
alias(
name='sys',
asname=None,
),
],
),
FunctionDef(
name='main',
args=arguments(
args=[],
vararg=None,
kwonlyargs=[],
kw_defaults=[],
kwarg=None,
defaults=[],
),
body=[
Assign(
targets=[
Name(
id='data',
ctx=Store(),
),
],
value=Call(
func=Attribute(
value=Attribute(
value=Name(
id='sys',
ctx=Load(),
),
attr='stdin',
ctx=Load(),
),
attr='read',
ctx=Load(),
),
args=[],
keywords=[],
starargs=None,
kwargs=None,
),
),
Assign(
targets=[
Name(
id='tree',
ctx=Store(),
),
],
value=Call(
func=Attribute(
value=Name(
id='ast',
ctx=Load(),
),
attr='parse',
ctx=Load(),
),
args=[
Name(
id='data',
ctx=Load(),
),
],
keywords=[],
starargs=None,
kwargs=None,
),
),
Expr(
value=Call(
func=Name(
id='print',
ctx=Load(),
),
args=[
Call(
func=Name(
id='dump',
ctx=Load(),
),
args=[
Name(
id='tree',
ctx=Load(),
),
],
keywords=[
keyword(
arg='indent',
value=Num(
n=2,
),
),
],
starargs=None,
kwargs=None,
),
],
keywords=[],
starargs=None,
kwargs=None,
),
),
],
decorator_list=[],
returns=None,
),
If(
test=Compare(
left=Name(
id='__name__',
ctx=Load(),
),
ops=[
Eq(),
],
comparators=[
Str(
s='__main__',
),
],
),
body=[
Expr(
value=Call(
func=Name(
id='main',
ctx=Load(),
),
args=[],
keywords=[],
starargs=None,
kwargs=None,
),
),
],
orelse=[],
),
],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment