py2dotTwo of my recent obsessions have been the standard library 'compiler' module and the Graphviz graph visualization software. To scratch these two itches, I wrote py2dot, a simple script for displaying Python ASTs. Here's a visualization of a 'factorial' function (click on the images for full-size versions): py2dot is easy to use. Here's how I generated the above image: andy:mitya:~/py2dot$ py2dot --reduce -o - factorial.py | dot -Tpng -ofactorial.png The '--reduce' option makes the output graph smaller by partially evaluating expressions containing only 'scalar' values and pruning the child nodes. Here's py2dot's usage message: usage: py2dot.py [options] FILE options: -h, --help show this help message and exit -o FILE, --output-file=FILE write output to FILE (- for stdout (default)) -r, --reduce-graph reduce the AST -l, --lineno print line numbers -b, --black-and-white output in black and white Here's the same factorial function, without the reduce option, and with line numbers displayed. Note the substantially larger tree: You can download py2dot here [ colorized source ].py2dot requires Graphviz and Python 2.4. py2dot is only hours old, so there's inefficiencies and bugs, to be sure. If you find them, please report them here. I have some new features I'll be adding as I find time, such as a profiler function for generating call graphs. last change 2005-01-21 01:32:32 |
py2dot is a useful script that generates Graphviz ".dot" files from Python source code. The output images can be used to teach parsing concepts, or as cubicle art. |
© 2005, Andrew R. Gross