diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py index 8ad6bf5776afa335ff67e5988ac12d1bad70cf82..d143fa7bfb20820afc614310ca962f59d15d7a54 100644 --- a/modules/base/pymod/table.py +++ b/modules/base/pymod/table.py @@ -193,6 +193,15 @@ class Table(object): if len(kwargs)>0: self._AddRowsFromDict(kwargs) + def __getattr__(self, col_name): + # pickling doesn't call the standard __init__ defined above and thus + # col_names might not be defined. This leads to infinite recursions. + # Protect against it by checking that col_names is contained in + # __dict__ + if 'col_names' not in self.__dict__ or col_name not in self.col_names: + raise AttributeError(col_name) + return TableCol(self, col_name) + @staticmethod def _ParseColTypes(types, exp_num=None): if types==None: @@ -614,7 +623,6 @@ Statistics for column %(col)s self._AddRowsFromDict(data, overwrite) else: if len(data)!=len(self.col_names): - print data, self.col_names msg='data array must have %d elements, not %d' raise ValueError(msg % (len(self.col_names), len(data))) new_row = [self._Coerce(v, t) for v, t in zip(data, self.col_types)] @@ -1468,7 +1476,6 @@ Statistics for column %(col)s fig=plt.figure() ax=fig.add_subplot(111) legend_data=[] - for i in range(len(data)): legend_data.append(ax.bar(ind+i*single_bar_width,data[i],single_bar_width,bottom=bottom,color=colors[i],yerr=yerr_data[i], ecolor='black')[0]) @@ -2750,6 +2757,7 @@ Statistics for column %(col)s return False return True + def Extend(self, tab, overwrite=None): """ Append each row of *tab* to the current table. The data is appended based diff --git a/modules/base/tests/test_table.py b/modules/base/tests/test_table.py index cd11bd729623d9b5463f4cc8a67daf43a0f10cd6..33cd65ab21a9d04718513e58cf828294451aed24 100644 --- a/modules/base/tests/test_table.py +++ b/modules/base/tests/test_table.py @@ -163,11 +163,17 @@ class TestTable(unittest.TestCase): diff = ImageChops.difference(img1, img2) self.assertEqual(diff.getbbox(),None) - def testSearchColNames(self): + def testAllowsToSearchColNames(self): tab = self.CreateTestTable() self.assertEquals(tab.SearchColNames('d$'), ['second', 'third']) self.assertEquals(tab.SearchColNames('(first|third)'), ['first','third']) + def testProvidesDirectAccessToColumns(self): + tab = Table(['x', 'two'], 'ii') + tab.AddRow([1,2]) + self.assertEqual([1], list(tab.x)) + self.assertEqual([2], list(tab.two)) + self.assertRaises(AttributeError, tab.__getattr__, 'one') def testZip(self): tab=Table(['col1', 'col2', 'col3', 'col4'], 'sssi') tab.AddRow(['a', 'b', 'c', 1])