本文整理汇总了Python中ast.keyword方法的典型用法代码示例。如果您正苦于以下问题:Python ast.keyword方法的具体用法?Python ast.keyword怎么用?Python ast.keyword使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.keyword方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: stateful_wait_for
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def stateful_wait_for(self, call):
if isinstance(call.func, ast.Attribute):
if call.func.attr in ['wait_for_message', 'wait_for_reaction']:
event = call.func.attr.split('_')[2]
event = 'message' if event == 'message' else 'reaction_add'
call.func.attr = 'wait_for'
if call.args:
timeout = call.args[0]
call.args = []
call.keywords.append(ast.keyword(arg='timeout', value=timeout))
call.args.insert(0, ast.Str(s=event))
for kw in list(call.keywords):
if kw.arg != 'check' and kw.arg != 'timeout':
call.keywords.remove(kw)
warnings.warn('wait_for keyword breaking change detected. Rewrite removes the {} keyword'
' from wait_for.'.format(kw.arg))
elif kw.arg == 'timeout':
warnings.warn('wait_for timeout breaking change detected. Timeouts now raise '
'asyncio.TimeoutError instead of returning None.')
stats_counter['call_changes'] += 1
return call
示例2: unparse_Call
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def unparse_Call(self, item):
unparsed = {}
if isinstance(item.func, (ast.Name, ast.Attribute)):
func_name = self.unparse(item.func)
else:
try:
func_name = self.unparse(item.func)
except Exception:
func_name = None
if not func_name:
return {}
if isinstance(func_name, dict):
unparsed.update(func_name)
func_name = next(iter(func_name.keys()))
else:
unparsed[func_name] = {}
for key in ("kwargs", "keywords"):
val = getattr(item, key, [])
if val is None:
continue
for keyword in self.unparse(val):
unparsed[func_name].update(self.unparse(keyword))
return unparsed
示例3: visit_Call
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def visit_Call(self, node):
# This will not visit Flask in Flask(__name__) but it will visit request in `request.args.get()
if not isinstance(node.func, ast.Name):
self.visit(node.func)
for arg_node in itertools.chain(node.args, node.keywords):
arg = arg_node.value if isinstance(arg_node, ast.keyword) else arg_node
if isinstance(arg, ast.Call):
if isinstance(arg.func, ast.Name):
# We can't just visit because we need to add 'ret_'
self.result.append('ret_' + arg.func.id)
elif isinstance(arg.func, ast.Attribute):
# e.g. html.replace('{{ param }}', param)
# func.attr is replace
# func.value.id is html
# We want replace
self.result.append('ret_' + arg.func.attr)
elif isinstance(arg.func, ast.Call):
self.visit_curried_call_inside_call_args(arg)
else:
raise Exception('Cannot visit vars of ' + ast.dump(arg))
else:
self.visit(arg)
示例4: visit_curried_call_inside_call_args
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def visit_curried_call_inside_call_args(self, inner_call):
# Curried functions aren't supported really, but we now at least have a defined behaviour.
# In f(g(a)(b)(c)), inner_call is the Call node with argument c
# Try to get the name of curried function g
curried_func = inner_call.func.func
while isinstance(curried_func, ast.Call):
curried_func = curried_func.func
if isinstance(curried_func, ast.Name):
self.result.append('ret_' + curried_func.id)
elif isinstance(curried_func, ast.Attribute):
self.result.append('ret_' + curried_func.attr)
# Visit all arguments except a (ignore the curried function g)
not_curried = inner_call
while not_curried.func is not curried_func:
for arg in itertools.chain(not_curried.args, not_curried.keywords):
self.visit(arg.value if isinstance(arg, ast.keyword) else arg)
not_curried = not_curried.func
示例5: test_classdef
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def test_classdef(self):
def cls(bases=None, keywords=None, body=None, decorator_list=None):
if bases is None:
bases = []
if keywords is None:
keywords = []
if body is None:
body = [ast.Pass()]
if decorator_list is None:
decorator_list = []
return ast.ClassDef("myclass", bases, keywords,
body, decorator_list)
self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
"must have Load context")
self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
"must have Load context")
self.stmt(cls(body=[]), "empty body on ClassDef")
self.stmt(cls(body=[None]), "None disallowed")
self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
"must have Load context")
示例6: test_classdef
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def test_classdef(self):
def cls(bases=None, keywords=None, starargs=None, kwargs=None,
body=None, decorator_list=None):
if bases is None:
bases = []
if keywords is None:
keywords = []
if body is None:
body = [ast.Pass()]
if decorator_list is None:
decorator_list = []
return ast.ClassDef("myclass", bases, keywords, starargs,
kwargs, body, decorator_list)
self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
"must have Load context")
self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
"must have Load context")
self.stmt(cls(starargs=ast.Name("x", ast.Store())),
"must have Load context")
self.stmt(cls(kwargs=ast.Name("x", ast.Store())),
"must have Load context")
self.stmt(cls(body=[]), "empty body on ClassDef")
self.stmt(cls(body=[None]), "None disallowed")
self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
"must have Load context")
示例7: test_call
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def test_call(self):
func = ast.Name("x", ast.Load())
args = [ast.Name("y", ast.Load())]
keywords = [ast.keyword("w", ast.Name("z", ast.Load()))]
stararg = ast.Name("p", ast.Load())
kwarg = ast.Name("q", ast.Load())
call = ast.Call(ast.Name("x", ast.Store()), args, keywords, stararg,
kwarg)
self.expr(call, "must have Load context")
call = ast.Call(func, [None], keywords, stararg, kwarg)
self.expr(call, "None disallowed")
bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))]
call = ast.Call(func, args, bad_keywords, stararg, kwarg)
self.expr(call, "must have Load context")
call = ast.Call(func, args, keywords, ast.Name("z", ast.Store()), kwarg)
self.expr(call, "must have Load context")
call = ast.Call(func, args, keywords, stararg,
ast.Name("w", ast.Store()))
self.expr(call, "must have Load context")
示例8: parse_annotation_function
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def parse_annotation_function(code, func_name):
"""Parse an annotation function.
Return the value of `name` keyword argument and the AST Call node.
func_name: expected function name
"""
expr = parse_annotation(code)
call = expr.value
assert type(call) is ast.Call, 'Annotation is not a function call'
assert type(call.func) is ast.Attribute, 'Unexpected annotation function'
assert type(call.func.value) is ast.Name, 'Invalid annotation function name'
assert call.func.value.id == 'nni', 'Annotation is not a NNI function'
assert call.func.attr == func_name, 'internal error #2'
assert len(call.keywords) == 1, 'Annotation function contains more than one keyword argument'
assert call.keywords[0].arg == 'name', 'Annotation keyword argument is not "name"'
name = call.keywords[0].value
return name, call
示例9: parse_nni_variable
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def parse_nni_variable(code):
"""Parse `nni.variable` expression.
Return the name argument and AST node of annotated expression.
code: annotation string
"""
name, call = parse_annotation_function(code, 'variable')
assert len(call.args) == 1, 'nni.variable contains more than one arguments'
arg = call.args[0]
assert type(arg) is ast.Call, 'Value of nni.variable is not a function call'
assert type(arg.func) is ast.Attribute, 'nni.variable value is not a NNI function'
assert type(arg.func.value) is ast.Name, 'nni.variable value is not a NNI function'
assert arg.func.value.id == 'nni', 'nni.variable value is not a NNI function'
name_str = astor.to_source(name).strip()
keyword_arg = ast.keyword(arg='name', value=ast.Str(s=name_str))
arg.keywords.append(keyword_arg)
if arg.func.attr == 'choice':
convert_args_to_dict(arg)
return name, arg
示例10: make_call_keywords
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def make_call_keywords(stack_builders, count):
"""
Make the keywords entry for an ast.Call node.
"""
out = []
for _ in range(count):
value = make_expr(stack_builders)
load_kwname = stack_builders.pop()
if not isinstance(load_kwname, instrs.LOAD_CONST):
raise DecompilationError(
"Expected a LOAD_CONST, but got %r" % load_kwname
)
if not isinstance(load_kwname.arg, str):
raise DecompilationError(
"Expected LOAD_CONST of a str, but got %r." % load_kwname,
)
out.append(ast.keyword(arg=load_kwname.arg, value=value))
out.reverse()
return out
示例11: translate_self_evaluating
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def translate_self_evaluating(self, exp):
if isinstance(exp, (int, float, complex)):
if hasattr(exp, 'value'):
return EMPTY, ast.Num(exp.value,
lineno=exp.lineno,
col_offset=0)
else:
return EMPTY, ast.Num(exp,
lineno=0,
col_offset=0)
expType = type(exp)
if expType is str:
return EMPTY, ast.Str(exp,
lineno=0,
col_offset=0)
if expType is Keyword:
#raise SyntaxError('keyword!', self.filename)
modast = ast.parse(
'Keyword("{name}", {lineno})'.format(name=exp.name, lineno=exp.lineno))
return EMPTY, modast.body[0].value
示例12: visit_Call
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def visit_Call(self, call: ast.Call) -> VisitExprReturnT:
result_actions = []
func, func_actions = self.visit_expr(call.func)
result_actions.extend(func_actions)
args = []
for arg in call.args:
arg_flattened, arg_actions = self.visit_expr(arg)
result_actions.extend(arg_actions)
args.append(arg_flattened)
keywords = []
for kw in call.keywords:
kw_value_flattened, kw_value_actions = self.visit_expr(kw.value)
result_actions.extend(kw_value_actions)
keywords.append(ast.keyword(arg=kw.arg, value=kw_value_flattened))
result_id = self.next_symbol_id()
result_actions.append(assign(result_id, ast.Call(func, args, keywords)))
return load(result_id), result_actions
示例13: visit_ClassDef
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def visit_ClassDef(self, class_def: ast.ClassDef) -> ActionsT:
bases, bases_actions = self.visit_expr_list(class_def.bases)
keywords = []
keywords_actions = []
for kw in class_def.keywords:
kw_value_flattened, kw_value_actions = self.visit_expr(kw.value)
keywords_actions.extend(kw_value_actions)
keywords.append(ast.keyword(arg=kw.arg, value=kw_value_flattened))
body = self.visit_stmt_list(class_def.body)
if class_def.decorator_list:
raise NodeNotSupportedError(class_def, "ClassDef decorators not supported")
result_node = ast.ClassDef(name=class_def.name, bases=bases, keywords=keywords, body=body,
decorator_list=class_def.decorator_list)
return bases_actions + keywords_actions + [result_node]
示例14: visit_Call_arguments
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def visit_Call_arguments(self, node):
def arg_location(tup):
arg = tup[1]
if isinstance(arg, ast.keyword):
arg = arg.value
return (getattr(arg, "lineno", 0), getattr(arg, "col_offset", 0))
if node.starargs:
sorted_keywords = sorted(
[(None, kw) for kw in node.keywords] + [('*', node.starargs)],
key=arg_location)
else:
sorted_keywords = [(None, kw) for kw in node.keywords]
all_args = [(None, n) for n in node.args] + sorted_keywords
if node.kwargs:
all_args.append(('**', node.kwargs))
for i, (prefix, arg) in enumerate(all_args):
if prefix is not None:
self.attr(node, '%s_prefix' % prefix, [self.ws, prefix], default=prefix)
self.visit(arg)
if arg is not all_args[-1][1]:
self.attr(node, 'comma_%d' % i, [self.ws, ',', self.ws], default=', ')
return bool(all_args)
示例15: _has_same_kwarg
# 需要导入模块: import ast [as 别名]
# 或者: from ast import keyword [as 别名]
def _has_same_kwarg(
node: types.AnyFunctionDefAndLambda,
call: ast.Call,
) -> bool:
"""Tells whether ``call`` has the same kwargs as ``node``."""
kwarg_name: Optional[str] = None
null_arg_keywords = filter(lambda key: key.arg is None, call.keywords)
for keyword in null_arg_keywords:
# `a=1` vs `**kwargs`:
# {'arg': 'a', 'value': <_ast.Num object at 0x1027882b0>}
# {'arg': None, 'value': <_ast.Name object at 0x102788320>}
if isinstance(keyword.value, ast.Name):
kwarg_name = keyword.value.id
else: # We can judge on things like `**{}`
return False
if node.args.kwarg and kwarg_name:
return node.args.kwarg.arg == kwarg_name
return node.args.kwarg == kwarg_name # type: ignore