本文整理汇总了Python中_ast.Tuple方法的典型用法代码示例。如果您正苦于以下问题:Python _ast.Tuple方法的具体用法?Python _ast.Tuple怎么用?Python _ast.Tuple使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类_ast
的用法示例。
在下文中一共展示了_ast.Tuple方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_assign_chained
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [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: convert_to_value
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def convert_to_value(item):
if isinstance(item, ast.Str):
return item.s
elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes):
return item.s
elif isinstance(item, ast.Tuple):
return tuple(convert_to_value(i) for i in item.elts)
elif isinstance(item, ast.Num):
return item.n
elif isinstance(item, ast.Name):
result = VariableKey(item=item)
constants_lookup = {
'True': True,
'False': False,
'None': None,
}
return constants_lookup.get(
result.name,
result,
)
elif (not PY33) and isinstance(item, ast.NameConstant):
# None, True, False are nameconstants in python3, but names in 2
return item.value
else:
return UnhandledKeyType()
示例3: TRY
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def TRY(self, node):
handler_names = []
# List the exception handlers
for i, handler in enumerate(node.handlers):
if isinstance(handler.type, ast.Tuple):
for exc_type in handler.type.elts:
handler_names.append(getNodeName(exc_type))
elif handler.type:
handler_names.append(getNodeName(handler.type))
if handler.type is None and i < len(node.handlers) - 1:
self.report(messages.DefaultExceptNotLast, handler)
# Memorize the except handlers and process the body
self.exceptHandlers.append(handler_names)
for child in node.body:
self.handleNode(child, node)
self.exceptHandlers.pop()
# Process the other nodes: "except:", "else:", "finally:"
self.handleChildren(node, omit='body')
示例4: TRY
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def TRY(self, node):
handler_names = []
# List the exception handlers
for handler in node.handlers:
if isinstance(handler.type, ast.Tuple):
for exc_type in handler.type.elts:
handler_names.append(getNodeName(exc_type))
elif handler.type:
handler_names.append(getNodeName(handler.type))
# Memorize the except handlers and process the body
self.exceptHandlers.append(handler_names)
for child in node.body:
self.handleNode(child, node)
self.exceptHandlers.pop()
# Process the other nodes: "except:", "else:", "finally:"
self.handleChildren(node, omit='body')
示例5: _expand_tuples
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def _expand_tuples(self, args):
for arg in args:
if isinstance(arg, _ast.Tuple):
for n in arg.elts:
yield n
else:
yield arg
示例6: make_assign_unpack
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [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)
示例7: make_assign_opt_unpack
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [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())
示例8: make_tuple_list
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def make_tuple_list(i, bytecode):
op, arg = bytecode[i][2], bytecode[i][3]
pop, push = get_stack_effect(op, arg)
values = []
for k in range(pop):
i, expr = Statement.make_expr(i - 1, bytecode)
values.insert(0, expr)
cls = _ast.Tuple if op == BUILD_TUPLE else _ast.List
return i, cls(values, _ast.Load())
示例9: sequence_typeof
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def sequence_typeof(ast_node):
if isinstance(ast_node, _ast.Str):
return StringType()
elif isinstance(ast_node, _ast.Tuple):
return TupleType()
elif isinstance(ast_node, _ast.List):
return ListType()
return SequenceType()
示例10: is_sequence
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def is_sequence(ast_node):
return isinstance(ast_node, _ast.Str) \
or isinstance(ast_node, _ast.Tuple) \
or isinstance(ast_node, _ast.List)
示例11: assign
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def assign(self, node, val):
cls = node.__class__
if cls == _ast.Name:
self.symbol_table[node.id] = val
elif cls in (_ast.Tuple, _ast.List):
if not isinstance(val, abc.Iterable):
raise TypeError(
'cannot unpack non-iterable {} object'.format(
type(val).__name__,
)
)
for telem, tval in itertools.zip_longest(
node.elts, val, fillvalue=PLACEHOLDER,
):
if telem == PLACEHOLDER:
raise ValueError('not enough values to unpack')
if tval == PLACEHOLDER:
raise ValueError('too many values to unpack')
self.assign(telem, tval)
elif cls == _ast.Subscript:
sym = self._run(node.value)
xslice = self._run(node.slice)
if isinstance(node.slice, _ast.Index):
sym[xslice] = val
elif isinstance(node.slice, _ast.Slice):
sym[slice(xslice.start, xslice.stop)] = val
elif isinstance(node.slice, _ast.ExtSlice):
sym[xslice] = val
else:
raise BadSyntax('This assign method is not allowed')
示例12: delete
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def delete(self, node):
cls = node.__class__
if cls == _ast.Name:
del self.symbol_table[node.id]
elif cls == _ast.Tuple:
for elt in node.elts:
self.delete(elt)
示例13: visit_tuple
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def visit_tuple(self, node: _ast.Tuple): # elts, ctx
return tuple(self._run(x) for x in node.elts)
示例14: __init__
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [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)
示例15: getParent
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import Tuple [as 别名]
def getParent(self, node):
# Lookup the first parent which is not Tuple, List or Starred
while True:
node = node.parent
if not hasattr(node, 'elts') and not hasattr(node, 'ctx'):
return node