我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用_ast.Or()。
def refactor_ifs(stmnt, ifs): ''' for if statements in list comprehension ''' if isinstance(stmnt, _ast.BoolOp): test, right = stmnt.values if isinstance(stmnt.op, _ast.Or): test = _ast.UnaryOp(op=_ast.Not(), operand=test, lineno=0, col_offset=0) ifs.append(test) return refactor_ifs(right, ifs) return stmnt
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