diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py index e655bbb7bfffa877df60980bf05d625597c28d74..6eea1f01062c8bc8e744ba15f8f15f2926260102 100644 --- a/modules/base/pymod/table.py +++ b/modules/base/pymod/table.py @@ -365,7 +365,7 @@ class Table(object): def __str__(self): return self.ToString() - def _AddRowsFromDict(self, d, merge=False): + def _AddRowsFromDict(self, d, overwrite=False): # get column indices idxs = [self.GetColIndex(k) for k in d.keys()] @@ -388,11 +388,11 @@ class Table(object): new_row[idx] = self._Coerce(v, self.col_types[idx]) # partially overwrite existing row with new data - if merge: - merge_idx = self.GetColIndex(merge) + if overwrite: + overwrite_idx = self.GetColIndex(overwrite) added = False for i,r in enumerate(self.rows): - if r[merge_idx]==new_row[merge_idx]: + if r[overwrite_idx]==new_row[overwrite_idx]: for j,e in enumerate(self.rows[i]): if new_row[j]==None: new_row[j] = e @@ -400,12 +400,12 @@ class Table(object): added = True break - # if not merge or merge did not find appropriate row - if not merge or not added: + # if not overwrite or overwrite did not find appropriate row + if not overwrite or not added: self.rows.append(new_row) - def AddRow(self, data, merge=None): + def AddRow(self, data, overwrite=None): """ Add a row to the table. @@ -417,17 +417,13 @@ class Table(object): columns in the table. A :class:`ValuerError` is raised otherwise. The values are added in the order specified in the list, thus, the order of the data must match the columns. - - *merge* looks for an existing row and adds the data there instead of - appending a new row. If merge is set to an existing column name, all - existing rows are searched for the first row where the value of column with - the name specified in merge is equal to the value of the specified column in - the new data. If such a row is found, all existing data of this row is - overwritten with the new data. If no matching row is found, a new row is - appended to the table. + + If *overwrite* is set and not None (must be set to an existing column name), + an existing row is overwritten if the value of column *overwrite* matches. + If no matching row is found, a new row is appended to the table. """ if type(data)==dict: - self._AddRowsFromDict(data, merge) + self._AddRowsFromDict(data, overwrite) else: if len(data)!=len(self.col_names): msg='data array must have %d elements, not %d' @@ -435,17 +431,17 @@ class Table(object): new_row = [self._Coerce(v, t) for v, t in zip(data, self.col_types)] # fully overwrite existing row with new data - if merge: - merge_idx = self.GetColIndex(merge) + if overwrite: + overwrite_idx = self.GetColIndex(overwrite) added = False for i,r in enumerate(self.rows): - if r[merge_idx]==new_row[merge_idx]: + if r[overwrite_idx]==new_row[overwrite_idx]: self.rows[i] = new_row added = True break - # if not merge or merge did not find appropriate row - if not merge or not added: + # if not overwrite or overwrite did not find appropriate row + if not overwrite or not added: self.rows.append(new_row) def RemoveCol(self, col): @@ -1477,10 +1473,10 @@ class Table(object): and the classification columns direction (*class_dir*). This will generate the classification on the fly - * if *class_dir*=='-': values in the classification column that are + - if *class_dir* =='-': values in the classification column that are less than or equal to *class_cutoff* will be counted as positives - * if *class_dir*=='+': values in the classification column that are + - if *class_dir* =='+': values in the classification column that are larger than or equal to *class_cutoff* will be counted as positives @@ -1652,7 +1648,7 @@ class Table(object): return False return True - def Extend(self, tab, merge=None): + def Extend(self, tab, overwrite=None): """ Append each row of *tab* to the current table. The data is appended based on the column names, thus the order of the table columns is *not* relevant, @@ -1665,13 +1661,9 @@ class Table(object): If the type of any column in *tab* is not the same as in the current table a *TypeError* is raised. - If merge is specified, the function looks for an existing row and adds the - data there instead of appending a new row. If merge is set to an existing - column name, all existing rows are searched for the first row where the - value of column with the name specified in merge is equal to the value of - the specified column in the new data. If such a row is found, all existing - data of this row is overwritten with the new data. If no matching row is - found, a new row is appended to the table. + If *overwrite* is set and not None (must be set to an existing column name), + an existing row is overwritten if the value of column *overwrite* matches. + If no matching row is found, a new row is appended to the table. """ # add column to current table if it doesn't exist for name,typ in zip(tab.col_names, tab.col_types): @@ -1691,7 +1683,7 @@ class Table(object): for i in range(0,num_rows): row = tab.rows[i] data = dict(zip(tab.col_names,row)) - self.AddRow(data, merge) + self.AddRow(data, overwrite) def Merge(table1, table2, by, only_matching=False): diff --git a/modules/base/tests/test_table.py b/modules/base/tests/test_table.py index 6acb407c65f26e168ceb169ffdb70c5bd19bb25f..ea823869c3e67cec719dce4b21794e005cb6f85e 100644 --- a/modules/base/tests/test_table.py +++ b/modules/base/tests/test_table.py @@ -62,9 +62,9 @@ class TestTable(unittest.TestCase): self.CompareColCount(tab, 3) self.CompareRowCount(tab, 0) self.CompareColTypes(tab, ['first','second', 'third'], 'sif') - tab.AddRow(['x',3, None], merge=None) - tab.AddRow(['foo',None, 2.2], merge=None) - tab.AddRow([None,9, 3.3], merge=None) + tab.AddRow(['x',3, None], overwrite=None) + tab.AddRow(['foo',None, 2.2], overwrite=None) + tab.AddRow([None,9, 3.3], overwrite=None) return tab def CompareRowCount(self, t, row_count): @@ -375,7 +375,7 @@ class TestTable(unittest.TestCase): tab = Table(['first'],'i') self.CompareColCount(tab, 1) self.CompareRowCount(tab, 0) - tab.AddRow([2], merge=None) + tab.AddRow([2], overwrite=None) self.CompareColCount(tab, 1) self.CompareRowCount(tab, 1) self.CompareColNames(tab, ['first']) @@ -394,7 +394,7 @@ class TestTable(unittest.TestCase): tab.AddCol('first', 'int') self.CompareColCount(tab, 1) self.CompareRowCount(tab, 0) - tab.AddRow([2], merge=None) + tab.AddRow([2], overwrite=None) self.CompareColCount(tab, 1) self.CompareRowCount(tab, 1) self.CompareColNames(tab, ['first']) @@ -414,7 +414,7 @@ class TestTable(unittest.TestCase): self.CompareColCount(tab, 2) self.CompareRowCount(tab, 0) self.CompareColTypes(tab, ['first','second'], 'si') - tab.AddRow(['x',3], merge=None) + tab.AddRow(['x',3], overwrite=None) self.CompareColCount(tab, 2) self.CompareRowCount(tab, 1) tab.AddCol('third', 'float', 3.141) @@ -441,9 +441,9 @@ class TestTable(unittest.TestCase): self.CompareColCount(tab, 3) self.CompareRowCount(tab, 0) self.CompareColTypes(tab, ['first','second', 'third'], 'sif') - tab.AddRow(['x',3, 1.0], merge=None) - tab.AddRow(['foo',6, 2.2], merge=None) - tab.AddRow(['bar',9, 3.3], merge=None) + tab.AddRow(['x',3, 1.0], overwrite=None) + tab.AddRow(['foo',6, 2.2], overwrite=None) + tab.AddRow(['bar',9, 3.3], overwrite=None) self.CompareColCount(tab, 3) self.CompareRowCount(tab, 3) self.CompareDataFromDict(tab, {'second': [3,6,9], 'first': ['x','foo','bar'], 'third': [1,2.2,3.3]}) @@ -466,9 +466,9 @@ class TestTable(unittest.TestCase): self.CompareColCount(tab, 3) self.CompareRowCount(tab, 0) self.CompareColTypes(tab, ['first','second', 'aaa'], 'sif') - tab.AddRow({'first':'x','second':3, 'aaa':1.0}, merge=None) - tab.AddRow({'aaa':2.2, 'second':6, 'first':'foo'}, merge=None) - tab.AddRow({'second':9, 'aaa':3.3, 'first':'bar'}, merge=None) + tab.AddRow({'first':'x','second':3, 'aaa':1.0}, overwrite=None) + tab.AddRow({'aaa':2.2, 'second':6, 'first':'foo'}, overwrite=None) + tab.AddRow({'second':9, 'aaa':3.3, 'first':'bar'}, overwrite=None) self.CompareColCount(tab, 3) self.CompareRowCount(tab, 3) self.CompareDataFromDict(tab, {'second': [3,6,9], 'first': ['x','foo','bar'], 'aaa': [1,2.2,3.3]}) @@ -490,9 +490,9 @@ class TestTable(unittest.TestCase): self.CompareColCount(tab, 1) self.CompareRowCount(tab, 0) self.CompareColTypes(tab, ['first'], 's') - tab.AddRow(['x'], merge=None) - tab.AddRow(['foo'], merge=None) - tab.AddRow(['bar'], merge=None) + tab.AddRow(['x'], overwrite=None) + tab.AddRow(['foo'], overwrite=None) + tab.AddRow(['bar'], overwrite=None) tab.AddCol('second', 'int') tab.AddCol('third', 'float', 3.141) self.CompareColCount(tab, 3) @@ -501,9 +501,9 @@ class TestTable(unittest.TestCase): 'first': ['x','foo','bar'], 'third': [3.141, 3.141, 3.141]}) - def testAddRowFromDictWithMerge(self): + def testAddRowFromDictWithOverwrite(self): ''' - add rows from dictionary with merge (i.e. overwrite third row with additional data) + add rows from dictionary with overwrite (i.e. overwrite third row with additional data) x foo bar ------------------ @@ -522,14 +522,14 @@ class TestTable(unittest.TestCase): self.CompareDataFromDict(tab, {'x': ['row1', 'row2', 'row3'], 'foo': [True, None, False], 'bar': [1, 2, None]}) - tab.AddRow({'x':'row3', 'bar':3}, merge='x') + tab.AddRow({'x':'row3', 'bar':3}, overwrite='x') self.CompareDataFromDict(tab, {'x': ['row1', 'row2', 'row3'], 'foo': [True, None, False], 'bar': [1, 2, 3]}) - def testAddRowFromListWithMerge(self): + def testAddRowFromListWithOverwrite(self): ''' - add rows from list with merge (i.e. overwrite third row with additional data) + add rows from list with overwrite (i.e. overwrite third row with additional data) x foo bar ------------------ @@ -549,7 +549,7 @@ class TestTable(unittest.TestCase): self.CompareDataFromDict(tab, {'x': ['row1', 'row2', 'row3'], 'foo': [True, None, False], 'bar': [1, 2, None]}) - tab.AddRow(['row3', True, 3], merge='x') + tab.AddRow(['row3', True, 3], overwrite='x') self.CompareDataFromDict(tab, {'x': ['row1', 'row2', 'row3'], 'foo': [True, None, True], 'bar': [1, 2, 3]}) @@ -1344,36 +1344,36 @@ class TestTable(unittest.TestCase): 'second': [3,None,9,3,None,9], 'third': [None,2.2,3.3,None,2.2,3.3]}) - # with merge (additional column) + # with overwrite (additional column) tab = self.CreateTestTable() tab2 = self.CreateTestTable() tab2.AddCol('foo','i',[1,2,3]) - tab.Extend(tab2, merge='first') + tab.Extend(tab2, overwrite='first') self.CompareDataFromDict(tab, {'first': ['x','foo',None], 'second': [3,None,9], 'third': [None,2.2,3.3], 'foo': [1,2,3]}) - # with merge (no matching value) + # with overwrite (no matching value) tab = self.CreateTestTable() tab2 = Table(['third','second','first'], 'fis', third=[None,2.2,3.3], first=['a','bar','bla'], second=[3, None, 9]) - tab.Extend(tab2, merge='first') + tab.Extend(tab2, overwrite='first') self.CompareDataFromDict(tab, {'first': ['x','foo',None,'a','bar','bla'], 'second': [3,None,9,3,None,9], 'third': [None,2.2,3.3,None,2.2,3.3]}) - # with merge (with matching values) + # with overwrite (with matching values) tab = self.CreateTestTable() tab2 = Table(['third','second','first'], 'fis', third=[None,2.2,3.4], first=['a','bar','bla'], second=[3, None, 9]) - tab.Extend(tab2, merge='third') + tab.Extend(tab2, overwrite='third') self.CompareDataFromDict(tab, {'first': ['a','bar',None,'bla'], 'second': [3,None,9,9], 'third': [None,2.2,3.3,3.4]})