From 149bafa97b3c93b5116090d2834f0c19e6face95 Mon Sep 17 00:00:00 2001 From: Josh Ourisman Date: Fri, 1 Oct 2010 16:17:04 -0400 Subject: [PATCH] added ability to append new column passing a callable as the value that will be applied to every row; w/ test --- tablib/core.py | 9 +++++++++ test_tablib.py | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/tablib/core.py b/tablib/core.py index 8f74975..dfb8027 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -177,6 +177,15 @@ class Dataset(object): self._validate(row) self._data.append(tuple(row)) elif col is not None: + col = list(col) + if self.headers: + header = [col.pop(0)] + else: + header = [] + if len(col) == 1 and callable(col[0]): + col = map(col[0], self._data) + col = tuple(header + col) + self._validate(col=col) if self.headers: diff --git a/test_tablib.py b/test_tablib.py index 67b693d..a315642 100755 --- a/test_tablib.py +++ b/test_tablib.py @@ -102,6 +102,13 @@ class TablibTestCase(unittest.TestCase): self.assertRaises(tablib.InvalidDimensions, data.append, col=new_col) + def test_add_callable_column(self): + """Verify adding column with values specified as callable.""" + new_col = ['first_again', lambda x: x[0]] + self.founders.append(col=new_col) + + self.assertTrue(map(lambda x: x[0] == x[-1], self.founders)) + def test_header_slicing(self): """Verify slicing by headers."""