How to make your Python classes document their own inheritance tree

First we connect our base Node class to a graph, which prevents searching further up the inheritance tree and trying to add 'Object' itself, which don't care to do:
           
    def __init__(self, name):
        self.ntype = self.__class__.__name__
        if not Node.node_added:

            Node.class_graph.add_node(
                Node.__name__)
        # now search the class graph
        # to see if the current class
        # is there:
        if self.ntype not in Node.class_graph:
            self.__class__.connect_to_class_tree()


And here is the method that recursively adds classes to the tree:

@classmethod 
def connect_to_class_tree(cls):
    for c in inspect.getmro(cls):
        if c != cls:
            if c.__name__ not in Node.class_graph:
                c.connect_to_class_tree()
                break
            else:
                Node.class_graph.add_edge(
                    get_class_name(c),               
                    get_class_name(cls))
# call until all upper classes are in 
                cls.connect_to_class_tree()

UPDATE: The call inspect.getmro() returns classes in "method resolution order."

And we get a nice tree like this:

Comments

  1. That little amount of code does all of that? If I'm correct in assuming that this uses comments to build documentation, is this code modifying the script file? And is the picture of your code or a specific instance of it?

    ReplyDelete
    Replies
    1. "That little amount of code does all of that?"

      It is using an existing graph package, networkx, that keeps track of edges and vertices.

      "If I'm correct in assuming that this uses comments to build documentation, is this code modifying the script file?"

      Comments are not involved here.

      "And is the picture of your code or a specific instance of it?"

      Not sure what you are asking: this is the class hierarchy of a particular program.

      Delete
    2. I assume you are using the graphics package, that can draw trees, to serve your own purpose of drawing the class hierarchy by adding nodes carefully as you walk the recursion tree ( stack). Nice.

      Delete
    3. We have two layers: networkx, which does graphs in the mathematical sense, and matplotlib, which displays them. My code just insert the nodes and edges into the graph at the right spots.

      And I'm not walking the stack, but the inheritance tree. I should make that clear above.

      Delete
    4. Right. I realized that later. You are looking at the static inheritance tree.
      One question though (it's been over 10 years since I knew Python). Does Python use the same kind of object model as JavaScript, where I can change my class hierarchy at runtime? [Too much power is my gut reaction to that.] just curious.

      Delete

Post a Comment

Popular posts from this blog

Libertarians, My Libertarians!

"Machine Learning"

"Pre-Galilean" Foolishness