from __future__ import annotations from collections.abc import Iterable, Iterator from domain.addresses.unstandardised_address import AddressList, UnstandardisedAddress from utilities.grouped_batching import iter_grouped_batches def iter_postcode_grouped_batches( addresses: Iterable[UnstandardisedAddress], *, max_batch_size: int = 500, ) -> Iterator[AddressList]: for batch in iter_grouped_batches( addresses, key=lambda a: a.postcode, max_batch_size=max_batch_size ): yield AddressList(batch)