python - printing a string with function calls -


i have ast.dump so:

"module(body=[assign(targets=[name(id='i', ctx=store())], value=num(n=0)), while(test=compare(left=name(id='i', ctx=load()), ops=[lt()], comparators=[num(n=10)]), body=[print(dest=none, values=[name(id='i', ctx=load())], nl=true), augassign(target=name(id='i', ctx=store()), op=add(), value=num(n=1))], orelse=[]), for(target=name(id='x', ctx=store()), iter=call(func=name(id='range', ctx=load()), args=[num(n=10)], keywords=[], starargs=none, kwargs=none), body=[print(dest=none, values=[name(id='x', ctx=load())], nl=true)], orelse=[])])" 

how (pretty) print in more readable form 1 below??

module(        body=[assign(targets=[name(id='i',                                   ctx=store())],                     value=num(n=0)),              while(test=compare(left=name(id='i',                                           ctx=load()),                                 ops=[lt()],                    comparators=[num(n=10)]),                    body=[print(dest=none,                                values=[name(id='i',                                             ctx=load())],                                nl=true),                          augassign(target=name(id='i',                                                ctx=store()),                                    op=add(),                                     value=num(n=1))],                           orelse=[]),              for(target=name(id='x',                               ctx=store()),                  iter=call(func=name(id='range',                                      ctx=load()),                             args=[num(n=10)],                            keywords=[],                             starargs=none,                            kwargs=none),                  body=[print(dest=none,                               values=[name(id='x',                                           ctx=load())],                               nl=true)],                  orelse=[])]) 

incase wondering code generated this:

text = ''' = 0  while < 10:    print    += 1 x in range(10):    print x ''' ast.dump(ast.parse(text)) 

it's done, example by function or astpp module.

the latter 1 code:

import ast import astpp  tree = ast.parse( """ print "hello world!" s = "i'm string!" print s """) print astpp.dump(tree) 

should produce output:

module(body=[     print(dest=none, values=[         str(s='hello world!'),       ], nl=true),     assign(targets=[         name(id='s', ctx=store()),       ], value=str(s="i'm string!")),     print(dest=none, values=[         name(id='s', ctx=load()),       ], nl=true),   ]) 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -