我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用_ast.BoolOp()。
def visit_Name(self, name): # Display the repr of the name if it's a local variable or # _should_repr_global_name() thinks it's acceptable. locs = ast_Call(self.builtin("locals"), [], []) inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs]) dorepr = self.helper("should_repr_global_name", name) test = ast.BoolOp(ast.Or(), [inlocs, dorepr]) expr = ast.IfExp(test, self.display(name), ast.Str(name.id)) return name, self.explanation_param(expr)
def visit_Compare(self, comp): self.push_format_context() left_res, left_expl = self.visit(comp.left) if isinstance(comp.left, (_ast.Compare, _ast.BoolOp)): left_expl = "({0})".format(left_expl) res_variables = [self.variable() for i in range(len(comp.ops))] load_names = [ast.Name(v, ast.Load()) for v in res_variables] store_names = [ast.Name(v, ast.Store()) for v in res_variables] it = zip(range(len(comp.ops)), comp.ops, comp.comparators) expls = [] syms = [] results = [left_res] for i, op, next_operand in it: next_res, next_expl = self.visit(next_operand) if isinstance(next_operand, (_ast.Compare, _ast.BoolOp)): next_expl = "({0})".format(next_expl) results.append(next_res) sym = binop_map[op.__class__] syms.append(ast.Str(sym)) expl = "%s %s %s" % (left_expl, sym, next_expl) expls.append(ast.Str(expl)) res_expr = ast.Compare(left_res, [op], [next_res]) self.statements.append(ast.Assign([store_names[i]], res_expr)) left_res, left_expl = next_res, next_expl # Use pytest.assertion.util._reprcompare if that's available. expl_call = self.helper("call_reprcompare", ast.Tuple(syms, ast.Load()), ast.Tuple(load_names, ast.Load()), ast.Tuple(expls, ast.Load()), ast.Tuple(results, ast.Load())) if len(comp.ops) > 1: res = ast.BoolOp(ast.And(), load_names) else: res = load_names[0] return res, self.explanation_param(self.pop_format_context(expl_call))
def test_compile_ast(self): fname = __file__ if fname.lower().endswith(('pyc', 'pyo')): fname = fname[:-1] with open(fname, 'r') as f: fcontents = f.read() sample_code = [ ['<assign>', 'x = 5'], ['<ifblock>', """if True:\n pass\n"""], ['<forblock>', """for n in [1, 2, 3]:\n print(n)\n"""], ['<deffunc>', """def foo():\n pass\nfoo()\n"""], [fname, fcontents], ] for fname, code in sample_code: co1 = compile(code, '%s1' % fname, 'exec') ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST) self.assertTrue(type(ast) == _ast.Module) co2 = compile(ast, '%s3' % fname, 'exec') self.assertEqual(co1, co2) # the code object's filename comes from the second compilation step self.assertEqual(co2.co_filename, '%s3' % fname) # raise exception when node type doesn't match with compile mode co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST) self.assertRaises(TypeError, compile, co1, '<ast>', 'eval') # raise exception when node type is no start node self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec') # raise exception when node has invalid children ast = _ast.Module() ast.body = [_ast.BoolOp()] self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
def test_compile_ast(self): fname = __file__ if fname.lower().endswith(('pyc', 'pyo')): fname = fname[:-1] with open(fname, 'r') as f: fcontents = f.read() sample_code = [ ['<assign>', 'x = 5'], ['<print1>', 'print 1'], ['<printv>', 'print v'], ['<printTrue>', 'print True'], ['<printList>', 'print []'], ['<ifblock>', """if True:\n pass\n"""], ['<forblock>', """for n in [1, 2, 3]:\n print n\n"""], ['<deffunc>', """def foo():\n pass\nfoo()\n"""], [fname, fcontents], ] for fname, code in sample_code: co1 = compile(code, '%s1' % fname, 'exec') ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST) self.assertTrue(type(ast) == _ast.Module) co2 = compile(ast, '%s3' % fname, 'exec') self.assertEqual(co1, co2) # the code object's filename comes from the second compilation step self.assertEqual(co2.co_filename, '%s3' % fname) # raise exception when node type doesn't match with compile mode co1 = compile('print 1', '<string>', 'exec', _ast.PyCF_ONLY_AST) self.assertRaises(TypeError, compile, co1, '<ast>', 'eval') # raise exception when node type is no start node self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec') # raise exception when node has invalid children ast = _ast.Module() ast.body = [_ast.BoolOp()] self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
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
def visit_Assign(self, node): if len(node.targets) > 1: raise SyntaxError("GPUMAP: multiple assignment not supported") target_types = map(lambda target: type(target), node.targets) if tuple in target_types or list in target_types: raise SyntaxError("GPUMAP: No unpacking allowed") target = node.targets[0] # assignment into object field output = "" value = self.visit(node.value) # assignment into variable if isinstance(target, _ast.Name): # assignment into new variable # not sure about the type just yet.. # see if it's a primitive if target.id not in self.local_vars: # binops and boolops return primitives if isinstance(node.value, _ast.Num) or isinstance(node.value, _ast.Compare) or isinstance(node.value, _ast.BinOp) \ or isinstance(node.value, _ast.BoolOp) or isinstance(node.value, _ast.NameConstant): output += "auto " # check if referenced list contains primitives elif isinstance(node.value, _ast.Subscript): list_name = value[:value.find("[")] try: idx = self.func_repr.args.index(list_name) t = self.func_repr.arg_types[idx] item_type = t[t.find("<") + 1: t.find(">")] if item_type in map(lambda t: t.__name__, primitive_map.keys()): output += "auto " else: output += "auto&& " except: raise RuntimeError("THIS SHOULD NEVER HAPPEN") else: # check if it's an existing variable try: idx = self.func_repr.args.index(value) t = self.func_repr.arg_types[idx] if t in primitive_map: output += "auto " else: output += "auto&& " except ValueError: output += "auto&& " self.local_vars[target.id] = None output += self.visit(target) output += " = " + value return output