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


Python _ast.BinOp方法代碼示例

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


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

示例1: visit_binop

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import BinOp [as 別名]
def visit_binop(self, node, parent):
        """visit a BinOp node by returning a fresh instance of it"""
        if isinstance(node.left, _ast.BinOp) and self._manager.optimize_ast:
            # Optimize BinOp operations in order to remove
            # redundant recursion. For instance, if the
            # following code is parsed in order to obtain
            # its ast, then the rebuilder will fail with an
            # infinite recursion, the same will happen with the
            # inference engine as well. There's no need to hold
            # so many objects for the BinOp if they can be reduced
            # to something else (also, the optimization
            # might handle only Const binops, which isn't a big
            # problem for the correctness of the program).
            #
            # ("a" + "b" + # one thousand more + "c")
            optimized = self._peepholer.optimize_binop(node, parent)
            if optimized:
                return optimized

        newnode = nodes.BinOp(_BIN_OP_CLASSES[type(node.op)],
                              node.lineno, node.col_offset, parent)
        newnode.postinit(self.visit(node.left, newnode),
                         self.visit(node.right, newnode))
        return newnode 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:26,代碼來源:rebuilder.py

示例2: make_binary_op

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import BinOp [as 別名]
def make_binary_op(i, bytecode):
    op = bytecode[i][2]
    bin_op = Statement.BIN_OP_AST_NODE_MAP[op]()
    i, rhs = Statement.make_expr(i - 1, bytecode)
    i, lhs = Statement.make_expr(i - 1, bytecode)
    return i, _ast.BinOp(lhs, bin_op, rhs)


  # Build Attr(value=Attr(value=Name(id=a), attr=b), attr=c) <=> a.b.c 
開發者ID:neuroo,項目名稱:equip,代碼行數:11,代碼來源:stmt.py

示例3: visit_binop

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import BinOp [as 別名]
def visit_binop(self, node: _ast.BinOp):  # left, op, right
        op = BINOP_TABLE.get(node.op.__class__)

        if op:
            return op(self._run(node.left), self._run(node.right))
        raise NotImplementedError 
開發者ID:item4,項目名稱:yui,代碼行數:8,代碼來源:calc.py

示例4: unparse

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import BinOp [as 別名]
def unparse(node, strip=None):
    result = astunparse.unparse(node)
    if strip:
        result = result.lstrip().rstrip()
        if isinstance(node, _ast.BinOp):
            return result[1:-1]
    return result 
開發者ID:ulope,項目名稱:pyformat.info,代碼行數:9,代碼來源:main.py

示例5: optimize_binop

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import BinOp [as 別名]
def optimize_binop(self, node, parent=None):
        """Optimize BinOps with string Const nodes on the lhs.

        This fixes an infinite recursion crash, where multiple
        strings are joined using the addition operator. With a
        sufficient number of such strings, astroid will fail
        with a maximum recursion limit exceeded. The
        function will return a Const node with all the strings
        already joined.
        Return ``None`` if no AST node can be obtained
        through optimization.
        """
        ast_nodes = []
        current = node
        while isinstance(current, _ast.BinOp):
            # lhs must be a BinOp with the addition operand.
            if not isinstance(current.left, _ast.BinOp):
                return None
            if (not isinstance(current.left.op, _ast.Add)
                    or not isinstance(current.op, _ast.Add)):
                return None

            # rhs must a str / bytes.
            if not isinstance(current.right, _TYPES):
                return None

            ast_nodes.append(current.right.s)
            current = current.left

            if (isinstance(current, _ast.BinOp)
                    and isinstance(current.left, _TYPES)
                    and isinstance(current.right, _TYPES)):
                # Stop early if we are at the last BinOp in
                # the operation
                ast_nodes.append(current.right.s)
                ast_nodes.append(current.left.s)
                break

        if not ast_nodes:
            return None

        # If we have inconsistent types, bail out.
        known = type(ast_nodes[0])
        if any(not isinstance(element, known)
               for element in ast_nodes[1:]):
            return None

        value = known().join(reversed(ast_nodes))
        newnode = nodes.Const(value, node.lineno, node.col_offset, parent)
        return newnode 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:52,代碼來源:astpeephole.py


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