本文整理汇总了Python中_ast.FunctionDef方法的典型用法代码示例。如果您正苦于以下问题:Python _ast.FunctionDef方法的具体用法?Python _ast.FunctionDef怎么用?Python _ast.FunctionDef使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类_ast
的用法示例。
在下文中一共展示了_ast.FunctionDef方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _visit_functiondef
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import FunctionDef [as 别名]
def _visit_functiondef(self, cls, node, parent):
"""visit an FunctionDef node to become astroid"""
self._global_names.append({})
node, doc = _get_doc(node)
newnode = cls(node.name, doc, node.lineno,
node.col_offset, parent)
if node.decorator_list:
decorators = self.visit_decorators(node, newnode)
else:
decorators = None
if PY3 and node.returns:
returns = self.visit(node.returns, newnode)
else:
returns = None
newnode.postinit(self.visit(node.args, newnode),
[self.visit(child, newnode)
for child in node.body],
decorators, returns)
self._global_names.pop()
return newnode
示例2: CONTINUE
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import FunctionDef [as 别名]
def CONTINUE(self, node):
# Walk the tree up until we see a loop (OK), a function or class
# definition (not OK), for 'continue', a finally block (not OK), or
# the top module scope (not OK)
n = node
while hasattr(n, 'parent'):
n, n_child = n.parent, n
if isinstance(n, LOOP_TYPES):
# Doesn't apply unless it's in the loop itself
if n_child not in n.orelse:
return
if isinstance(n, (ast.FunctionDef, ast.ClassDef)):
break
# Handle Try/TryFinally difference in Python < and >= 3.3
if hasattr(n, 'finalbody') and isinstance(node, ast.Continue):
if n_child in n.finalbody:
self.report(messages.ContinueInFinally, node)
return
if isinstance(node, ast.Continue):
self.report(messages.ContinueOutsideLoop, node)
else: # ast.Break
self.report(messages.BreakOutsideLoop, node)
示例3: parse_class
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import FunctionDef [as 别名]
def parse_class(node):
"""
parse_class parses the given node representing a test class for example
test cases and puts everything into a Section object.
"""
name = node.name[4:]
title, details = parse_docstring(ast.get_docstring(node))
examples = []
for n in node.body:
if isinstance(n, _ast.FunctionDef) and n.name.startswith('test_'):
example = parse_function(n)
examples.append(example._replace(
name='{}__{}'.format(name, example.name)))
return Section(name, title, details, examples)
示例4: visit_decorators
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import FunctionDef [as 别名]
def visit_decorators(self, node, parent):
"""visit a Decorators node by returning a fresh instance of it"""
# /!\ node is actually a _ast.FunctionDef node while
# parent is a astroid.nodes.FunctionDef node
newnode = nodes.Decorators(node.lineno, node.col_offset, parent)
newnode.postinit([self.visit(child, newnode)
for child in node.decorator_list])
return newnode
示例5: visit_functiondef
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import FunctionDef [as 别名]
def visit_functiondef(self, node, parent):
return self._visit_functiondef(nodes.FunctionDef, node, parent)
示例6: visit_functiondef
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import FunctionDef [as 别名]
def visit_functiondef(self, node: _ast.FunctionDef):
raise BadSyntax('Defining new function via def syntax is not allowed')
示例7: extract_parser
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import FunctionDef [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)()
示例8: parse_tree
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import FunctionDef [as 别名]
def parse_tree(tree, content):
for node in ast.iter_child_nodes(tree):
if not isinstance(node, _ast.FunctionDef):
continue
doc_string = ast.get_docstring(node)
if not doc_string:
continue
func_def = re.findall("def\s%s\s*(.+?)\s*:" % node.name, content)
assert func_def and len(func_def) == 1
func_def = node.name + func_def[0] + 2 * '\\n\\\n'
doc_string = doc_string.replace('\n', '\\n\\\n').replace('"', '\\"')
doc_string = doc_string.replace('\n' + 8 * ' ', '\n' + 4 * ' ')
doc_string = '#define %s_doc \\\n"%s%s"\n' % (node.name, func_def, doc_string)
yield doc_string
示例9: get_rules
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import FunctionDef [as 别名]
def get_rules():
with open(PARSER_FILE) as f:
src = f.read()
rules = collections.OrderedDict()
for node in ast.parse(src).body:
if not isinstance(node, _ast.FunctionDef):
continue
if not node.decorator_list:
continue
assert len(node.decorator_list) == 1
decorator = node.decorator_list[0]
if not isinstance(decorator, _ast.Call):
continue
func = decorator.func
if not isinstance(func, _ast.Attribute):
continue
assert func.attr == 'production'
ln = decorator.args[0].s
name, match = ln.split(' : ', 1)
rules.setdefault(name, []).append(tuple(match.split()))
return rules
示例10: get_content
# 需要导入模块: import _ast [as 别名]
# 或者: from _ast import FunctionDef [as 别名]
def get_content(filename=None):
"""
get_content generates sections or examples out of the given file path.
"""
log.info("Parsing content.")
if filename is None:
filename = CONTENT_MODULE_PATH
with open(str(filename), encoding='utf-8') as fp:
source = fp.read()
module = ast.parse(source)
for node in module.body:
if isinstance(node, _ast.FunctionDef) and node.name.startswith('test_'):
yield parse_function(node)
if isinstance(node, _ast.ClassDef) and node.name.startswith('Test'):
yield parse_class(node)