Beispiel #1
0
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
Beispiel #2
0
    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)
Beispiel #3
0
    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)
Beispiel #4
0
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
Beispiel #5
0
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
Beispiel #6
0
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
Beispiel #7
0
 def test_start__none(self):
     bfs = __unit__.breadth_first(None, expand=functions.empty())
     self.assertItemsEqual([None], bfs)
Beispiel #8
0
 def test_expand__none(self):
     with self.assertRaises(TypeError):
         __unit__.breadth_first(self._create_node(), expand=None)
Beispiel #9
0
 def test_expand__some_object(self):
     with self.assertRaises(TypeError):
         __unit__.breadth_first(self._create_node(), expand=object())
Beispiel #10
0
 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)
Beispiel #11
0
 def test_start__none(self):
     bfs = __unit__.breadth_first(None, expand=functions.empty())
     self.assertItemsEqual([None], bfs)
Beispiel #12
0
 def test_start__some_object(self):
     obj = object()
     bfs = __unit__.breadth_first(obj, expand=functions.empty())
     self.assertItemsEqual([obj], bfs)
Beispiel #13
0
 def test_start__single_node(self):
     node = self._create_node()
     self.assertItemsEqual(
         [node], __unit__.breadth_first(node, self.CHILDREN_FUNC))
Beispiel #14
0
 def test_start__some_object(self):
     obj = object()
     bfs = __unit__.breadth_first(obj, expand=functions.empty())
     self.assertItemsEqual([obj], bfs)
Beispiel #15
0
 def test_expand__some_object(self):
     with self.assertRaises(TypeError):
         __unit__.breadth_first(self._create_node(), expand=object())
Beispiel #16
0
 def test_expand__none(self):
     with self.assertRaises(TypeError):
         __unit__.breadth_first(self._create_node(), expand=None)
Beispiel #17
0
 def test_start__single_node(self):
     node = self._create_node()
     self.assertItemsEqual([node],
                           __unit__.breadth_first(node, self.CHILDREN_FUNC))
Beispiel #18
0
 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)