From d60de420ed1d03fc8a1d87800d2b0ada3d5f2012 Mon Sep 17 00:00:00 2001 From: Marco Biasini <marco.biasini@unibas.ch> Date: Tue, 17 May 2011 19:15:22 +0200 Subject: [PATCH] added Zip method to table class --- modules/base/pymod/table.py | 19 +++++++++++++++++++ modules/base/tests/test_table.py | 11 ++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py index c770fdc9e..4d6f3bdd4 100644 --- a/modules/base/pymod/table.py +++ b/modules/base/pymod/table.py @@ -508,6 +508,25 @@ class Table: return sign*cmp(lhs[key_index], rhs[key_index]) self.rows=sorted(self.rows, _key_cmp) + def Zip(self, *args): + """ + Allows to conveniently iterate over a selection of columns, e.g. + + .. code-block::python + + tab=Table.Load('...') + for col1, col in tab.Zip('col1', 'col2'): + print col1, col2 + + is a shortcut for + + .. code-block::python + + tab=Table.Load('...') + for col1, col2 in zip(tab['col1'], tab['col2']): + print col1, col2 + """ + return zip(*[self[arg] for arg in args]) def Plot(self, x, y=None, z=None, style='.', x_title=None, y_title=None, z_title=None, x_range=None, y_range=None, z_range=None, diff --git a/modules/base/tests/test_table.py b/modules/base/tests/test_table.py index 56d7ba134..463853d0c 100644 --- a/modules/base/tests/test_table.py +++ b/modules/base/tests/test_table.py @@ -100,7 +100,16 @@ class TestTable(unittest.TestCase): ref, "data (%s) in col (%s), row (%i) different from expected value (%s)" \ %(row[idx], col_name, i, ref)) - + def testZip(self): + tab=Table(['col1', 'col2', 'col3'], 'sss') + tab.AddRow(['a', 'b', 'c']) + tab.AddRow(['x', 'y', 'z']) + z=tab.Zip('col1', 'col3') + self.assertEqual(len(z), 2) + self.assertEqual(z[0][0], 'a') + self.assertEqual(z[0][1], 'c') + self.assertEqual(z[1][0], 'x') + self.assertEqual(z[1][1], 'z') def CompareColTypes(self, t, col_names, ref_types): ''' Compare the types of n columns specified by their names with reference -- GitLab