Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from cStringIO import StringIO
- class LineBuffer(object):
- """
- Simple line buffer implementation for line-based data streams.
- Use like this (in this example with a socket `s`):
- ------
- from line_buffer import LineBuffer
- ...
- lb = LineBuffer()
- while True:
- lb.feed(s.recv(2048))
- for line in lb:
- print(line)
- ...
- ------
- If you set `strip` to False in the constructor, CR and LF are retained,
- otherwise they are stripped.
- """
- def __init__(self, strip=True):
- self.strip = strip
- self.buffer = StringIO()
- def feed(self, data):
- self.buffer.write(data)
- def __iter__(self):
- self.buffer.seek(0)
- return self
- def next(self):
- line = self.buffer.readline()
- if line.endswith('\n'):
- return line.strip('\r\n') if self.strip else line
- else:
- self.buffer.seek(0)
- self.buffer.write(line)
- self.buffer.truncate()
- raise StopIteration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement