Recursive method to instantiate services

def _do(self, nodes):
        """ Recursive method to instantiate services """
        if not isinstance(nodes, dict):
            raise TypeError('"nodes" must be a dictionary')

        if not nodes:
            # we're done!
            return
        starting_num_nodes = len(nodes)
        newly_instantiated = set()

        # Instantiate services with an empty dependency set
        for (name, dependency_set) in six.iteritems(nodes):
            if dependency_set:
                # Skip non-empty dependency sets
                continue

            # Instantiate
            config = self._config[name]
            service = self._factory.create_from_dict(config)
            self._factory.add_instantiated_service(name, service)

            newly_instantiated.add(name)

        # We ALWAYS should have instantiated a new service
        # or we'll end up in an infinite loop.
        if not newly_instantiated:
            raise Exception('No newly instantiated services')

        # Remove from Nodes
        for name in newly_instantiated:
            del nodes[name]

        # Check if the number of nodes have changed
        # to prevent infinite loops.
        # We should ALWAYS have removed a node.
        if starting_num_nodes == len(nodes):
            raise Exception('No nodes removed!')

        # Remove newly instantiated services from dependency sets
        for (name, dependency_set) in six.iteritems(nodes):
            nodes[name] = dependency_set.difference(newly_instantiated)

        # Recursion is recursion is ...
        self._do(nodes)