Skip to content

Input

input

Input handling for the security classification process.

read_identifiers(filename: str | Path) -> list[SecurityIdentifier]

Read security identifiers from an Excel file.

The input file must contain the columns Type and Value. Type may be omitted or empty. In that case the identifier type is detected automatically for ISINs and tickers.

Source code in src/equities_classifier/processflow/input.py
def read_identifiers(
    filename: str | Path,
) -> list[SecurityIdentifier]:
    """Read security identifiers from an Excel file.

    The input file must contain the columns ``Type`` and ``Value``.
    ``Type`` may be omitted or empty. In that case the identifier type
    is detected automatically for ISINs and tickers.
    """

    path = Path(filename)
    workbook = load_workbook(path, read_only=True, data_only=True)

    try:
        worksheet = workbook.active
        rows = worksheet.iter_rows(values_only=True)

        headers = next(rows, None)
        if headers is None:
            return []

        header_map = _get_header_map(headers)
        if "value" not in header_map:
            message = "Input file must contain a 'Value' column."
            raise ValueError(message)

        return [
            _create_identifier(row, header_map, row_number)
            for row_number, row in enumerate(rows, start=2)
        ]
    finally:
        workbook.close()