Skip to content

Commit 8098845

Browse files
author
Mike Dirolf
committed
add readline to GridOut PYTHON-157
1 parent e17b169 commit 8098845

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

gridfs/grid_file.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,22 @@ def read(self, size=-1):
390390
self.__buffer = data[size:]
391391
return to_return
392392

393+
def readline(self, size=-1):
394+
"""Read one line or up to `size` bytes from the file.
395+
396+
:Parameters:
397+
- `size` (optional): the maximum number of bytes to read
398+
399+
.. versionadded:: 1.8.1+
400+
"""
401+
bytes = ""
402+
while len(bytes) != size:
403+
byte = self.read(1)
404+
bytes += byte
405+
if byte == "" or byte =="\n":
406+
break
407+
return bytes
408+
393409
def tell(self):
394410
"""Return the current position of this file.
395411
"""

test/test_grid_file.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,24 @@ def test_multiple_reads(self):
367367
self.assertEqual("d", g.read(2))
368368
self.assertEqual("", g.read(2))
369369

370+
def test_readline(self):
371+
f = GridIn(self.db.fs, chunkSize=5)
372+
f.write("""Hello world,
373+
How are you?
374+
Hope all is well.
375+
Bye""")
376+
f.close()
377+
378+
g = GridOut(self.db.fs, f._id)
379+
self.assertEqual("H", g.read(1))
380+
self.assertEqual("ello world,\n", g.readline())
381+
self.assertEqual("How a", g.readline(5))
382+
self.assertEqual("", g.readline(0))
383+
self.assertEqual("re you?\n", g.readline())
384+
self.assertEqual("Hope all is well.\n", g.readline(1000))
385+
self.assertEqual("Bye", g.readline())
386+
self.assertEqual("", g.readline())
387+
370388
def test_iterator(self):
371389
f = GridIn(self.db.fs)
372390
f.close()

0 commit comments

Comments
 (0)