Next: , Previous: , Up: Python Library   [Contents][Index]


7.2 Running the SEL Python Library

The actual Python interface is defined in python/asts/asts.py. The following demonstrates usage of this library.

$ python3
Python 3.8.5
Type "help", "copyright", "credits" or "license" for more information.
>>> import asts
>>> it = asts.AST("python", "x + 88")
>>> it.children()
[<asts.asts.AST object at 0x7f8e91fb52b0>]
>>> it.children()[0].children()
[<asts.asts.AST object at 0x7f8e918b7100>]
>>> it.children()[0].children()[0].children()
[<asts.asts.AST object at 0x7f8e918b7490>, <asts.asts.AST object at 0x7f8e918b73a0>, <asts.asts.AST object at 0x7f8e918b73d0>]
>>> it.children()[0].children()[0].children()[0].source_text()
'x'
>>> it.children()[0].children()[0].children()[1].source_text()
'+'
>>> it.children()[0].children()[0].children()[2].source_text()
'88'
>>> it.children()[0].children()[0].source_text()
'x + 88'
>>> it.children()[0].children()[0].child_slots()
[['PYTHON-LEFT', 1], ['PYTHON-OPERATOR', 1], ['PYTHON-RIGHT', 1], ['CHILDREN', 0]]
>>> list(map(lambda x:x.source_text(), it.children()[0].children()[0].children()))
['x', '+', '88']
>>> list(map(lambda x:x.ast_type(), it.children()[0].children()[0].children()))
['PYTHON-IDENTIFIER', 'PYTHON-+', 'PYTHON-INTEGER']