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