add readline to GridOut PYTHON-157
This commit is contained in:
parent
e17b1694d7
commit
8098845c0e
@ -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.
|
||||
"""
|
||||
|
||||
@ -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()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user