Skip to content

Some utility functions built on top of the official ast module of Python.

License

Notifications You must be signed in to change notification settings

matteogabburo/python-ast-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-ast-utils Build Status

Some utility functions built on top of the official ast module of Python.

Installation

  • Important: To use all the functions, Python3.9 is required.

pip + git

pip install git+https://github.com/matteogabburo/python-ast-utils.git

Examples

Parse a Python file:

import astutils
ast = astutils.ast_parse("path/to/a/file.py")

Parse a string representing a Python file:

import astutils
sourcecode = "def hello(name):\n\tprint('hello', name)\nhello('John')\n"
ast = astutils.ast_parse_from_string(sourcecode)

Unparse a Python AST (only from Python3.9):

import astutils
ast = astutils.ast_parse("path/to/a/file.py")
py_program_str = astutils.ast_unparse(ast)

AST2Dict:

import astutils
ast = astutils.ast_parse("path/to/a/file.py")
dict_ast = astutils.ast2dict(ast)

Dict2AST:

import astutils
ast1 = astutils.ast_parse("path/to/a/file.py")
dict_ast = astutils.ast2dict(ast1)
ast2 = astutils.dict2ast(ast)
# ast1 == ast2

dict2Json:

import astutils
ast = astutils.ast_parse("path/to/a/file.py")
dict_ast = astutils.ast2dict(ast)
json_ast = astutils.dict2json(dict_ast)

AST2Json:

import astutils
ast = astutils.ast_parse("path/to/a/file.py")
json_ast = astutils.ast2json(ast)

AST2Heap:

import astutils
ast = astutils.ast_parse("path/to/a/file.py")
heap_ast = astutils.ast2heap(ast)

Heap2Code:

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)

Heap2Tokens:

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]))

HeapDecompose:

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))

HeapGreadyDecompose:

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))

About

Some utility functions built on top of the official ast module of Python.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages