ast_visualizer.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:Lyra 作者: caterinaurban 项目源码 文件源码
def generate_pyast(code):
    """
    Parses the code and creates a structure of lists and dicts only.
    :param code: the code as a string
    :return: a structure of lists and dicts only, representing the ast of code
    """
    import ast

    def transform_ast(code_ast):
        if isinstance(code_ast, ast.AST):
            # noinspection PyProtectedMember
            node = {to_camelcase(k): transform_ast(getattr(code_ast, k)) for k in code_ast._fields}
            node['node_type'] = to_camelcase(code_ast.__class__.__name__)
            return node
        elif isinstance(code_ast, list):
            return [transform_ast(el) for el in code_ast]
        else:
            return code_ast

    return transform_ast(ast.parse(code))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号