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

Class AutoFlush

object --+
         |
        AutoFlush

An AutoFlush wraps a file-like object and flushes the output (via a call to flush() after every write operation. Here's how to use an AutoFlush object to force standard output to flush after every write:

import sys
from grizzled.io import AutoFlush

sys.stdout = AutoFlush(sys.stdout)
Instance Methods
 
__init__(self, f)
Create a new AutoFlush object to wrap a file-like object.
 
write(self, buf)
Write the specified buffer to 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 AutoFlush 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.
Parameters:
  • buf (str or bytes) - buffer to write

truncate(self, size=-1)

 
Truncate the underlying file. Might fail.
Parameters:
  • size (int) - Where to truncate. If less than 0, then file's current position is used.

tell(self)

 
Return the file's current position, if applicable.
Returns: int
Current file position

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

 

Set the file's current position. The whence argument is optional; legal values are:

  • os.SEEK_SET or 0: absolute file positioning (default)
  • os.SEEK_CUR or 1: seek relative to the current position
  • os.SEEK_END or 2: seek relative to the file's end

There is no return value. Note that if the file is opened for appending (mode 'a' or 'a+'), any seek() operations will be undone at the next write. If the file is only opened for writing in append mode (mode 'a'), this method is essentially a no-op, but it remains useful for files opened in append mode with reading enabled (mode 'a+'). If the file is opened in text mode (without 'b'), only offsets returned by tell() are legal. Use of other offsets causes undefined behavior.

Note that not all file objects are seekable.

Parameters:
  • offset (int) - where to seek
  • whence (int) - see above

fileno(self)

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