Parsing Python to a list of instances -
i want parse pure-python code list of instances of classes represent various parts of original code..
an example:
>>> text = ''' ... x in range(100): ... print x ... ''' >>> tree = parse(text) >>> print tree tree( forloop(x,range(100), [stmt(print(x))]) ) # here forloop, range, stmt, print custom classes
the ast module has tools need:
>>> import ast >>> text = ''' x in range(100): print x ''' >>> m = ast.parse(text) >>> ast.dump(m) "module(body=[for(target=name(id='x', ctx=store()), iter=call(func=name(id='range', ctx=load()), args=[num(n=100)], keywords=[], starargs=none, kwargs=none), body=[print(dest=none, values=[name(id='x', ctx=load())], nl=true)], orelse=[])])"
Comments
Post a Comment