add readline to GridOut PYTHON-157

This commit is contained in:
Mike Dirolf 2010-08-20 11:26:36 -04:00
parent e17b1694d7
commit 8098845c0e
2 changed files with 34 additions and 0 deletions

View File

@ -390,6 +390,22 @@ class GridOut(object):
self.__buffer = data[size:]
return to_return
def readline(self, size=-1):
"""Read one line or up to `size` bytes from the file.
:Parameters:
- `size` (optional): the maximum number of bytes to read
.. versionadded:: 1.8.1+
"""
bytes = ""
while len(bytes) != size:
byte = self.read(1)
bytes += byte
if byte == "" or byte =="\n":
break
return bytes
def tell(self):
"""Return the current position of this file.
"""

View File

@ -367,6 +367,24 @@ class TestGridFile(unittest.TestCase):
self.assertEqual("d", g.read(2))
self.assertEqual("", g.read(2))
def test_readline(self):
f = GridIn(self.db.fs, chunkSize=5)
f.write("""Hello world,
How are you?
Hope all is well.
Bye""")
f.close()
g = GridOut(self.db.fs, f._id)
self.assertEqual("H", g.read(1))
self.assertEqual("ello world,\n", g.readline())
self.assertEqual("How a", g.readline(5))
self.assertEqual("", g.readline(0))
self.assertEqual("re you?\n", g.readline())
self.assertEqual("Hope all is well.\n", g.readline(1000))
self.assertEqual("Bye", g.readline())
self.assertEqual("", g.readline())
def test_iterator(self):
f = GridIn(self.db.fs)
f.close()