Some utility functions built on top of the official ast
module of Python.
- Important: To use all the functions,
Python3.9
is required.
pip install git+https://github.com/matteogabburo/python-ast-utils.git
import astutils
ast = astutils.ast_parse("path/to/a/file.py")
import astutils
sourcecode = "def hello(name):\n\tprint('hello', name)\nhello('John')\n"
ast = astutils.ast_parse_from_string(sourcecode)
import astutils
ast = astutils.ast_parse("path/to/a/file.py")
py_program_str = astutils.ast_unparse(ast)
import astutils
ast = astutils.ast_parse("path/to/a/file.py")
dict_ast = astutils.ast2dict(ast)
import astutils
ast1 = astutils.ast_parse("path/to/a/file.py")
dict_ast = astutils.ast2dict(ast1)
ast2 = astutils.dict2ast(ast)
# ast1 == ast2
import astutils
ast = astutils.ast_parse("path/to/a/file.py")
dict_ast = astutils.ast2dict(ast)
json_ast = astutils.dict2json(dict_ast)
import astutils
ast = astutils.ast_parse("path/to/a/file.py")
json_ast = astutils.ast2json(ast)
import astutils
ast = astutils.ast_parse("path/to/a/file.py")
heap_ast = astutils.ast2heap(ast)
import astutils
sourcecode = "def hello(name):\n\tprint('hello', name)\nhello('John')\n"
ast = astutils.ast_parse_from_string(sourcecode)
heap_ast = astutils.ast2heap(ast, source=sourcecode)
code = astutils.heap2code(heap_ast)
assert(sourcecode == code)
import astutils
sourcecode = "def hello(name):\n\tprint('hello', name)\nhello('John')\n"
ast = astutils.ast_parse_from_string(sourcecode)
heap_ast = astutils.ast2heap(ast, source=sourcecode)
tokens = astutils.heap2tokens(heap_ast)
assert(sourcecode == "".join([tok for tok, node_id, node_type in tokens]))
import astutils
sourcecode = "def hello(name):\n\tprint('hello', name)\nhello('John')\n"
ast = astutils.ast_parse_from_string(sourcecode)
heap_ast = astutils.ast2heap(ast, source=sourcecode)
sub_heaps = astutils.decompose(heap_ast)
print("num sub-heaps:", len(sub_heaps))
import astutils
sourcecode = "def hello(name):\n\tprint('hello', name)\nhello('John')\n"
ast = astutils.ast_parse_from_string(sourcecode)
heap_ast = astutils.ast2heap(ast, source=sourcecode)
sub_heaps = astutils.greedy_decompose(heap_ast, min_size=1, max_size=4, mode='max', measure="ntokens")
print("num sub-heaps:", len(sub_heaps))