Last active
October 14, 2015 23:33
-
-
Save teepark/8a72ae473e904820b9fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
def gen_lines(readable, chunk_size=8192, sep=b'\n'): | |
buf = io.BytesIO() | |
while 1: | |
buf.seek(0) | |
buffered = buf.read() | |
idx = buffered.find(sep) | |
if idx >= 0: | |
idx += len(sep) | |
buf.seek(0) | |
buf.truncate() | |
buf.write(buffered[idx:]) | |
yield buffered[:idx] | |
continue | |
data = readable.read(chunk_size) | |
if data == b'': # EOF | |
break | |
buf.write(data) | |
# only broke out of the loop after hitting EOF with no sep, | |
# so if there's anything dangling pass that out as well. | |
if buffered: | |
yield buffered | |
class LineReader(object): | |
def __init__(self, readable, chunk_size=8192, sep=b'\n'): | |
self._gen = gen_lines(readable, chunk_size, sep) | |
def __iter__(self): | |
return self._gen | |
def readline(self): | |
return next(self._gen) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment