Extracting attributes from compiled python code or parse trees

Peter Otten __peter__ at web.de
Tue Jul 24 08:54:05 CEST 2007


Matteo wrote:

> I am trying to get Python to extract attributes in full dotted form
> from compiled expression. For instance, if I have the following:
> 
> param = compile('a.x + a.y','','single')
> 
> then I would like to retrieve the list consisting of ['a.x','a.y'].
> I have tried using inspect to look at 'co_names', but when I do that,

You can have a look at the compiler package. A very limited example:

import compiler
import compiler.ast
import sys

class Visitor:
    def __init__(self):
        self.names = []
    def visitName(self, node):
        self.names.append(node.name)
    def visitGetattr(self, node):
        dotted = []
        n = node
        while isinstance(n, compiler.ast.Getattr):
            dotted.append(n.attrname)
            n = n.expr
        try:
            dotted.append(n.name)
        except AttributeError:
            print >> sys.stderr, "ignoring", node
        else:
            self.names.append(".".join(reversed(dotted)))


if __name__ == "__main__":
    expr = " ".join(sys.argv[1:])
    visitor = Visitor()
    compiler.walk(compiler.parse(expr), visitor)
    print "\n".join(visitor.names)

Output:

$ python dotted_names.py "a + b * (c + sin(d.e) + x.y.z)"
a
b
c
sin
d.e
x.y.z

$ python dotted_names.py "a + b * ((c + d).e + x.y.z)"
ignoring Getattr(Add((Name('c'), Name('d'))), 'e')
a
b
x.y.z

Peter



More information about the Python-list mailing list