def order_cols(line, table_list): ''' Execute 'ORDENATELOS X' command :param line: line without the 'ORDENATELOS X' :param table_list: a dict {col_name: [entries]} :return: a dict {col_name: [entries]} ''' def read_line(l): col_name, dir_str = l.split(' PA ') if 'RIBA' in dir_str: # Sort ascending asc = True elif 'BAJO' in dir_str: # Sort descending asc = False else: raise ValueError('direction string: {0}'.format(dir_str)) return col_name.strip(), asc col_name, asc = read_line(line) # Get table using col_name table = [tb for tb in table_list if man.has_col(tb, col_name)][0] # Get the list of sorted entry indexes for col_name index = man.get_sorted_indexes(table[col_name], asc) ordered_tb = {col_name: man.order_col(entries, index) for col_name, entries in table.items()} tb_list = [tb for tb in table_list if not man.has_col(tb, col_name)] tb_list.append(ordered_tb) return tb_list
def test_get_ordered_indexes_dsc(self): entries = ['c', 'e', 'd', 'a', 'b'] expected = [1, 2, 0, 4, 3] test = man.get_sorted_indexes(entries, False) self.assertEqual(test, expected)
def test_get_ordered_indexes_asc(self): entries = ['c', 'e', 'd', 'a', 'b'] expected = [3, 4, 0, 2, 1] test = man.get_sorted_indexes(entries, True) self.assertEqual(test, expected)