from collections.abc import Generator, Iterable from typing import Any from fansifter_common.adapters.sendgrid.exceptions import SendGridClientError def batch_iterable( iterable: Iterable[Any], batch_size: int ) -> Generator[tuple[list[Any], int]]: batch: list[Any] = [] start_index = 0 for index, item in enumerate(iterable): if len(batch) == 0: start_index = index batch.append(item) if len(batch) == batch_size: yield batch, start_index batch = [] if batch: yield batch, start_index def get_sendgrid_error_log_msg(exc: SendGridClientError) -> str: if exc.response_obj and exc.response_obj.errors: error = exc.response_obj.errors[0] if error.field: return f"SendGridClientError: `{error.field}` {error.message}" return f"SendGridClientError: {error.message}" return exc.log_msg