def test_list_tuple_set_dict(self): for s in [(1, 2, 3), [4, 5, 6], {7, 8, 8}]: flat = nest.flatten(s) self.assertEqual(flat, list(s)) s = {'a': 1, 'b': 2} flat = nest.flatten(s) self.assertEqual(flat, [1, 2])
def test_atomic(self): s = [Point(1, 2), (Point(3, 4), Point(5, 6)), {'a': Point(7, 8)}] flat = nest.flatten(s, is_atomic=lambda x: isinstance(x, Point)) self.assertEqual(flat, [Point(1, 2), Point(3, 4), Point(5, 6), Point(7, 8)])
def test_nested(self): s = (1, [2, {3, 4, 5}, {'a': 6, 'b': 7}, Coordinates(Point(8, 9), Point(10, 11))]) flat = nest.flatten(s) self.assertEqual(flat, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
def test_attr(self): c = Coordinates(1, 2) flat = nest.flatten(c) self.assertEqual(flat, [1, 2])
def test_namedtuple(self): p = Point(1, 2) flat = nest.flatten(p) self.assertEqual(flat, [1, 2])
def test_single_element(self): for s in ["example", 2, False]: flat = nest.flatten(s) self.assertEqual(flat, [s])
def test_none(self): s = None flat = nest.flatten(s) self.assertEqual(flat, [])
def test_has_max_depth_with_nested(self): s = {'a': [1, (3, {4, 5})], 'b': {'c': 1}, 'd': [Point(2, 3), Coordinates(Point(1, 2), 7)]} f = nest.flatten(s, is_atomic=nest.has_max_depth(1)) self.assertEqual(f, [1, 3, {4, 5}, {'c': 1}, Point(2, 3), Point(1, 2), 7])