當前位置: 首頁>>代碼示例>>Python>>正文


Python ast.BoolOp方法代碼示例

本文整理匯總了Python中ast.BoolOp方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.BoolOp方法的具體用法?Python ast.BoolOp怎麽用?Python ast.BoolOp使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ast的用法示例。


在下文中一共展示了ast.BoolOp方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: visit_BoolOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def visit_BoolOp(self, node: ast.BoolOp) -> ast.AST:
        """Boolean operations, AND/OR."""
        self.generic_visit(node)
        log_header = f"visit_BoolOp: {self.src_file}:"

        node_span = NodeSpan(node)
        idx = LocIndex(
            ast_class="BoolOp",
            lineno=node_span.lineno,
            col_offset=node_span.col_offset,
            op_type=type(node.op),
            end_lineno=node_span.end_lineno,
            end_col_offset=node_span.end_col_offset,
        )
        self.locs.add(idx)

        if idx == self.target_idx and self.mutation and not self.readonly:
            LOGGER.debug("%s mutating idx: %s with %s", log_header, self.target_idx, self.mutation)
            return ast.copy_location(ast.BoolOp(op=self.mutation(), values=node.values), node)

        LOGGER.debug("%s (%s, %s): no mutations applied.", log_header, node.lineno, node.col_offset)
        return node 
開發者ID:EvanKepner,項目名稱:mutatest,代碼行數:24,代碼來源:transformers.py

示例2: visit_Compare

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def visit_Compare(self, node, **kwargs):
        ops = node.ops
        comps = node.comparators

        # base case: we have something like a CMP b
        if len(comps) == 1:
            op = self.translate_In(ops[0])
            binop = ast.BinOp(op=op, left=node.left, right=comps[0])
            return self.visit(binop)

        # recursive case: we have a chained comparison, a CMP b CMP c, etc.
        left = node.left
        values = []
        for op, comp in zip(ops, comps):
            new_node = self.visit(ast.Compare(comparators=[comp], left=left,
                                              ops=[self.translate_In(op)]))
            left = comp
            values.append(new_node)
        return self.visit(ast.BoolOp(op=ast.And(), values=values)) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:21,代碼來源:expr.py

示例3: test_afterSubMult

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def test_afterSubMult(self):
        'Tests after SubToMult pre-processing'

        tests = [("1 + 2 - 3", ast.BoolOp(ast.Add(), [ast.Num(1), ast.Num(2),
                                                      ast.BinOp(ast.Num(-1),
                                                                ast.Mult(),
                                                                ast.Num(3))])),
                 ("1 + 2 - 3 + 4", ast.BoolOp(ast.Add(),
                                              [ast.Num(1),
                                               ast.Num(2),
                                               ast.BinOp(ast.Num(-1),
                                                         ast.Mult(),
                                                         ast.Num(3)),
                                               ast.Num(4)])),
                 ("(1 + 2) - (3 + 4)",
                  ast.BoolOp(ast.Add(),
                             [ast.Num(1), ast.Num(2),
                              ast.BinOp(ast.Num(-1), ast.Mult(),
                                        ast.BinOp(ast.Num(3), ast.Add(),
                                                  ast.Num(4)))]))]
        for teststring, ref_ast in tests:
            test_ast = ast.parse(teststring, mode="eval").body
            test_ast = pre_processing.all_preprocessings(test_ast)
            test_ast = Flattening(ast.Add).visit(test_ast)
            self.assertTrue(Comparator().visit(test_ast, ref_ast)) 
開發者ID:quarkslab,項目名稱:sspam,代碼行數:27,代碼來源:test_flattening.py

示例4: test_onBoolOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def test_onBoolOp(self):
        'Tests on BoolOp'

        expr_a = ast.BoolOp(ast.Add(), [ast.Num(1), ast.Num(2), ast.Num(3)])
        expr_b = ast.BoolOp(ast.Add(), [ast.Num(3), ast.Num(2), ast.Num(1)])
        self.assertTrue(asttools.Comparator().visit(expr_a, expr_b))

        expr_a = ast.BoolOp(ast.Add, [ast.Num(1), ast.BoolOp(ast.Mult(),
                                                             [ast.Num(5),
                                                              ast.Num(6)]),
                                      ast.Num(4)])
        expr_b = ast.BoolOp(ast.Add, [ast.BoolOp(ast.Mult(), [ast.Num(6),
                                                              ast.Num(5)]),
                                      ast.Num(4),
                                      ast.Num(1)])
        self.assertTrue(asttools.Comparator().visit(expr_a, expr_b)) 
開發者ID:quarkslab,項目名稱:sspam,代碼行數:18,代碼來源:test_asttools.py

示例5: __init__

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def __init__(self, patt_ast, target_ast, rep_ast, nbits=0):
        'Pattern ast should have as root: BinOp, BoolOp, UnaryOp or Call'
        if isinstance(patt_ast, ast.Module):
            self.patt_ast = patt_ast.body[0].value
        elif isinstance(patt_ast, ast.Expression):
            self.patt_ast = patt_ast.body
        else:
            self.patt_ast = patt_ast
        if isinstance(rep_ast, ast.Module):
            self.rep_ast = deepcopy(rep_ast.body[0].value)
        elif isinstance(rep_ast, ast.Expression):
            self.rep_ast = deepcopy(rep_ast.body)
        else:
            self.rep_ast = deepcopy(rep_ast)

        if not nbits:
            getsize = asttools.GetSize()
            getsize.visit(target_ast)
            if getsize.result:
                self.nbits = getsize.result
            # default bitsize is 8
            else:
                self.nbits = 8
        else:
            self.nbits = nbits 
開發者ID:quarkslab,項目名稱:sspam,代碼行數:27,代碼來源:pattern_matcher.py

示例6: visit_BoolOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def visit_BoolOp(self, node):
        'A custom BoolOp can be used in flattened AST'
        if type(node.op) not in (ast.Add, ast.Mult,
                                 ast.BitXor, ast.BitAnd, ast.BitOr):
            return self.generic_visit(node)
        # get constant parts of node:
        list_cste = [child for child in node.values
                     if isinstance(child, ast.Num)]
        if len(list_cste) < 2:
            return self.generic_visit(node)
        rest_values = [n for n in node.values if n not in list_cste]
        fake_node = Unflattening().visit(ast.BoolOp(node.op, list_cste))
        fake_node = ast.Expression(fake_node)
        ast.fix_missing_locations(fake_node)
        code = compile(fake_node, '<constant folding>', 'eval')
        obj_env = globals().copy()
        exec code in obj_env
        value = eval(code, obj_env)

        new_node = ast.Num(value)
        rest_values.append(new_node)
        return ast.BoolOp(node.op, rest_values) 
開發者ID:quarkslab,項目名稱:sspam,代碼行數:24,代碼來源:asttools.py

示例7: normalize_compare

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def normalize_compare(node):
    """Rewrites a compare expression to a `and` expression
    1 < 2 < 3 > 0
    1 < 2 and 2 < 3 and 3 > 0"""
    and_values = []
    left = node.left
    for (op, val) in zip(node.ops, node.comparators):
        comp = ast.Compare(ops=[op],
                           left=left,
                           comparators=[val],
                           lineno=node.lineno,
                           col_offset=node.col_offset)
        and_values.append(comp)
        left = val
    return ast.BoolOp(op=ast.And(),
                      values=and_values,
                      lineno=node.lineno,
                      col_offset=node.col_offset) 
開發者ID:danhper,項目名稱:bigcode-tools,代碼行數:20,代碼來源:normalizer.py

示例8: merge_exitcases

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def merge_exitcases(exit1, exit2):
    """
    Merge the exitcases of two Links.

    Args:
        exit1: The exitcase of a Link object.
        exit2: Another exitcase to merge with exit1.

    Returns:
        The merged exitcases.
    """
    if exit1:
        if exit2:
            return ast.BoolOp(ast.And(), values=[exit1, exit2])
        return exit1
    return exit2 
開發者ID:coetaur0,項目名稱:staticfg,代碼行數:18,代碼來源:builder.py

示例9: verify_ast

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def verify_ast(expr, variables_and_values):
  """Verifies that |expr| is of the form
  expr ::= expr ( "or" | "and" ) expr
         | identifier "==" ( string | int )
  Also collects the variable identifiers and string/int values in the dict
  |variables_and_values|, in the form {'var': set([val1, val2, ...]), ...}.
  """
  assert isinstance(expr, (ast.BoolOp, ast.Compare))
  if isinstance(expr, ast.BoolOp):
    assert isinstance(expr.op, (ast.And, ast.Or))
    for subexpr in expr.values:
      verify_ast(subexpr, variables_and_values)
  else:
    assert isinstance(expr.left.ctx, ast.Load)
    assert len(expr.ops) == 1
    assert isinstance(expr.ops[0], ast.Eq)
    var_values = variables_and_values.setdefault(expr.left.id, set())
    rhs = expr.comparators[0]
    assert isinstance(rhs, (ast.Str, ast.Num))
    var_values.add(rhs.n if isinstance(rhs, ast.Num) else rhs.s) 
開發者ID:luci,項目名稱:luci-py,代碼行數:22,代碼來源:isolate_format.py

示例10: boolop

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def boolop(self, block, node, var=None):
    #Get the type of operation
    if isinstance(node.op, ast.And):
      op = Operator.bool_and
    elif isinstance(node.op, ast.Or):
      op = Operator.bool_or
    else:
      raise Exception('Unexpected BoolOp (%s)'%(node.op.__class__))
    #Chain operations together
    left = self.expression(block, node.values[0])
    for node_value in node.values[1:]:
      right = self.expression(block, node_value)
      operation = BinaryOperation(left, op, right)
      if node_value == node.values[-1] and var is not None:
        #The last operation in the chain should be assigned to this variable
        temp_var = var
      else:
        #Create a temporary variable to store the intermediate result
        temp_var = self.add_variable(None, is_mask=True)
      assignment = Assignment(temp_var, operation)
      block.add(assignment)
      left = temp_var
    return temp_var

  #Parses an array element (AST Subscript) 
