Ast.py 文件源码

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

项目:pythonql 作者: pythonql 项目源码 文件源码
def get_all_vars(a):
    if is_ast(a) and not is_literal(a):
        if type(a) == name_e:
            return {a.id}

        if is_comprehension(a):
            vs = list(get_all_vars(a.expr)) + [v for g in a.generators for v in get_all_vars(g)]
            vs = set(vs) - {v for g in a.generators for v in get_all_vars(g.target)}
            return vs

        if type(a)==call_e:
            # We need a special case for the make_pql_tuple
            if isinstance(a.func, name_e) and a.func.id == 'make_pql_tuple':
                t = a.args[0].values
                vs = [v for x in t for v in get_all_vars(get_ast(x.values[0].value))]
                return set(vs)

            # And we need a special case for nested queries also. However, instead of digging
            # into the nested query, we just return an impossible variable, so that nothing can
            # satisfy its dependency.
            if isinstance(a.func, name_e) and a.func.id == 'PyQuery':
                return {"#nested_query"}
            else:        
                vs = [v for x in a[1:] for y in x for v in get_all_vars(y) ]
                return set(vs)

        if type(a)==attribute_e:
            return get_all_vars(a.value)

        retvars = set()
        for x in a:
            if is_ast(x):
                retvars = retvars.union(get_all_vars(x))
            elif type(x)==list:
                for y in x:
                    if is_ast(y):
                        retvars = retvars.union(get_all_vars(y))
        return retvars
    return set()

# Replace variables inside an expression accorind to the table
# of mappings
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号