def test_non_tuple_type(self): t = "Hobbits" self.assertEqual(flatten_tuple(t), (t, )) t = 0 self.assertEqual(flatten_tuple(t), (t, )) t = {} self.assertEqual(flatten_tuple(t), (t, )) t = [] self.assertEqual(flatten_tuple(t), (t, ))
def get_total_by_month(data): """ Return a row of total (by column) to be used in the footer of a data table. :param data: A two-dimensional list that represents the data table :return: A list of integers, which are the result of adding the values in each column of each row. For example, if [["title1", a, b, c], ["title2", d, e, f]] were passed in, the return value will be [a+d, b+e, c+f]. """ # If initializer is not provided and there's only one row in data, zipped_reduced.pop(0) will, somehow, affect # context["data"] - making it lose index 0 as well. initializer = ["", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] zipped_reduced = reduce(lambda row_1, row_2: zip(row_1, row_2), data, initializer) zipped_reduced[0] = "Total" return [reduce(lambda a, b: int(a) + int(b), flatten_tuple(t)) for t in zipped_reduced]
def test_nasty_tuple(self): t = (1, (1, (1, (1, 1, 1, ())), (1, 1)), (1, (1, 1))) self.assertTupleEqual(flatten_tuple(t), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
def test_nested_tuple(self): t = (1, (1, ), 1) self.assertTupleEqual(flatten_tuple(t), (1, 1, 1))
def test_flat_tuple(self): self.assertTupleEqual(flatten_tuple((1, 2, 3)), (1, 2, 3))
def test_no_length(self): self.assertTupleEqual(flatten_tuple(()), ())