Advanced
Parse a stream that arrives in pieces
Write a class SseParser with a method feed(chunk) that returns a list of the events finished by this chunk. A chunk can end anywhere, even in the middle of a line.
Rules: a line starting with data: adds its value (without one optional leading space) to the current event. A blank line finishes the event (several data: lines are joined with a newline). Lines starting with : are comments and ignored. Strip a trailing \r from each line. An event that is not yet finished is not returned.
What Check tests
- Two events in one chunk
- A chunk that ends in the middle of a line
- The same stream one character at a time
- Comments are ignored and two data lines make one event
- An unfinished event is not returned
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.