[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-ideas
Subject:    Re: [Python-ideas] AST Pretty Printer
From:       anatoly techtonik <techtonik () gmail ! com>
Date:       2013-09-15 20:34:24
Message-ID: CAPkN8xJZ9LP2ertKBaZ-24DQhr+CQ2rou1tCwXMhnTDKFgEthQ () mail ! gmail ! com
[Download RAW message or body]

On Fri, Sep 13, 2013 at 3:01 AM, Andrew Barnert <abarnert@yahoo.com> wrote:
> On Sep 12, 2013, at 16:31, Ryan <rymg19@gmail.com> wrote:
> 
> > I always encounter one problem when dealing with Python ASTs: When I print it, it \
> > looks like Lisp(aka Lots of Irritated Superfluous Parenthesis).
> 
> Why are the parentheses irritated? Have you been taunting them? :)

These look like smiley monsta to myeyes.

Module([ImportFrom('distutils.core', [alias('setup', None)], 0),
Expr(Call(Name('setup', Load()), [], [keyword('name', Str('astdump')),
keyword('version', Str('1.0')), keyword('author', Str('anatoly
techtonik')), keyword('author_email', Str('techtonik@gmail.com')),
keyword('description', Str('Extract information from Python module
without importing it.')), keyword('license', Str('Public Domain')),
keyword('py_modules', List([Str('astdump')], Load()))], None, None))])

> > In short: it's a mess.
> > 
> > My idea is an AST pretty printer built on ast.NodeVisitor. If anyone finds this \
> > interesting, I can probably have a prototype of the class between later today and \
> > sometime tomorrow.
> 
> Yes please!
> 
> I'll bet most people who play with ASTs want this, build something half-assed, \
> never finish it, and lose it by the next time they look at ASTs again three years \
> later... So if you finish something, that'll save effort for hundreds of people in \
> the future (who have no idea they'll want it one day).

My version of half-assed, semi-finished, only one year fresh and code
complete for that it does. =)

$ hg clone https://bitbucket.org/techtonik/astdump
$ cd astdump
$ ./astdump.py --generate astdump.py > setup.py
$ ./astdump.py --dump setup.py
Module
  ImportFrom
    alias
  Expr
    Call
      Name
        Load
      keyword
        Str
      keyword
        Str
      keyword
        Str
      keyword
        Str
      keyword
        Str
      keyword
        Str
      keyword
        List
          Str
          Load

Source code is in public domain, latest version:
https://bitbucket.org/techtonik/astdump/src/tip/astdump.py?at=default

The API for dumping is:
  TreeDumper().dump(root)

  class TreeDumper(ast.NodeVisitor):
    def dump(self, node, types=[], level=None, callback=None):
      """pretty-print AST tree

         if `types` is set, process only types in the list
         if `level` is set, limit output to the given depth
         `callback` (if set) will be called to process filtered node
      """

To customize, just supply a callback. Example callbacks:

  def printcb(node, level):
    nodename = node.__class__.__name__
    print(' '*level*2 + nodename)

It played with it on Python 2, but it should be runnable on Python 3
with simple print replacements.

--
anatoly t.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic