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


Python ast.Div方法代碼示例

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


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

示例1: test_MutateAST_visit_augassign

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def test_MutateAST_visit_augassign(augassign_file, augassign_expected_locs):
    """Test mutation for AugAssign: +=, -=, /=, *=."""
    tree = Genome(augassign_file).ast
    test_mutation = "AugAssign_Div"

    testing_tree = deepcopy(tree)
    mutated_tree = MutateAST(target_idx=augassign_expected_locs[0], mutation=test_mutation).visit(
        testing_tree
    )

    mast = MutateAST(readonly=True)
    mast.visit(mutated_tree)

    assert len(mast.locs) == 4

    for loc in mast.locs:
        # spot check on mutation from Add tp Div
        if loc.lineno == 1 and loc.col_offset == 4:
            assert loc.op_type == test_mutation

        # spot check on not-mutated location still being Mult
        if loc.lineno == 5 and loc.col_offset == 4:
            assert loc.op_type == "AugAssign_Mult" 
開發者ID:EvanKepner,項目名稱:mutatest,代碼行數:25,代碼來源:test_transformers.py

示例2: term_rewrite

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def term_rewrite(head, tail):
    if tail:
        for op, each in tail:
            head = ast.BinOp(
                head,
                {
                    '*': ast.Mult,
                    '@': ast.MatMult,
                    '%': ast.Mod,
                    '//': ast.FloorDiv,
                    '/': ast.Div
                }[op.value](),
                each,
                **loc @ op,
            )
    return head 
開發者ID:Xython,項目名稱:YAPyPy,代碼行數:18,代碼來源:helper.py

示例3: augassign_rewrite

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def augassign_rewrite(it: Tokenizer):
    return {
        '+=': ast.Add,
        '-=': ast.Sub,
        '*=': ast.Mult,
        '/=': ast.Div,
        '//=': ast.FloorDiv,
        '@=': ast.MatMult,
        '%=': ast.Mod,
        '&=': ast.BitAnd,
        '|=': ast.BitOr,
        '^=': ast.BitXor,
        '<<=': ast.LShift,
        '>>=': ast.RShift,
        '**=': ast.Pow,
    }[it.value] 
開發者ID:Xython,項目名稱:YAPyPy,代碼行數:18,代碼來源:helper.py

示例4: handle_bin_op

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def handle_bin_op(expr, **kwargs) -> Optional[Token]:
    token_info = dict(line=expr.lineno, col=expr.col_offset)
    if isinstance(expr.op, ast.Div) or expr.op == '/':
        if isinstance(expr.right, astroid.node_classes.NodeNG):
            guesses = infer(expr=expr.right)
            token_info['col'] = expr.right.col_offset
            for guess in guesses:
                if type(guess) is not astroid.Const:
                    continue
                return Token(value=ZeroDivisionError, **token_info)
        if isinstance(expr.right, ast.Num) and expr.right.n == 0:
            token_info['col'] = expr.right.col_offset
            return Token(value=ZeroDivisionError, **token_info)
    return None


# exit() 
開發者ID:life4,項目名稱:deal,代碼行數:19,代碼來源:exceptions.py

示例5: test_mutate_TypeError_source_file

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def test_mutate_TypeError_source_file(mock_LocIdx):
    """Mutate with a NoneType source_file property raises a TypeError."""
    genome = Genome()
    with pytest.raises(TypeError):
        _ = genome.mutate(target_idx=mock_LocIdx, mutation_op=ast.Div, write_cache=False) 
開發者ID:EvanKepner,項目名稱:mutatest,代碼行數:7,代碼來源:test_api.py

示例6: test_mutate_ValueError_target

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def test_mutate_ValueError_target(binop_file, mock_LocIdx):
    """Mutate with a target_idx not in the targets raises a ValueError."""
    genome = Genome(binop_file)
    with pytest.raises(ValueError):
        _ = genome.mutate(target_idx=mock_LocIdx, mutation_op=ast.Div, write_cache=False) 
開發者ID:EvanKepner,項目名稱:mutatest,代碼行數:7,代碼來源:test_api.py

示例7: visit_BinOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def visit_BinOp(self, node):
        if isinstance(node.op, ast.Div):
            self.raiseError(node, _error.NotSupported, "true division - consider '//'") 
開發者ID:myhdl,項目名稱:myhdl,代碼行數:5,代碼來源:_analyze.py

示例8: onReturnPressed

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def onReturnPressed(self):

        expr = str(self.ui.lineEdit.text())


        import ast
        import operator as op
        # supported operators
        operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul,
                     ast.Div: op.floordiv, ast.Pow: op.pow, ast.USub: op.neg}

        def eval_expr(expr):
            return eval_(ast.parse(expr, mode='eval').body)

        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, object):
                # handle constants
                k = str(node.id).upper()
                if k in self.konstants:
                    return self.konstants[k](k)
                else:
                    raise TypeError(node)
            else:
                raise TypeError(node)

        try:
            result = eval_expr(expr)
        except Exception as e:
            self.ui.label.setText('error.')
            return
        

        self.ui.label.setText('{0} ({1})'.format(hex(result), result))

        self.onResult(result) 
開發者ID:mtivadar,項目名稱:qiew,代碼行數:43,代碼來源:FileFormat.py

