[Chicago] compiler.walk() help
skip at pobox.com
skip at pobox.com
Tue Jul 11 22:46:43 CEST 2006
Does anybody here have a small example of using the compiler.walk()
function? The online docs seem fairly incomplete and my simpleminded
attempts to get it working have thusfar been unsuccessful:
>>> import compiler
>>> ast = compiler.parse(open("a.py").read())
>>> ast
Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))
>>> compiler.walk(ast, compiler.visitor.ExampleASTVisitor, verbose=10)
>>> <class compiler.visitor.ExampleASTVisitor at 0x831ce3c>
>>> compiler.walk(ast, compiler.visitor.ExampleASTVisitor(), verbose=10)
<compiler.visitor.ExampleASTVisitor instance at 0x81f0eac>
I did manage to get a verbose print the first time I executed this:
>>> compiler.walk(ast, compiler.visitor.ExampleASTVisitor(), walker=compiler.visitor.ExampleASTVisitor(), verbose=10)
which emitted nice verbose stuff like
dispatch Module
<compiler.visitor.ExampleASTVisitor instance at 0x835074c>
compiler.ast.Module
asList <bound method Module.asList of Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))>
doc None
getChildNode <bound method Module.getChildNodes of Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))>
getChildren <bound method Module.getChildren of Module(None, Stmt([Assign([AssName('RED', 'OP_ASSIGN')], Const('red')), Assign([AssName('BLUE', 'OP_ASSIGN')], Const('blue'))]))>
lineno None
...
but successive calls just yielded the more electron-conserving:
dispatch Module
dispatch Stmt
dispatch Assign
dispatch AssName
dispatch Const
dispatch Assign
dispatch AssName
dispatch Const
Any examples would be much appreciated.
Thx,
Skip
More information about the Chicago
mailing list