Python _ast.FunctionDef() Examples

The following are 10 code examples of _ast.FunctionDef(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module _ast , or try the search function .
Example #1
Source File: rebuilder.py    From linter-pylama with MIT License 6 votes vote down vote up
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 
Example #2
Source File: checker.py    From blackmamba with MIT License 6 votes vote down vote up
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) 
Example #3
Source File: main.py    From pyformat.info with MIT License 6 votes vote down vote up
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) 
Example #4
Source File: rebuilder.py    From linter-pylama with MIT License 5 votes vote down vote up
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 
Example #5
Source File: rebuilder.py    From linter-pylama with MIT License 5 votes vote down vote up
def visit_functiondef(self, node, parent):
        return self._visit_functiondef(nodes.FunctionDef, node, parent) 
Example #6
Source File: calc.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def visit_functiondef(self, node: _ast.FunctionDef):
        raise BadSyntax('Defining new function via def syntax is not allowed') 
Example #7
Source File: source_parser.py    From pyFileFixity with MIT License 5 votes vote down vote up
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)() 
Example #8
Source File: setup.py    From pyFileFixity with MIT License 5 votes vote down vote up
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 
Example #9
Source File: grammar.py    From runa with MIT License 5 votes vote down vote up
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 
Example #10
Source File: main.py    From pyformat.info with MIT License 5 votes vote down vote up
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)