本文整理匯總了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
示例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
示例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
示例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
示例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