當前位置: 首頁>>代碼示例>>Python>>正文


Python _ast.FunctionDef方法代碼示例

本文整理匯總了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 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:22,代碼來源:rebuilder.py

示例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) 
開發者ID:zrzka,項目名稱:blackmamba,代碼行數:24,代碼來源:checker.py

示例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) 
開發者ID:ulope,項目名稱:pyformat.info,代碼行數:18,代碼來源:main.py

示例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 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:10,代碼來源:rebuilder.py

示例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) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:4,代碼來源:rebuilder.py

示例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') 
開發者ID:item4,項目名稱:yui,代碼行數:4,代碼來源:calc.py

示例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)() 
開發者ID:lrq3000,項目名稱:pyFileFixity,代碼行數:29,代碼來源:source_parser.py

示例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 
開發者ID:lrq3000,項目名稱:pyFileFixity,代碼行數:16,代碼來源:setup.py

示例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 
開發者ID:djc,項目名稱:runa,代碼行數:31,代碼來源:grammar.py

示例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) 
開發者ID:ulope,項目名稱:pyformat.info,代碼行數:17,代碼來源:main.py


注:本文中的_ast.FunctionDef方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。