A Short Introduction
ASTNG - Abstract Syntax Tree New Generation - is an enhanced Python Syntax Tree Generator. It uses the tree generated from the '_ast' python module and rebuilds an new tree with more informations.
It is mainly used by Pylint, but also by Pyreverse and some other projects...
Here is a file foo.py:
class Super(object): @classmethod def instance(cls): return cls() class Sub(Super): def method(self): print 'method called' sub = Sub.instance()
Astng builds a tree with the ASTNGBuilder from a file or from string.
The returned tree has a list of nodes in tree.body. Nodes in the dictionary tree.locals store locally defined variables.
Finally, inference is an important feature of astng. So let us see how all this works:
>>> from logilab.astng import builder as buildmod >>> builder = buildmod.ASTNGBuilder() >>> tree = builder.string_build(open("foo.py").read()) >>> # you can also write : >>> tree = builder.file_build("foo.py") >>> print ', '.join(str(n) for n in tree.body) ClassDef(Super), ClassDef(Sub), Assign() >>> for key, vals in tree.locals.items(): ... print key, ":", [str(n) for n in vals] Super : [ClassDef(Super)] Sub : [ClassDef(Sub)] sub : [AssName(sub)] >>> print list(tree['sub'].infer()) [<Instance of .Sub at 0x38697296>]
To understand the tree structure of the analyzed code, we can use the tree_repr method. Here is a tree representation for the sub assignment:
tree.body.tree_repr() Assign() targets = [ AssName(sub) ] value = Call() func = Attribute() expr = Name(Sub) args = [ ] starargs = kwargs =