diff --git a/backend/app/modelling/batching.py b/backend/app/modelling/batching.py index 914746e24..7cf02afef 100644 --- a/backend/app/modelling/batching.py +++ b/backend/app/modelling/batching.py @@ -16,4 +16,18 @@ def pack_postcode_batches( """Pack *properties* into batches of ~*batch_size*, never splitting a postcode across batches. A single postcode larger than *batch_size* becomes its own oversized batch.""" - raise NotImplementedError + groups: dict[str, list[FilteredProperty]] = {} + for prop in properties: + groups.setdefault(prop.postcode or "", []).append(prop) + + batches: list[list[FilteredProperty]] = [] + current: list[FilteredProperty] = [] + for group in groups.values(): + if current and len(current) + len(group) > batch_size: + batches.append(current) + current = list(group) + else: + current.extend(group) + if current: + batches.append(current) + return batches