Analysieren Sie Funktionen in einem Python-Skript

Dieser analysiert ein Python-Skript und meldet für jede definierte Funktion die Zeilennummer, an der die Funktion begann, wo die Signatur endet, wo der Docstring endet und wo die Funktionsdefinition endet.


import ast
import sys

""" The data we collect.  Each key is a function name; each value is a dict
with keys: firstline, sigend, docend, and lastline and values of line numbers
where that happens. """
functions = {}

def process(functions):
    """ Handle the function data stored in functions. """
    for funcname,data in functions.items():
        print("\tstarts at line:",data['firstline'])
        print("\tsignature ends at line:",data['sigend'])
        if ( data['sigend'] < data['docend'] ):
            print("\tdocstring ends at line:",data['docend'])
            print("\tno docstring")
        print("\tfunction ends at line:",data['lastline'])

class FuncLister(ast.NodeVisitor):
    def visit_FunctionDef(self, node):
        """ Recursively visit all functions, determining where each function
        starts, where its signature ends, where the docstring ends, and where
        the function ends. """
        functions[] = {'firstline':node.lineno}
        sigend = max(node.lineno,lastline(node.args))
        functions[]['sigend'] = sigend
        docstring = ast.get_docstring(node)
        docstringlength = len(docstring.split('\n')) if docstring else -1
        functions[]['docend'] = sigend+docstringlength
        functions[]['lastline'] = lastline(node)

def lastline(node):
    """ Recursively find the last line of a node """
    return max( [ node.lineno if hasattr(node,'lineno') else -1 , ]
                +[lastline(child) for child in ast.iter_child_nodes(node)] )

def readin(pythonfilename):
    """ Read the file name and store the function data into functions. """
    with open(pythonfilename) as f:
        code =

def analyze(file,process):
    """ Read the file and process the function data. """

if __name__ == '__main__':
    if len(sys.argv)>1:
        for file in sys.argv[1:]:

Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow