title: Parsing class and function dependencies from a project
body: <p>I'm trying to run some analysis of class and function dependencies in a Python code base. My first step was to create a .csv file for import into Excel using Python's <code>csv</code> module and regular expressions.</p>
  
  <p>The current version of what I have looks like this:</p>
  
  <pre><code>import re
  import os
  import csv 
  from os.path import join
  
  
  class ClassParser(object):
     class_expr = re.compile(r'class (.+?)(?:\((.+?)\))?:')                                                                                                                                                                                    
     python_file_expr = re.compile(r'^\w+[.]py$')
  
     def findAllClasses(self, python_file):
        """ Read in a python file and return all the class names
        """
        with open(python_file) as infile:
           everything = infile.read()
           class_names = ClassParser.class_expr.findall(everything)
           return class_names
  
     def findAllPythonFiles(self, directory):
        """ Find all the python files starting from a top level directory
        """
        python_files = []
        for root, dirs, files in os.walk(directory):
           for file in files:
              if ClassParser.python_file_expr.match(file):
                 python_files.append(join(root,file))
        return python_files
  
     def parse(self, directory, output_directory="classes.csv"):
        """ Parse the directory and spit out a csv file
        """
        with open(output_directory,'w') as csv_file:
           writer = csv.writer(csv_file)
           python_files = self.findAllPythonFiles(directory)
           for file in python_files:
              classes = self.findAllClasses(file)
              for classname in classes:
                 writer.writerow([classname[0], classname[1], file])
  
  if __name__=="__main__":
     parser = ClassParser()
     parser.parse("/path/to/my/project/main/directory")
  </code></pre>
  
  <p>This generates a .csv output in format:</p>
  
  <pre><code>class name, inherited classes (also comma separated), file
  class name, inherited classes (also comma separated), file
  ... etc. ...
  </code></pre>
  
  <p>I'm at the point where I'd like to start parsing function declaration and definitions in addition to the class names. My question: <em>Is there a better way to get the class names, inherited class names, function names, parameter names, etc.?</em></p>
  
  <p>NOTE: I've considered using the Python <code>ast</code> module, but I don't have experience with it and don't know how to use it to get the desired information or if it can even do that.</p>
  
  <p>EDIT: <strong>In response to Martin Thurau's request for more information</strong> - The reason I'm trying to solve this issue is because I've inherited a rather lengthy (100k+ lines) project that has no discernible structure to its modules, classes and functions; they all exist as a collection of files in a single source directory.</p>
  
  <p>Some of the source files contain dozens of tangentially related classes and are 10k+ lines long which makes them difficult to maintain. I'm starting to perform analysis for the relative difficulty of taking every class and packaging it into a more cohesive structure using <a href="http://docs.python-guide.org/en/latest/shipping/packaging/" rel="nofollow noreferrer">The Hitchhiker's Guide to Packaging</a> as a base. Part of what I care about for that analysis is how intertwined a class is with other classes in its file and what imported or inherited classes a particular class relies on.</p>