The post has been translated automatically. Original language: Russian
In this exercise a shift hands over two lists in matching order. The instruments are A, B and C. The readings are 18 and 21. The operator expects three records or rejection of the entire batch. Python’s default zip yields (A, 18) and (B, 21), then stops normally. Even the indication that C lacks a reading disappears.
Try list(zip(["A", "B", "C"], [18, 21], strict=True)). Python 3.10 and later raise ValueError. The timing matters: the iterator yields two pairs before the error. If the loop has already written them to a database, strict does not roll those writes back.
For this small batch, build the complete list in memory before saving it in one transaction. Acceptance test: with lengths 3/2, zero records persist and the operator sees both lengths; with 3/3, all three pairs persist. Large files need staging. Equal lengths cannot detect reordered readings; include the instrument ID on each row to check the match.
Учебная смена передала два списка в одном порядке. Номера приборов — A, B, C. Показания — 18 и 21. Оператор ожидает либо три записи, либо отказ всей партии. Обычный zip в Python вернёт пары (A, 18) и (B, 21), после чего цикл спокойно завершится. У прибора C исчезнет даже признак пропуска.
Проверка на маленьком файле занимает одну строку: list(zip(["A", "B", "C"], [18, 21], strict=True)). В Python 3.10 и новее результатом будет ValueError. Существенная деталь: итератор успел выдать две пары до ошибки. Если цикл уже записал их в базу, сам strict ничего не откатит.
Для этой партии сначала полностью формируем пары в памяти и только затем сохраняем результат одной транзакцией. Критерий приёмки: при 3/2 сохранено 0 записей, оператор видит обе длины; при 3/3 сохранены все три пары. Большим файлам потребуется промежуточное хранилище. Равенство длин не обнаружит перестановку показаний: для такой проверки нужен номер прибора в каждой строке.
Comments 3
Newest Popular