def test_insert_at():

    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [7, 8, 9]
    indices = [2, 2, 4]
    r = insert_at(list1, list2, indices)
    assert r == [1, 2, 7, 8, 3, 4, 9, 5, 6]

    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [7, 8, 9]
    indices = [0, 2, 3]
    r = insert_at(list1, list2, indices)
    assert r == [7, 1, 2, 8, 3, 9, 4, 5, 6]

    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [7, 8, 9]
    indices = [2, 2, 20]  # Index too damn high!
    with pytest.raises(AssertionError):
        r = insert_at(list1, list2, indices)


    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [7, 8, 9]
    indices = [2, 3]  # len(indeces) does not match len(list2)
    with pytest.raises(AssertionError):
        r = insert_at(list1, list2, indices)

    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [7, 8, 9]
    indices = [2, 2, 6]  # Index too damn high!
    r = insert_at(list1, list2, indices)
    assert r == [1, 2, 7, 8, 3, 4, 5, 6, 9]

    list1 = []
    list2 = [0, 1, 2, 3, 4]
    indices = [0, 0, 0, 0, 0]
    r = insert_at(list1, list2, indices)
    assert r == [0, 1, 2, 3, 4]
Пример #2
0
def test_insert_at():

    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [7, 8, 9]
    indices = [2, 2, 4]
    r = insert_at(list1, list2, indices)
    assert r == [1, 2, 7, 8, 3, 4, 9, 5, 6]

    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [7, 8, 9]
    indices = [0, 2, 3]
    r = insert_at(list1, list2, indices)
    assert r == [7, 1, 2, 8, 3, 9, 4, 5, 6]

    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [7, 8, 9]
    indices = [2, 2, 20]  # Index too damn high!
    with pytest.raises(AssertionError):
        r = insert_at(list1, list2, indices)


    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [7, 8, 9]
    indices = [2, 3]  # len(indeces) does not match len(list2)
    with pytest.raises(AssertionError):
        r = insert_at(list1, list2, indices)

    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [7, 8, 9]
    indices = [2, 2, 6]  # Index too damn high!
    r = insert_at(list1, list2, indices)
    assert r == [1, 2, 7, 8, 3, 4, 5, 6, 9]

    list1 = []
    list2 = [0, 1, 2, 3, 4]
    indices = [0, 0, 0, 0, 0]
    r = insert_at(list1, list2, indices)
    assert r == [0, 1, 2, 3, 4]
