Package grizzled :: Package io :: Class PushbackFile
[frames] | no frames]

Class PushbackFile

object --+
         |
        PushbackFile

A file-like wrapper object that permits pushback.
Instance Methods
 
__init__(self, f)
Create a new PushbackFile object to wrap a file-like object.
 
write(self, buf)
Write the specified buffer to the file.
 
pushback(self, s)
Push a character or string back onto the input stream.
 
unread(self, s)
Push a character or string back onto the input stream.
 
read(self, n=-1)
Read n bytes from the open file.
str
readline(self, length=-1)
Read the next line from the file.
list
readlines(self, sizehint=0)
Read all remaining lines in the file.
 
__iter__(self)
str
next(self)
A file object is its own iterator.
 
close(self)
Close the file.
 
flush(self)
Force a flush.
 
truncate(self, size=-1)
Truncate the underlying file.
int
tell(self)
Return the file's current position, if applicable.
 
seek(self, offset, whence=os.SEEK_SET)
Set the file's current position.
int
fileno(self)
Return the integer file descriptor used by the underlying file.

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties

Inherited from object: __class__

Method Details

__init__(self, f)
(Constructor)

 
Create a new PushbackFile object to wrap a file-like object.
Parameters:
  • f (file) - A file-like object that contains both a write() method and a flush() method.
Overrides: object.__init__

write(self, buf)

 
Write the specified buffer to the file. This method throws an unconditional exception, since PushbackFile objects are read-only.
Parameters:
  • buf (str or bytes) - buffer to write
Raises:
  • NotImplementedError - unconditionally

pushback(self, s)

 
Push a character or string back onto the input stream.
Parameters:
  • s (str) - the string to push back onto the input stream

unread(self, s)

 
Push a character or string back onto the input stream.
Parameters:
  • s (str) - the string to push back onto the input stream

read(self, n=-1)

 
Read n bytes from the open file.
Parameters:
  • n (int) - Number of bytes to read. A negative number instructs read() to read all remaining bytes.
Returns:
the bytes read

readline(self, length=-1)

 
Read the next line from the file.
Parameters:
  • length (int) - a length hint, or negative if you don't care
Returns: str
the line

readlines(self, sizehint=0)

 
Read all remaining lines in the file.
Returns: list
list of lines

next(self)

 
A file object is its own iterator.
Returns: str
the next line from the file
Raises:

close(self)

 
Close the file. A no-op in this class.

flush(self)

 
Force a flush. This method throws an unconditional exception, since PushbackFile objects are read-only.
Raises:
  • NotImplementedError - unconditionally

truncate(self, size=-1)

 
Truncate the underlying file. This method throws an unconditional exception, since PushbackFile objects are read-only.
Parameters:
  • size (int) - Where to truncate. If less than 0, then file's current position is used
Raises:
  • NotImplementedError - unconditionally

tell(self)

 
Return the file's current position, if applicable. This method throws an unconditional exception, since PushbackFile objects are read-only.
Returns: int
Current file position
Raises:
  • NotImplementedError - unconditionally

seek(self, offset, whence=os.SEEK_SET)

 
Set the file's current position. This method throws an unconditional exception, since PushbackFile objects are not seekable.
Parameters:
  • offset (int) - where to seek
  • whence (int) - see above
Raises:
  • NotImplementedError - unconditionally

fileno(self)

 
Return the integer file descriptor used by the underlying file.
Returns: int
the file descriptor