File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff 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 """
Original file line number Diff line number Diff 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 ()
You can’t perform that action at this time.
0 commit comments