Пример #3
0
    def get_experiment_list_str(self, exp_record_dict):
        headers = {
            'full': [
                ExpRecordDisplayFields.RUNS, ExpRecordDisplayFields.DURATION,
                ExpRecordDisplayFields.STATUS,
                ExpRecordDisplayFields.ARGS_CHANGED,
                ExpRecordDisplayFields.RESULT_STR, ExpRecordDisplayFields.NOTES
            ],
            'results': [ExpRecordDisplayFields.RESULT_STR]
        }[self.view_mode]

        if self.remove_prefix:
            deprefixed_ids = deprefix_experiment_ids(exp_record_dict.keys())
            exp_record_dict = OrderedDict(
                (k, v)
                for k, v in zip(deprefixed_ids, exp_record_dict.values()))

        row_func = _get_record_rows_cached if self.cache_result_string else _get_record_rows
        header_names = [h.value for h in headers]

        def remove_notes_if_no_notes(_record_rows):
            notes_column_index = headers.index(
                ExpRecordDisplayFields.NOTES
            ) if ExpRecordDisplayFields.NOTES in headers else None
            # Remove the notes column if there are no notes!
            if notes_column_index is not None and all(
                    row[notes_column_index] == '' for row in _record_rows):
                for row in _record_rows:
                    del row[notes_column_index]

        if self.display_format == 'nested':  # New Display mode

            if self.show_args:
                _, argdiff = separate_common_items([
                    load_experiment(ex).get_args().items()
                    for ex in exp_record_dict
                ])
                argdiff = {
                    k: args
                    for k, args in izip_equal(exp_record_dict.keys(), argdiff)
                }
            # Build a list of experiments and a list of records.
            full_headers = ['#'] + header_names
            record_rows = []
            experiment_rows = []
            experiment_row_ixs = []
            counter = 1  # Start at 2 because record table has the headers.
            for i, (exp_id, record_ids) in enumerate(exp_record_dict.items()):
                experiment_row_ixs.append(counter)
                exp_identifier = exp_id if not self.show_args else ','.join(
                    '{}={}'.format(k, v) for k, v in argdiff[exp_id])
                experiment_rows.append([i, exp_identifier])
                for j, record_id in enumerate(record_ids):
                    record_rows.append([j] + row_func(
                        record_id,
                        headers,
                        raise_display_errors=self.raise_display_errors,
                        truncate_to=self.truncate_result_to,
                        ignore_valid_keys=self.ignore_valid_keys))
                    counter += 1
            remove_notes_if_no_notes(record_rows)
            # Merge the experiments table and record table.
            record_table_rows = tabulate(record_rows,
                                         headers=full_headers,
                                         tablefmt="pipe").split('\n')
            del record_table_rows[1]  # Get rid of that silly line.
            experiment_table_rows = tabulate(
                experiment_rows, numalign='left').split('\n')[
                    1:-1]  # First and last are just borders
            longest_row = max(max(len(r) for r in record_table_rows),
                              max(len(r) for r in experiment_table_rows) +
                              4) if len(record_table_rows) > 0 else 0
            record_table_rows = [
                r if len(r) == longest_row else r[:-1] + ' ' *
                (longest_row - len(r)) + r[-1] for r in record_table_rows
            ]
            experiment_table_rows = [
                ('=' if i == 0 else '-') * longest_row + '\n' + r + ' ' *
                (longest_row - len(r) - 1) + '|'
                for i, r in enumerate(experiment_table_rows)
            ]
            all_rows = [
                surround_with_header(
                    'Experiments', width=longest_row, char='=')
            ] + insert_at(record_table_rows,
                          experiment_table_rows,
                          indices=experiment_row_ixs) + ['=' * longest_row]
            table = '\n'.join(all_rows)

        elif self.display_format == 'flat':  # Display First record on same row
            full_headers = ['E#', 'R#', 'Experiment'] + header_names
            rows = []
            for i, (exp_id, record_ids) in enumerate(exp_record_dict.items()):
                if len(record_ids) == 0:
                    rows.append([str(i), '', exp_id, '<No Records>'] + ['-'] *
                                (len(headers) - 1))
                else:
                    for j, record_id in enumerate(record_ids):
                        rows.append([
                            str(i) if j == 0 else '', j, exp_id if j ==
                            0 else ''
                        ] + row_func(
                            record_id,
                            headers,
                            raise_display_errors=self.raise_display_errors,
                            truncate_to=self.truncate_result_to,
                            ignore_valid_keys=self.ignore_valid_keys))
            remove_notes_if_no_notes(rows)

            table = tabulate(rows, headers=full_headers)
        else:
            raise NotImplementedError(self.display_format)

        return table
