예제 #1
0
파일: classes.py 프로젝트: Xion/taipan
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
예제 #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)
예제 #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)
예제 #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
예제 #5
0
파일: classes.py 프로젝트: Xion/taipan
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
예제 #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
예제 #7
0
 def test_start__none(self):
     bfs = __unit__.breadth_first(None, expand=functions.empty())
     self.assertItemsEqual([None], bfs)
예제 #8
0
 def test_expand__none(self):
     with self.assertRaises(TypeError):
         __unit__.breadth_first(self._create_node(), expand=None)
예제 #9
0
 def test_expand__some_object(self):
     with self.assertRaises(TypeError):
         __unit__.breadth_first(self._create_node(), expand=object())
예제 #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)
예제 #11
0
 def test_start__none(self):
     bfs = __unit__.breadth_first(None, expand=functions.empty())
     self.assertItemsEqual([None], bfs)
예제 #12
0
 def test_start__some_object(self):
     obj = object()
     bfs = __unit__.breadth_first(obj, expand=functions.empty())
     self.assertItemsEqual([obj], bfs)
예제 #13
0
 def test_start__single_node(self):
     node = self._create_node()
     self.assertItemsEqual(
         [node], __unit__.breadth_first(node, self.CHILDREN_FUNC))
예제 #14
0
 def test_start__some_object(self):
     obj = object()
     bfs = __unit__.breadth_first(obj, expand=functions.empty())
     self.assertItemsEqual([obj], bfs)
예제 #15
0
 def test_expand__some_object(self):
     with self.assertRaises(TypeError):
         __unit__.breadth_first(self._create_node(), expand=object())
예제 #16
0
 def test_expand__none(self):
     with self.assertRaises(TypeError):
         __unit__.breadth_first(self._create_node(), expand=None)
예제 #17
0
 def test_start__single_node(self):
     node = self._create_node()
     self.assertItemsEqual([node],
                           __unit__.breadth_first(node, self.CHILDREN_FUNC))
예제 #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)