Solution: collect packets
The implementation
examples/iterators/packets/packets.py
class Packets(): def __init__(self, filename): self.filename = filename self.fh = open(filename) self.packets = {} self.max = {} def __iter__(self): return self def __next__(self): while True: line = self.fh.readline() #print(f"line: {line}") if line == '': raise StopIteration line = line.rstrip("\n") if line == '': continue pid, seqid, maxseq, content = line.split(",") pid = int(pid) seqid = int(seqid) maxseq = int(maxseq) if pid not in self.packets: self.packets[pid] = {} self.max[pid] = maxseq if seqid in self.packets[pid]: raise Exception("pid arrived twice") if maxseq != self.max[pid]: raise Exception("maxseq changed") self.packets[pid][seqid] = content if len(self.packets[pid].keys()) == self.max[pid]: content = list(map(lambda i: self.packets[pid][i+1], range(self.max[pid]))) del(self.max[pid]) del(self.packets[pid]) return content
The use:
examples/iterators/packets/use_packetes.py
import sys from packets import Packets if len(sys.argv) < 2: exit(f"Usage: {sys.argv[0]} FILENAME") for packet in Packets(sys.argv[1]): print(packet)
The test to verify it
examples/iterators/packets/test_packets.py
import os import json import pytest from packets import Packets root = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(root, 'packets.json')) as fh: expected_results = json.load(fh) @pytest.mark.parametrize('filename', ['packets.txt', 'packets1.txt', 'packets2.txt']) def test_packetes(filename): filepath = os.path.join(root, filename) results = [] for packet in Packets(filepath): results.append(packet) assert results == expected_results
Expected result:
examples/iterators/packets/packets.json
[["First of Twelve", "Second of Twelve", "Third of Twelve", "Fourth of Twelve", "Fifth of Twelve"], ["First of Nine", "Second of Nine", "Third of Nine", "Fourth of Nine"], ["First of Eleven", "Second of Eleven", "Third of Eleven"]]