def test_line_iterator(self): f = io.BytesIO(b"Hello\nWorld\n\nfoo") self.assertEqual(list(iotools.line_iterator(f)), [b"Hello\n", b"World\n", b"\n", b"foo"]) f = io.BytesIO(b"Hello\nWorld\n\nfoo") self.assertEqual(list(iotools.line_iterator(f, 10)), [b"Hello\n", b"Worl"])
def test_line_iterator(self): f = io.BytesIO(b'Hello\nWorld\n\nfoo') self.assertEqual(list(iotools.line_iterator(f)), [b'Hello\n', b'World\n', b'\n', b'foo']) f = io.BytesIO(b'Hello\nWorld\n\nfoo') self.assertEqual(list(iotools.line_iterator(f, 10)), [b'Hello\n', b'Worl'])
def readline(self, size=-1): """ Read `size` bytes from the file starting from current position in the file until the end of the line. If `size` is negative read until end of the line. :param int size: Number of bytes to read from the current line. """ # type: (int) -> bytes return next(line_iterator(self, size)) # type: ignore
def readlines(self, hint=-1): """ Read `hint` lines from the file starting from current position. If `hint` is negative read until end of the line. :param int hint: Number of lines to read. """ # type: (int) -> List[bytes] lines = [] size = 0 for line in line_iterator(self): # type: ignore lines.append(line) size += len(line) if hint != -1 and size > hint: break return lines
def __iter__(self): return iotools.line_iterator(self)
def readline(self, size=None): return next(iotools.line_iterator(self, size))
def readline(self, size=-1): return next(line_iterator(self, None if size == -1 else size))