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_order_entries(self): entries = ['a', 'b', 'c', 'd', 'e'] indexes = [2, 1, 0, 3, 4] expected = ['c', 'b', 'a', 'd', 'e'] test = man.order_col(entries, indexes) self.assertEqual(test, expected)