Python _ast.ClassDef() Examples

The following are 3 code examples of _ast.ClassDef(). 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: 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 #2
Source File: calc.py    From yui with GNU Affero General Public License v3.0 5 votes vote down vote up
def visit_classdef(self, node: _ast.ClassDef):
        raise BadSyntax('Defining new class via def syntax is not allowed') 
Example #3
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)