我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用_ast.And()。
def parse_logic(struct): lineno = struct.lineno kw = dict(lineno=lineno, col_offset=0) if isinstance(struct.right, LogicalOp): ast_right, insert_into = parse_logic(struct.right) assert insert_into is None else: ast_right = struct.right parent = struct.parent Logic = _ast.Or if struct.flag == 'OR' else _ast.And if isinstance(parent, LogicalOp): ast_parent, insert_into = parse_logic(struct.parent) new_insert_into = [ast_right] insert_into.insert(0, _ast.BoolOp(op=Logic(), values=new_insert_into, **kw)) return ast_parent, new_insert_into elif parent is None: insert_into = [ast_right] return _ast.BoolOp(op=Logic(), values=insert_into, **kw), insert_into else: bool_op = _ast.BoolOp(op=Logic(), values=[parent, ast_right], **kw) return bool_op, None
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