本文整理汇总了Python中_ast.Assign方法的典型用法代码示例。如果您正苦于以下问题:Python _ast.Assign方法的具体用法?Python _ast.Assign怎么用?Python _ast.Assign使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类_ast
的用法示例。
在下文中一共展示了_ast.Assign方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_assign_chained
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def make_assign_chained(i, bytecode):
store_exprs = []
value_exprs = []
store_state, value_state = True, False
while i >= 0:
op, arg = bytecode[i][2], bytecode[i][3]
if store_state:
if op == DUP_TOP:
prev_op = bytecode[i - 1][2] if i > 0 else -1
if prev_op not in STORE_OPCODES:
value_state = True
store_state = False
elif op in STORE_OPCODES:
i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store())
store_exprs.insert(0, store_stmt)
elif value_state:
i, value_exprs = Statement.make_expr(i, bytecode)
break
i -= 1
store_exprs = _ast.Tuple(store_exprs, _ast.Store())
return i, _ast.Assign([store_exprs], value_exprs)
示例2: run
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [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
示例3: test_oauth_auth_example
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def test_oauth_auth_example():
import ast
import _ast
import codegen
client_id = "n4mnzQGfDEfOhFixwBvLV2mZJJLvf86pzfMMiPF5"
client_secret = "40ON9IPJRDAngUkVbGBTEjCBAwc2wB7lV8e71jJUPKabdKq6KBTUBKb1xGkh82KtAI1AqISrL3Zi4sTfhCBVh27YvlV6Y5klpXXV5loUWvuhMSRiN3HRZzVDO0fLBibv"
with open("examples/oauth_auth_example.py", "r") as f:
data = f.read()
p = ast.parse(data)
for node in p.body:
if type(node) == _ast.Assign:
if node.targets[0].id == 'client_id':
node.value.s = client_id
if node.targets[0].id == 'client_secret':
node.value.s = client_secret
ls = {}
exec(codegen.to_source(p), ls)
assert ls['course']['courses'][0]['id'] == 67
示例4: _list_tasks
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def _list_tasks():
"""
Fetches a list of all valid tasks that may be run, and the args they
accept. Does not actually import the task module to prevent errors if a
user does not have the dependencies installed for every task.
:return:
A list of 2-element tuples:
0: a unicode string of the task name
1: a list of dicts containing the parameter definitions
"""
out = []
dev_path = os.path.join(package_root, 'dev')
for fname in sorted(os.listdir(dev_path)):
if fname.startswith('.') or fname.startswith('_'):
continue
if not fname.endswith('.py'):
continue
name = fname[:-3]
args = ()
full_path = os.path.join(package_root, 'dev', fname)
with open(full_path, 'rb') as f:
full_code = f.read()
if sys.version_info >= (3,):
full_code = full_code.decode('utf-8')
task_node = ast.parse(full_code, filename=full_path)
for node in ast.iter_child_nodes(task_node):
if isinstance(node, _ast.Assign):
if len(node.targets) == 1 \
and isinstance(node.targets[0], _ast.Name) \
and node.targets[0].id == 'run_args':
args = ast.literal_eval(node.value)
break
out.append((name, args))
return out
示例5: make_assign_unpack
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def make_assign_unpack(i, bytecode, unpack_num=-1):
if unpack_num < 1:
logger.error("Could not find the number of unpacked items. ")
return i, None
store_exprs = []
value_exprs = []
store_state, value_state = True, False
while i >= 0:
op, arg = bytecode[i][2], bytecode[i][3]
if store_state:
if op == UNPACK_SEQUENCE:
store_state = False
prev_op = bytecode[i - 1][2] if i > 0 else -1
if prev_op == BUILD_TUPLE:
value_state = True
else:
i, value_exprs = Statement.make_expr(i - 1, bytecode)
break
elif op in STORE_OPCODES:
i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store())
store_exprs.insert(0, store_stmt)
elif value_state:
i, value_stmt = Statement.make_expr(i, bytecode)
value_exprs.insert(0, value_stmt)
i -= 1
store_exprs = _ast.Tuple(store_exprs, _ast.Store())
if not isinstance(value_exprs, _ast.AST):
value_exprs = _ast.Tuple(value_exprs, _ast.Load())
return i, _ast.Assign([store_exprs], value_exprs)
示例6: make_assign_opt_unpack
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def make_assign_opt_unpack(i, bytecode):
store_exprs = []
value_exprs = []
store_state, value_state = True, False
while i >= 0:
op, arg = bytecode[i][2], bytecode[i][3]
if store_state:
if op == ROT_TWO:
prev_op = bytecode[i - 1][2] if i > 0 else -1
if prev_op == ROT_THREE:
i -= 1
value_state = True
store_state = False
elif op in STORE_OPCODES:
i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store())
store_exprs.insert(0, store_stmt)
elif value_state:
i, value_stmt = Statement.make_expr(i, bytecode)
value_exprs.insert(0, value_stmt)
i -= 1
store_exprs = _ast.Tuple(store_exprs, _ast.Store())
if not isinstance(value_exprs, _ast.AST):
value_exprs = _ast.Tuple(value_exprs, _ast.Load())
return i, _ast.Assign([store_exprs], value_exprs)
# Only one case here for:
# a = b = z.d.f = foo()
# => AST: _ast.Assign(targets=[Tuple(a, b, z.d.f)], value=foo())
示例7: make_store_delete_slice
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def make_store_delete_slice(i, bytecode, context=None):
op = bytecode[i][2]
is_delete = op in DELETE_SLICE_OPCODES
if context is None:
context = _ast.Store() if not is_delete else _ast.Del()
lhs_expr = None
if op in (STORE_SLICE_0, DELETE_SLICE_0):
i, lhs_expr = Statement.make_expr(i - 1, bytecode, context=context)
lhs_expr = _ast.Subscript(lhs_expr,
_ast.Slice(None, None, None),
_ast.Store())
elif op in (STORE_SLICE_1, STORE_SLICE_2, DELETE_SLICE_1, DELETE_SLICE_2):
i, index_expr = Statement.make_expr(i - 1, bytecode)
i, arr_expr = Statement.make_expr(i - 1, bytecode, context=context)
args = [None] * 3
index_index = 0 if op in (STORE_SLICE_1, DELETE_SLICE_1) else 1
args[index_index] = index_expr
lhs_expr = _ast.Subscript(arr_expr,
_ast.Slice(*args),
_ast.Store())
else:
i, end_index_expr = Statement.make_expr(i - 1, bytecode)
i, start_index_expr = Statement.make_expr(i - 1, bytecode)
i, arr_expr = Statement.make_expr(i - 1, bytecode, context=context)
lhs_expr = _ast.Subscript(arr_expr,
_ast.Slice(start_index_expr, end_index_expr, None),
_ast.Store())
if is_delete:
return i, _ast.Delete([lhs_expr])
else:
i, rhs_expr = Statement.make_expr(i - 1, bytecode)
return i, _ast.Assign([lhs_expr], rhs_expr)
示例8: run
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [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
示例9: update_use
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def update_use(stmt_state, var, index):
if var not in stmt_state[USE]:
stmt_state[USE][var] = set()
stmt_state[USE][var].add(index)
# Assign: a <- b
示例10: visit_assign
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def visit_assign(self, node: _ast.Assign): # targets, value
value = self._run(node.value)
for tnode in node.targets:
self.assign(tnode, value)
return
示例11: get_assignment_name
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def get_assignment_name(lines):
nodes = ast.parse(''.join(lines))
assignments = get_nodes_by_instance_type(nodes, _ast.Assign)
argparse_var = get_nodes_by_containing_attr(assignments, 'parse_args')
return argparse_var[0].value.func.value.id
示例12: extract_parser
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def extract_parser(modulepath, func_with_argparse):
source = read_client_module(modulepath)
nodes = ast.parse(''.join(source))
funcs = get_nodes_by_instance_type(nodes, _ast.FunctionDef)
assignment_objs = get_nodes_by_instance_type(nodes, _ast.Assign)
main_func = get_nodes_by_containing_attr(funcs, func_with_argparse)[0]
parse_args_assignment = get_nodes_by_containing_attr(main_func.body, 'parse_args')[0]
# ast reports the line no of a block structure as the start of the structure,
# not the end, so we look for the line no of the next node after main()
# and use that as the end of the main() function.
try:
restart_line = nodes.body[nodes.body.index(main_func)+1].lineno - 1
except IndexError:
restart_line = len(source)
module_source = format_source_to_return_parser(
source,
cutoff_line=parse_args_assignment.lineno,
restart_line=restart_line,
col_offset=parse_args_assignment.col_offset,
parser_name=parse_args_assignment.value.func.value.id
)
client_module = modules.load(module_source)
return getattr(client_module, func_with_argparse)()
示例13: isLiteralTupleUnpacking
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def isLiteralTupleUnpacking(self, node):
if isinstance(node, ast.Assign):
for child in node.targets + [node.value]:
if not hasattr(child, 'elts'):
return False
return True
示例14: parse_function
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [as 别名]
def parse_function(node):
old_style = None
new_style = None
output = None
setup = []
setup_done = False
title, details = parse_docstring(ast.get_docstring(node, clean=True))
name = node.name[5:] if node.name.startswith('test_') else node.name
for n in node.body:
# Ignore the docstring
if isinstance(n, _ast.Expr) and isinstance(n.value, _ast.Str):
continue
if isinstance(n, _ast.Assign) and n.targets[0].id == 'old_result':
setup_done = True
old_style = unparse(n.value, strip=True)
if isinstance(n, _ast.Assign) and n.targets[0].id == 'new_result':
setup_done = True
new_style = unparse(n.value, strip=True)
if isinstance(n, _ast.Assert) and isinstance(
n.test.comparators[0], _ast.Str):
setup_done = True
output = n.test.comparators[0].s
if not setup_done:
setup.append(n)
if setup:
setup = unparse(setup, strip=True)
return Example(
name,
title,
details,
setup or "",
old_style or "",
new_style or "",
output or ""
)
示例15: make_assign
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Assign [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()))