Example #1
0
    def test_recursive_flatten(self):
        """recursive_flatten should remove all nesting from nested sequences"""
        self.assertEqual(recursive_flatten([1, [2, 3], [[4, [5]]]]), [1, 2, 3, 4, 5])

        # test default behavior on str unpacking
        self.assertEqual(
            recursive_flatten(["aa", [8, "cc", "dd"], ["ee", ["ff", "gg"]]]),
            ["a", "a", 8, "c", "c", "d", "d", "e", "e", "f", "f", "g", "g"],
        )
Example #2
0
 def test_recursive_flatten_max_depth(self):
     """recursive_flatten should not remover more than max_depth levels"""
     self.assertEqual(recursive_flatten([1, [2, 3], [[4, [5]]]]),
                      [1, 2, 3, 4, 5])
     self.assertEqual(recursive_flatten([1, [2, 3], [[4, [5]]]], 0),
                      [1, [2, 3], [[4, [5]]]])
     self.assertEqual(recursive_flatten([1, [2, 3], [[4, [5]]]], 1),
                      [1, 2, 3, [4, [5]]])
     self.assertEqual(recursive_flatten([1, [2, 3], [[4, [5]]]], 2),
                      [1, 2, 3, 4, [5]])
     self.assertEqual(recursive_flatten([1, [2, 3], [[4, [5]]]], 3),
                      [1, 2, 3, 4, 5])
     self.assertEqual(recursive_flatten([1, [2, 3], [[4, [5]]]], 5000),
                      [1, 2, 3, 4, 5])