Пример #1
0
def get_row(row_number: int, referencing_row: Row,
            referencing_column: Column) -> Row:
    """ Return the row at a row index. """

    context = os.path.basename(referencing_row.data_path)

    from_row_index = referencing_row.row_index
    from_column_name = referencing_column.name

    # when looking at rows in a CSV they are not zero-based, and the first row
    # is always the headers, which makes the first row of actual data (that
    # you see) appear visually at row #2, like for example:
    #   #1 'rank,title'
    #   #2 '2,My Card'
    #   #2 '4,My Other Card'
    # however, of course, when reading from the file, the first row is
    # actually at index 0, so we have to take this into account
    # this logic essentially makes '#0' and '#1' invalid row numbers
    line_number = row_number - 2

    if line_number < 0:
        if row_number == 1:
            # special case- user probably meant the first data row (which is always #2)
            WarningDisplay.referencing_row_header(
                WarningContext(context,
                               row_index=from_row_index,
                               column=from_column_name))
        else:
            WarningDisplay.referencing_row_out_of_bounds(
                WarningContext(context,
                               row_index=from_row_index,
                               column=from_column_name),
                referenced_row_number=row_number)

        return None

    with open(referencing_row.data_path) as data_file_raw:
        data_file = FileWrapper(data_file_raw)
        # read data appropriately
        data = csv.DictReader(lower_first_row(data_file))

        try:
            # then read rows until reaching the target row_number
            row_data = next(itertools.islice(data, line_number, None))
        except StopIteration:
            WarningDisplay.referencing_row_out_of_bounds(
                WarningContext(context,
                               row_index=from_row_index,
                               column=from_column_name),
                referenced_row_number=row_number)
        else:
            if Row.is_excluded(data_file.raw_line):
                WarningDisplay.referencing_excluded_row(
                    WarningContext(context,
                                   row_index=from_row_index,
                                   column=from_column_name),
                    referenced_row_number=row_number)
            else:
                # create a new row with the data at the referenced row
                return Row(row_data, referencing_row.data_path)
Пример #2
0
def get_row(row_number: int, referencing_row: Row, referencing_column: Column) -> Row:
    """ Return the row at a row index. """

    context = os.path.basename(referencing_row.data_path)

    from_row_index = referencing_row.row_index
    from_column_name = referencing_column.name

    # when looking at rows in a CSV they are not zero-based, and the first row
    # is always the headers, which makes the first row of actual data (that
    # you see) appear visually at row #2, like for example:
    #   #1 'rank,title'
    #   #2 '2,My Card'
    #   #2 '4,My Other Card'
    # however, of course, when reading from the file, the first row is
    # actually at index 0, so we have to take this into account
    # this logic essentially makes '#0' and '#1' invalid row numbers
    line_number = row_number - 2

    if line_number < 0:
        if row_number == 1:
            # special case- user probably meant the first data row (which is always #2)
            WarningDisplay.referencing_row_header(
                WarningContext(context, row_index=from_row_index, column=from_column_name))
        else:
            WarningDisplay.referencing_row_out_of_bounds(
                WarningContext(context, row_index=from_row_index, column=from_column_name),
                referenced_row_number=row_number)

        return None

    with open(referencing_row.data_path) as data_file_raw:
        data_file = FileWrapper(data_file_raw)
        # read data appropriately
        data = csv.DictReader(lower_first_row(data_file))

        try:
            # then read rows until reaching the target row_number
            row_data = next(itertools.islice(data, line_number, None))
        except StopIteration:
            WarningDisplay.referencing_row_out_of_bounds(
                WarningContext(context, row_index=from_row_index, column=from_column_name),
                referenced_row_number=row_number)
        else:
            if Row.is_excluded(data_file.raw_line):
                WarningDisplay.referencing_excluded_row(
                    WarningContext(context, row_index=from_row_index, column=from_column_name),
                    referenced_row_number=row_number)
            else:
                # create a new row with the data at the referenced row
                return Row(row_data, referencing_row.data_path)