Пример #4
0
    def get_experiment_list_str(self, exp_record_dict):
        headers = {
            'full': [ExpRecordDisplayFields.RUNS, ExpRecordDisplayFields.DURATION, ExpRecordDisplayFields.STATUS, ExpRecordDisplayFields.ARGS_CHANGED, ExpRecordDisplayFields.RESULT_STR, ExpRecordDisplayFields.NOTES],
            'results': [ExpRecordDisplayFields.RESULT_STR]
            }[self.view_mode]

        if self.remove_prefix:
            deprefixed_ids = deprefix_experiment_ids(exp_record_dict.keys())
            exp_record_dict = OrderedDict((k, v) for k, v in zip(deprefixed_ids, exp_record_dict.values()))

        row_func = _get_record_rows_cached if self.cache_result_string else _get_record_rows
        header_names = [h.value for h in headers]

        def remove_notes_if_no_notes(_record_rows, _record_headers):
            notes_column_index = _record_headers.index(ExpRecordDisplayFields.NOTES.value) if ExpRecordDisplayFields.NOTES.value in _record_headers else None
            # Remove the notes column if there are no notes!
            if notes_column_index is not None and all(row[notes_column_index]=='' or row[notes_column_index]=='-' for row in _record_rows):
                new_rows = []
                for row in _record_rows:
                    new_rows.append(row[:notes_column_index]+row[notes_column_index+1:])
                new_headers = _record_headers[:notes_column_index]+_record_headers[notes_column_index+1:]
            else:
                new_rows = _record_rows
                new_headers = _record_headers
            return new_rows, new_headers

        if self.display_format=='nested':  # New Display mode

            if self.show_args:
                _, argdiff = separate_common_items([load_experiment(ex).get_args().items() for ex in exp_record_dict])
                argdiff = {k: args for k, args in izip_equal(exp_record_dict.keys(), argdiff)}
            # Build a list of experiments and a list of records.
            full_headers = ['#']+header_names
            record_rows = []
            experiment_rows = []
            experiment_row_ixs = []
            counter = 1  # Start at 2 because record table has the headers.
            for i, (exp_id, record_ids) in enumerate(exp_record_dict.items()):
                experiment_row_ixs.append(counter)
                exp_identifier = exp_id if not self.show_args else ','.join('{}={}'.format(k, v) for k, v in argdiff[exp_id])
                experiment_rows.append([i, exp_identifier])
                for j, record_id in enumerate(record_ids):
                    record_rows.append([j]+row_func(record_id, headers, raise_display_errors=self.raise_display_errors, truncate_to=self.truncate_result_to, ignore_valid_keys=self.ignore_valid_keys))
                    counter+=1
            record_rows, full_headers = remove_notes_if_no_notes(record_rows, full_headers)
            # Merge the experiments table and record table.

            if self.table_package=='tabulate':
                record_table_rows = tabulate(record_rows, headers=full_headers, tablefmt="pipe").split('\n')
                del record_table_rows[1]  # Get rid of that silly line.
                experiment_table_rows = tabulate(experiment_rows, numalign='left').split('\n')[1:-1]  # First and last are just borders
                longest_row = max(max(len(r) for r in record_table_rows), max(len(r) for r in experiment_table_rows)+4) if len(experiment_table_rows)>0 and len(record_table_rows)>0 else 0
                record_table_rows = [r if len(r)==longest_row else r[:-1] + ' '*(longest_row-len(r)) + r[-1] for r in record_table_rows]
                experiment_table_rows = [('=' if i==0 else '-')*longest_row+'\n'+r + ' '*(longest_row-len(r)-1)+'|' for i, r in enumerate(experiment_table_rows)]
                all_rows = [surround_with_header('Experiments', width=longest_row, char='=')] + (insert_at(record_table_rows, experiment_table_rows, indices=experiment_row_ixs) if len(experiment_table_rows)>0 else ['<No non-root Experiments>']) + ['='*longest_row]
                table = '\n'.join(all_rows)
            else:
                raise NotImplementedError(self.table_package)

        elif self.display_format=='flat':  # Display First record on same row
            full_headers = ['E#', 'R#', 'Experiment']+header_names
            rows = []
            for i, (exp_id, record_ids) in enumerate(exp_record_dict.items()):
                if len(record_ids)==0:
                    rows.append([str(i), '', exp_id, '<No Records>'] + ['-']*(len(headers)-1))
                else:
                    for j, record_id in enumerate(record_ids):
                        rows.append([str(i) if j==0 else '', j, exp_id if j==0 else '']+row_func(record_id, headers, raise_display_errors=self.raise_display_errors, truncate_to=self.truncate_result_to, ignore_valid_keys=self.ignore_valid_keys))
            assert len(rows[0])==len(full_headers)
            rows, full_headers = remove_notes_if_no_notes(rows, full_headers)

            if self.table_package == 'pretty_table':
                from prettytable.prettytable import PrettyTable
                table = str(PrettyTable(rows, field_names=full_headers, align='l', max_table_width=self.max_width))
            elif self.table_package == 'tabulate':
                table = tabulate(rows, headers=full_headers)
            else:
                raise NotImplementedError(self.table_package)
        else:
            raise NotImplementedError(self.display_format)

        return table