本文整理汇总了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