From 1427be2901cb53c52d60a1438492b01cee39b867 Mon Sep 17 00:00:00 2001 From: Luca Beltrame Date: Mon, 15 Nov 2010 08:59:49 +0100 Subject: [PATCH 1/2] Support for row and column stacking. Unit-tested. --- tablib/core.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ test_tablib.py | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/tablib/core.py b/tablib/core.py index 23026a2..47d2e13 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -535,6 +535,59 @@ class Dataset(object): _dset.append(row=row_data) return _dset + + def row_stack(self, other): + + """Stack two :class:`Dataset` instances together by + joining them at the row level, and return a new + combined ``Dataset`` instance.""" + + if not isinstance(other, Dataset): + return + + if self.width != other.width: + raise InvalidDimensions + + # Copy the source data + _dset = copy(self) + + rows_to_stack = [row for row in _dset._data] + other_rows = [row for row in other._data] + + rows_to_stack.extend(other_rows) + _dset._data = rows_to_stack + + return _dset + + def column_stack(self, other): + + """Stack two :class:`Dataset` instances together by + joining at the column level, and return a new + combined ``Dataset`` instance. Requires headers + to be set.""" + + if not isinstance(other, Dataset): + return + + if not self.headers or not other.headers: + raise HeadersNeeded + + if self.height != other.height: + raise InvalidDimensions + + new_headers = self.headers + other.headers + + _dset = Dataset() + + for column in self.headers: + _dset.append(col=self[column]) + + for column in other.headers: + _dset.append(col=other[column]) + + _dset.headers = new_headers + + return _dset def wipe(self): """Removes all content and headers from the :class:`Dataset` object.""" diff --git a/test_tablib.py b/test_tablib.py index f203179..126ee84 100755 --- a/test_tablib.py +++ b/test_tablib.py @@ -3,6 +3,7 @@ """Tests for Tablib.""" +from copy import copy import unittest import tablib @@ -364,7 +365,44 @@ class TablibTestCase(unittest.TestCase): ("last_name","Adams", "Washington", "Jefferson")) self.assertEqual(second_row, ("gpa",90, 67, 50)) + + def test_row_stacking(self): + """Row stacking.""" + + to_join = tablib.Dataset(headers=self.founders.headers) + + for row in self.founders: + to_join.append(row=row) + + row_stacked = self.founders.row_stack(to_join) + + for column in row_stacked.headers: + + original_data = self.founders[column] + expected_data = original_data + original_data + self.assertEqual(row_stacked[column], expected_data) + + def test_column_stacking(self): + + """Column stacking""" + + to_join = tablib.Dataset(headers=self.founders.headers) + + for row in self.founders: + to_join.append(row=row) + + column_stacked = self.founders.column_stack(to_join) + + for index, row in enumerate(column_stacked): + + original_data = self.founders[index] + expected_data = original_data + original_data + self.assertEqual(row, expected_data) + + self.assertEqual(column_stacked[0], + ("John", "Adams", 90, "John", "Adams", 90)) + def test_wipe(self): """Purge a dataset.""" From 36bbe2726bb014188241164956273096ac795c05 Mon Sep 17 00:00:00 2001 From: Luca Beltrame Date: Mon, 15 Nov 2010 09:00:57 +0100 Subject: [PATCH 2/2] Remove unneded import --- test_tablib.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test_tablib.py b/test_tablib.py index 126ee84..e9ef5af 100755 --- a/test_tablib.py +++ b/test_tablib.py @@ -3,7 +3,6 @@ """Tests for Tablib.""" -from copy import copy import unittest import tablib