示例9: __init__

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def __init__( s, component ):
    super().__init__( component )
    s.loop_var_env = set()
    s.tmp_var_env = set()

    # opmap maps an ast operator to its RTLIR counterpart.
    s.opmap = {
      # Bool operators
      # Note: we do not support boolean operators because Python does
      # not allow overloading And and Or operators. Using them in
      # expressions might lead to inconsistent semantics.
      # ast.And    : bir.And(),       ast.Or     : bir.Or(),
      # Unary operators
      # Note: ast.Not is disallowed because it is a boolean operator
      # ast.Not    : bir.Not(),
      ast.Invert : bir.Invert(),
      ast.UAdd   : bir.UAdd(),      ast.USub   : bir.USub(),
      # Binary operators
      ast.Add    : bir.Add(),       ast.Sub    : bir.Sub(),
      ast.Mult   : bir.Mult(),      ast.Div    : bir.Div(),
      ast.Mod    : bir.Mod(),       ast.Pow    : bir.Pow(),
      ast.LShift : bir.ShiftLeft(), ast.RShift : bir.ShiftRightLogic(),
      ast.BitOr  : bir.BitOr(),     ast.BitAnd : bir.BitAnd(),
      ast.BitXor : bir.BitXor(),
      # Compare bir.bir.operators
      ast.Eq     : bir.Eq(),        ast.NotEq  : bir.NotEq(),
      ast.Lt     : bir.Lt(),        ast.LtE    : bir.LtE(),
      ast.Gt     : bir.Gt(),        ast.GtE    : bir.GtE()
    }

  # Override 
開發者ID:pymtl,項目名稱:pymtl3,代碼行數:33,代碼來源:BehavioralRTLIRGenL2Pass.py

示例10: stringify_operation

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def stringify_operation(op: Any) -> str:
        if isinstance(op, ast.Add):
            return '+'
        if isinstance(op, ast.Sub):
            return '-'
        if isinstance(op, ast.Mult):
            return '*'
        if isinstance(op, ast.Div) or isinstance(op, ast.FloorDiv):
            return '/'
        return str(op) 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:12,代碼來源:expressions.py

示例11: visit_BinOp

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def visit_BinOp(self, node: ast.BinOp) -> Any:
        """Recursively visit the left and right operand, respectively, and apply the operation on the results."""
        # pylint: disable=too-many-branches
        left = self.visit(node=node.left)
        right = self.visit(node=node.right)

        if isinstance(node.op, ast.Add):
            result = left + right
        elif isinstance(node.op, ast.Sub):
            result = left - right
        elif isinstance(node.op, ast.Mult):
            result = left * right
        elif isinstance(node.op, ast.Div):
            result = left / right
        elif isinstance(node.op, ast.FloorDiv):
            result = left // right
        elif isinstance(node.op, ast.Mod):
            result = left % right
        elif isinstance(node.op, ast.Pow):
            result = left**right
        elif isinstance(node.op, ast.LShift):
            result = left << right
        elif isinstance(node.op, ast.RShift):
            result = left >> right
        elif isinstance(node.op, ast.BitOr):
            result = left | right
        elif isinstance(node.op, ast.BitXor):
            result = left ^ right
        elif isinstance(node.op, ast.BitAnd):
            result = left & right
        elif isinstance(node.op, ast.MatMult):
            result = left @ right
        else:
            raise NotImplementedError("Unhandled op of {}: {}".format(node, node.op))

        self.recomputed_values[node] = result
        return result 
開發者ID:Parquery,項目名稱:icontract,代碼行數:39,代碼來源:_recompute.py

示例12: binop

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def binop(self, block, node, var=None):
    if var is None:
      var = self.add_variable(None)
    left = self.expression(block, node.left)
    right = self.expression(block, node.right)
    if isinstance(node.op, ast.Add):
      op = Operator.add
    elif isinstance(node.op, ast.Sub):
      op = Operator.subtract
    elif isinstance(node.op, ast.Mult):
      op = Operator.multiply
    elif isinstance(node.op, ast.Div):
      op = Operator.divide
    elif isinstance(node.op, ast.FloorDiv):
      op = Operator.divide_int
    elif isinstance(node.op, ast.Mod):
      op = Operator.mod
    elif isinstance(node.op, ast.Pow):
      op = Operator.pow
    elif isinstance(node.op, ast.BitAnd):
      op = Operator.bit_and
    elif isinstance(node.op, ast.BitOr):
      op = Operator.bit_or
    elif isinstance(node.op, ast.BitXor):
      op = Operator.bit_xor
    elif isinstance(node.op, ast.LShift):
      op = Operator.shift_left
    elif isinstance(node.op, ast.RShift):
      op = Operator.shift_right
    else:
      raise Exception('Unexpected BinOp (%s)'%(node.op.__class__))
    operation = BinaryOperation(left, op, right)
    assignment = Assignment(var, operation)
    block.add(assignment)
    return var

  #Parses a unary uperation (AST UnaryOp) 
開發者ID:undefx,項目名稱:vecpy,代碼行數:39,代碼來源:parser.py

示例13: p_augassign_4

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def p_augassign_4(p):
    '''augassign : SLASHEQUAL'''
    #                       1
    p[0] = ast.Div(rule=inspect.currentframe().f_code.co_name, **p[1][1]) 
開發者ID:histogrammar,項目名稱:histogrammar-python,代碼行數:6,代碼來源:hgawk_grammar.py

示例14: p_term_star_2

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def p_term_star_2(p):
    '''term_star : SLASH factor'''
    #                  1      2
    p[0] = [ast.Div(rule=inspect.currentframe().f_code.co_name, **p[1][1]), p[2]] 
開發者ID:histogrammar,項目名稱:histogrammar-python,代碼行數:6,代碼來源:hgawk_grammar.py

示例15: p_term_star_6

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Div [as 別名]
def p_term_star_6(p):
    '''term_star : term_star SLASH factor'''
    #                      1     2      3
    p[0] = p[1] + [ast.Div(rule=inspect.currentframe().f_code.co_name, **p[2][1]), p[3]] 
開發者ID:histogrammar,項目名稱:histogrammar-python,代碼行數:6,代碼來源:hgawk_grammar.py


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