開發者ID:undefx,項目名稱:vecpy,代碼行數:27,代碼來源:parser.py

示例11: eval_expr

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def eval_expr(expr):

    """ Eval and expression inside a #define using a suppart of python grammar """

    def _eval(node):
        if isinstance(node, ast.Num):
            return node.n
        elif isinstance(node, ast.BinOp):
            return OPERATORS[type(node.op)](_eval(node.left), _eval(node.right))
        elif isinstance(node, ast.UnaryOp):
            return OPERATORS[type(node.op)](_eval(node.operand))
        elif isinstance(node, ast.BoolOp):
            values = [_eval(x) for x in node.values]
            return OPERATORS[type(node.op)](**values)
        else:
            raise TypeError(node)

    return _eval(ast.parse(expr, mode='eval').body) 
開發者ID:ynsta,項目名稱:steamcontroller,代碼行數:20,代碼來源:cheader.py

示例12: normalize_boolop

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def normalize_boolop(expr):
    """
    Normalize a boolop by folding together nested And/Or exprs.
    """
    optype = expr.op
    newvalues = []
    for subexpr in expr.values:
        if not isinstance(subexpr, ast.BoolOp):
            newvalues.append(subexpr)
        elif type(subexpr.op) != type(optype):
            newvalues.append(normalize_boolop(subexpr))
        else:
            # Normalize subexpression, then inline its values into the
            # top-level subexpr.
            newvalues.extend(normalize_boolop(subexpr).values)
    return ast.BoolOp(op=optype, values=newvalues) 
開發者ID:llllllllll,項目名稱:codetransformer,代碼行數:18,代碼來源:_343.py

示例13: p_logic_or_expr

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def p_logic_or_expr(t):
    '''logic_or_expr : logic_or_expr OR logic_and_expr
                     | logic_and_expr'''
    if len(t) == 4:
        if isinstance(t[1], ast.BoolOp) and isinstance(t[1].op, ast.Or):
            t[0] = t[1]
            t[0].values.append(t[3])
        else:
            or_ast = ast.Or()
            or_ast.lineno = t.lineno(2)
            or_ast.col_offset = -1 # XXX
            t[0] = ast.BoolOp(or_ast, [t[1], t[3]])
            t[0].lineno = t.lineno(2)
            t[0].col_offset = -1 # XXX
    else:
        t[0] = t[1] 
開發者ID:yaksok,項目名稱:yaksok,代碼行數:18,代碼來源:yacc.py

示例14: p_logic_and_expr

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def p_logic_and_expr(t):
    '''logic_and_expr : logic_and_expr AND logic_expr
                      | logic_expr'''
    if len(t) == 4:
        if isinstance(t[1], ast.BoolOp) and isinstance(t[1].op, ast.And):
            t[0] = t[1]
            t[0].values.append(t[3])
        else:
            and_ast = ast.And()
            and_ast.lineno = t.lineno(2)
            and_ast.col_offset = -1 # XXX
            t[0] = ast.BoolOp(and_ast, [t[1], t[3]])
            t[0].lineno = t.lineno(2)
            t[0].col_offset = -1 # XXX
    else:
        t[0] = t[1] 
開發者ID:yaksok,項目名稱:yaksok,代碼行數:18,代碼來源:yacc.py

示例15: evaluate_node

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import BoolOp [as 別名]
def evaluate_node(self, node):
        if isinstance(node, ast.BinOp):
            return self.operators[type(node.op)](self.evaluate_node(node.left),
                                                 self.evaluate_node(node.right))
        elif isinstance(node, ast.UnaryOp):
            return self.operators[type(node.op)](self.evaluate_node(node.operand))
        elif isinstance(node, ast.Compare):
            to_string = isinstance(node.comparators[0], ast.Str)

            return self.operators[type(node.ops[0])](self.evaluate_attribute_node(node.left, to_string),
                                                     self.evaluate_node(node.comparators[0]))
        elif isinstance(node, ast.BoolOp):
            func = all if isinstance(node.op, ast.And) else any
            return func(self.evaluate_node(value) for value in node.values)
        elif isinstance(node, ast.Str):
            return node.s
        elif isinstance(node, ast.Attribute):
            return self.evaluate_attribute_node(node)
        elif isinstance(node, ast.Num):
            return node.n
        else:
            logger.error("Error during parsing") 
開發者ID:jopohl,項目名稱:urh,代碼行數:24,代碼來源:SimulatorExpressionParser.py


注:本文中的ast.BoolOp方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。