本文整理匯總了Python中_ast.AugAssign方法的典型用法代碼示例。如果您正苦於以下問題:Python _ast.AugAssign方法的具體用法?Python _ast.AugAssign怎麽用?Python _ast.AugAssign使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類_ast
的用法示例。
在下文中一共展示了_ast.AugAssign方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: run
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import AugAssign [as 別名]
def run(self, node, input_state):
result_state = input_state.copy()
block = node.data
constraints = self.cfg.block_constraints
for stmt in block.statements:
native = stmt.native
if isinstance(native, _ast.Assign) or isinstance(native, _ast.AugAssign):
self.transfer_assign(result_state, native, stmt.start_bytecode_index)
elif isinstance(native, _ast.Expr):
value = native.value
logger.debug("Stmt kind: %s", type(value))
if isinstance(value, _ast.Call):
self.transfer_call(result_state, native, stmt.start_bytecode_index)
else:
logger.error("Unknown stmt: %s", dump_native_ast(native))
return result_state
# Assign: a <- b
示例2: visit_augassign
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import AugAssign [as 別名]
def visit_augassign(self, node: _ast.AugAssign): # target, op, value
value = self._run(node.value)
target = node.target
target_cls = target.__class__
op_cls = node.op.__class__
if target_cls == _ast.Name:
target_id = target.id # type: ignore
self.symbol_table[target_id] = BINOP_TABLE[op_cls](
self.symbol_table[target_id], value,
)
elif target_cls == _ast.Subscript:
sym = self._run(target.value) # type: ignore
xslice = self._run(target.slice) # type: ignore
if isinstance(target.slice, _ast.Index): # type: ignore
sym[xslice] = BINOP_TABLE[op_cls](sym[xslice], value,)
else:
raise BadSyntax('This assign method is not allowed')
else:
raise BadSyntax('This assign method is not allowed')
return
示例3: run
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import AugAssign [as 別名]
def run(self, node, input_state):
result_state = input_state.copy()
block = node.data
for stmt in block.statements:
native = stmt.native
if not native:
continue
if isinstance(native, _ast.Assign) or isinstance(native, _ast.AugAssign):
self.transfer_assign(result_state, native, stmt.start_bytecode_index)
else:
self.transfer_load(result_state, native, stmt.start_bytecode_index)
return result_state
示例4: __init__
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import AugAssign [as 別名]
def __init__(self, name, source, scope):
if '__all__' in scope and isinstance(source, ast.AugAssign):
self.names = list(scope['__all__'].names)
else:
self.names = []
if isinstance(source.value, (ast.List, ast.Tuple)):
for node in source.value.elts:
if isinstance(node, ast.Str):
self.names.append(node.s)
super(ExportBinding, self).__init__(name, source)
示例5: make_assign
# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import AugAssign [as 別名]
def make_assign(i, bytecode):
op = bytecode[i][2]
if op == STORE_SUBSCR:
return Statement.make_subscript(i, bytecode)
prev_op = bytecode[i - 1][2] if i > 0 else -1
if prev_op in INPLACE_OPCODES:
in_cls = Statement.INPLACE_OPERATORS[prev_op]
i -= 1
i, rhs = Statement.make_expr(i - 1, bytecode, context=_ast.AugStore())
i, lhs = Statement.make_expr(i - 1, bytecode, context=_ast.AugLoad())
return i, _ast.AugAssign(lhs, in_cls(), rhs)
else:
# We can either have multiple assignments: a = b = c = 1
# or unpacked sequences: a, b = 1, foo()
# the compiler does some optimization so that: a, b = c, d
# does not rely on UNPACK_SEQUENCE, but a ROT_TWO (or ROT_THREE & ROT_TWO for 3 elements).
# This happens for 2 or 3 elements to unpack
targets = []
value = None
has_unpack, has_ROT_2_3, has_multiple = False, False, 0
num_unpack = -1
j = i
while j >= 0:
op = bytecode[j][2]
if op == UNPACK_SEQUENCE:
has_unpack = True
num_unpack = bytecode[j][3]
break
elif op in (ROT_TWO, ROT_THREE):
has_ROT_2_3 = True
break
if op == DUP_TOP:
has_multiple += 1
j -= 1
if has_unpack:
return Statement.make_assign_unpack(i, bytecode, unpack_num=num_unpack)
elif has_ROT_2_3:
return Statement.make_assign_opt_unpack(i, bytecode)
elif has_multiple > 0:
return Statement.make_assign_chained(i, bytecode)
else:
# A simple assignment
i, store_expr = Statement.make_expr(i, bytecode)
i, value_expr = Statement.make_expr(i - 1, bytecode)
return i, _ast.Assign([store_expr], value_expr)
return i, None
# 2 cases here:
# (1) a, b, c = foo() <=> v = foo(), a = v[0], b = v[1], c = v[2]
# => AST: _ast.Assign(targets=[Tuple(a, b, c)], value=foo())
#
# (2) a, b = foo(), bar() <=> a = foo(), b = bar()
# => AST: _ast.Assign(targets=[Tuple(a, b)], value=Tuple(baz(), bar()))