本文整理汇总了Python中ast.Starred方法的典型用法代码示例。如果您正苦于以下问题:Python ast.Starred方法的具体用法?Python ast.Starred怎么用?Python ast.Starred使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.Starred方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TUPLE
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def TUPLE(self, node):
if not PY2 and isinstance(node.ctx, ast.Store):
# Python 3 advanced tuple unpacking: a, *b, c = d.
# Only one starred expression is allowed, and no more than 1<<8
# assignments are allowed before a stared expression. There is
# also a limit of 1<<24 expressions after the starred expression,
# which is impossible to test due to memory restrictions, but we
# add it here anyway
has_starred = False
star_loc = -1
for i, n in enumerate(node.elts):
if isinstance(n, ast.Starred):
if has_starred:
self.report(messages.TwoStarredExpressions, node)
# The SyntaxError doesn't distinguish two from more
# than two.
break
has_starred = True
star_loc = i
if star_loc >= 1 << 8 or len(node.elts) - star_loc - 1 >= 1 << 24:
self.report(messages.TooManyExpressionsInStarredAssignment, node)
self.handleChildren(node)
示例2: visit_Call
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def visit_Call(self, call_node):
func_name = get_call_names_as_string(call_node.func)
trigger_re = r"(^|\.){}$".format(re.escape(self._trigger_str))
if re.search(trigger_re, func_name):
seen_starred = False
for index, arg in enumerate(call_node.args):
if isinstance(arg, ast.Starred):
seen_starred = True
if seen_starred:
self.unknown_arg_visitor.visit(arg)
else:
self.argument_visitors[index].visit(arg)
for keyword in call_node.keywords:
if keyword.arg is None:
self.unknown_kwarg_visitor.visit(keyword.value)
else:
self.argument_visitors[keyword.arg].visit(keyword.value)
self.generic_visit(call_node)
示例3: visit_Call
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def visit_Call(self, node: ast.Call):
"""
Convert `ast.Call` to `ast.JoinedStr` f-string
"""
match = matching_call(node)
if match:
state.call_candidates += 1
# bail in these edge cases...
if any(isinstance(arg, ast.Starred) for arg in node.args):
return node
result_node = joined_string(node)
self.visit(result_node)
self.counter += 1
state.call_transforms += 1
return result_node
return node
示例4: check_for_loop
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def check_for_loop(self, statement):
stack = [statement.target]
while stack:
item = stack.pop()
if isinstance(item, (ast.Tuple, ast.List)):
stack.extend(list(item.elts))
elif isinstance(item, ast.Name) and \
item.id in BUILTINS:
yield self.error(statement, variable=item.id)
elif PY3 and isinstance(item, ast.Starred):
if hasattr(item.value, 'id') and item.value.id in BUILTINS:
yield self.error(
statement,
variable=item.value.id,
)
elif hasattr(item.value, 'elts'):
stack.extend(list(item.value.elts))
示例5: visit_Call
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def visit_Call(self, node: ast.Call) -> Any:
"""Visit the function and the arguments and finally make the function call with them."""
func = self.visit(node=node.func)
args = [] # type: List[Any]
for arg_node in node.args:
if isinstance(arg_node, ast.Starred):
args.extend(self.visit(node=arg_node))
else:
args.append(self.visit(node=arg_node))
kwargs = dict() # type: Dict[str, Any]
for keyword in node.keywords:
if keyword.arg is None:
kw = self.visit(node=keyword.value)
for key, val in kw.items():
kwargs[key] = val
else:
kwargs[keyword.arg] = self.visit(node=keyword.value)
result = func(*args, **kwargs)
self.recomputed_values[node] = result
return result
示例6: is_primitive
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def is_primitive(node: ast.AST) -> bool:
"""
Detects if node is a form of a primitive value.
We use this predicate to allow values
like ``[]`` or ``call()`` to be overused.
Because you cannot simply them.
We do not check for strings, numbers, etc
because they are globally ignored.
"""
if isinstance(node, (ast.Tuple, ast.List)):
return not node.elts # we do allow `[]` and `()`
elif isinstance(node, ast.Set):
return ( # we do allow `{*set_items}`
len(node.elts) == 1 and
isinstance(node.elts[0], ast.Starred)
)
elif isinstance(node, ast.Dict): # we do allow `{}` and `{**values}`
return not list(filter(None, node.keys))
elif isinstance(node, ast.Call):
return not call_args.get_all_args(node) # we do allow `call()`
return False
示例7: extract_name
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def extract_name(node: ast.AST) -> Optional[str]:
"""
Utility to extract names for several types of nodes.
Is used to get name from node in case it is ``ast.Name``.
Should not be used direclty with assigns,
use safer :py:`~get_assign_names` function.
Example:
>>> import ast
>>> tree = ast.parse('a')
>>> node = tree.body[0].value
>>> extract_name(node)
'a'
"""
if isinstance(node, ast.Starred):
node = node.value
if isinstance(node, ast.Name):
return node.id
return None
示例8: _build_call35
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def _build_call35(self, o):
"""
Workaround for python 3.5 _ast.Call signature, docs found here
https://greentreesnakes.readthedocs.org/en/latest/nodes.html
"""
import ast
callee = self.build(o.func)
args = []
if o.args is not None:
for a in o.args:
if isinstance(a, ast.Starred):
args.append(self.build(a.value))
else:
args.append(self.build(a))
kwargs = {}
for kw in o.keywords:
if kw.arg is None: # double asterix `**`
rst = self.build(kw.value)
if not isinstance(rst, dict):
raise TypeError('Invalid argument for call.'
'Must be a mapping object.')
# give preference to the keys set directly from arg=value
for k, v in rst.items():
if k not in kwargs:
kwargs[k] = v
else: # defined on the call as: arg=value
kwargs[kw.arg] = self.build(kw.value)
return callee(*args, **kwargs)
示例9: visit_Call
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def visit_Call(self, node):
# ast.Call signature changed in python 3.5
# http://greentreesnakes.readthedocs.org/en/latest/nodes.html#Call
if sys.version_info >= (3, 5):
starargs = any(isinstance(arg, ast.Starred) for arg in node.args)
kwargs = any(kw.arg is None for kw in node.keywords)
else:
starargs = node.starargs is not None
kwargs = node.kwargs is not None
if starargs:
self.raiseError(node, _error.NotSupported, "extra positional arguments")
if kwargs:
self.raiseError(node, _error.NotSupported, "extra named arguments")
self.generic_visit(node)
示例10: getParent
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [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
示例11: check_call_args
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def check_call_args(loc, seq: t.List[ast.expr]):
in_keyword_section = False
for each in seq:
if isinstance(each, ast.keyword):
in_keyword_section = True
elif in_keyword_section and not isinstance(each, ast.Starred):
error = SyntaxError()
error.lineno = loc['lineno']
error.msg = 'non-keyword argument follows keyword argument'
raise error
return seq
示例12: visit_Starred
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def visit_Starred(self, starred):
# From Python 3.5, a Starred node can appear in a function call
res, expl = self.visit(starred.value)
new_starred = ast.Starred(res, starred.ctx)
return new_starred, "*" + expl
示例13: _starargs
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def _starargs(call: ast.Call) -> bool:
return (
any(k.arg is None for k in call.keywords) or
any(isinstance(a, ast.Starred) for a in call.args)
)
示例14: visit_Starred
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def visit_Starred(self, starred: ast.Starred) -> Tuple[ast.Starred, str]:
# From Python 3.5, a Starred node can appear in a function call
res, expl = self.visit(starred.value)
new_starred = ast.Starred(res, starred.ctx)
return new_starred, "*" + expl
示例15: _get_names
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Starred [as 别名]
def _get_names(node, result):
"""Recursively finds all names."""
if isinstance(node, ast.Name):
return node.id + result
elif isinstance(node, ast.Subscript):
return result
elif isinstance(node, ast.Starred):
return _get_names(node.value, result)
else:
return _get_names(node.value, result + '.' + node.attr)