def iter_superclasses(class_): """Iterate over all the superclasses (and superclasses thereof, etc.) of given class. :param class_: Class to yield the superclasses of :return: Iterable of superclasses, super-superclasses, etc. of ``class_`` .. note:: In most cases, the result of :func:`iter_superclasses` is the same as ``class_.__mro__``, except when the method resolution order has been customized by the metaclass of ``class_``. """ ensure_class(class_) classes = set() def ascend(class_): superclasses = set(class_.__bases__) - classes classes.update(superclasses) return superclasses result = breadth_first(class_, ascend) next(result) # omit ``class_`` itself return result
def test_start__flat_graph(self): nodes = list(map(self._create_node, range(1, 5 + 1))) graph = self._create_node(0, nodes) bfs = __unit__.breadth_first(graph, self.CHILDREN_FUNC) for i, node in enumerate(bfs, 0): self.assertEquals(i, node.value)
def iter_subclasses(class_): """Iterate over all the subclasses (and subclasses thereof, etc.) of given class. :param class_: Class to yield the subclasses of :return: Iterable of subclasses, sub-subclasses, etc. of ``class_`` """ ensure_class(class_) classes = set() def descend(class_): subclasses = set(class_.__subclasses__()) - classes classes.update(subclasses) return subclasses result = breadth_first(class_, descend) next(result) # omit ``class_`` itself return result
def test_start__none(self): bfs = __unit__.breadth_first(None, expand=functions.empty()) self.assertItemsEqual([None], bfs)
def test_expand__none(self): with self.assertRaises(TypeError): __unit__.breadth_first(self._create_node(), expand=None)
def test_expand__some_object(self): with self.assertRaises(TypeError): __unit__.breadth_first(self._create_node(), expand=object())
def test_start__path(self): graph = self._create_path(10) bfs = __unit__.breadth_first(graph, self.CHILDREN_FUNC) for i, node in enumerate(bfs, 0): self.assertEquals(i, node.value)
def test_start__some_object(self): obj = object() bfs = __unit__.breadth_first(obj, expand=functions.empty()) self.assertItemsEqual([obj], bfs)
def test_start__single_node(self): node = self._create_node() self.assertItemsEqual( [node], __unit__.breadth_first(node, self.CHILDREN_FUNC))
def test_start__single_node(self): node = self._create_node() self.assertItemsEqual([node], __unit__.breadth_first(node, self.CHILDREN_FUNC))