grizzled.io module
Input/Output utility methods and classes.
Classes
class 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)
Ancestors (in MRO)
- AutoFlush
- builtins.object
Static methods
def __init__(
self, f)
Create a new AutoFlush
object to wrap a file-like object.
Parameters
f
(file-like object): A file-like object that contains both awrite()
method and aflush()
method.
def fileno(
self)
Return the integer file descriptor used by the underlying file.
def flush(
self)
Force a flush.
def seek(
self, offset, whence=0)
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 positionos.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.
def tell(
self)
Return the file's current position, if applicable.
def 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.
def write(
self, buf)
Write the specified buffer to the file.
Parameters
buf
(str
orbytes
): buffer to write
class MultiWriter
Wraps multiple file-like objects so that they all may be written at once.
For example, the following code arranges to have anything written to
sys.stdout
go to sys.stdout
and to a temporary file:
import sys from grizzled.io import MultiWriter sys.stdout = MultiWriter(sys.__stdout__, open('/tmp/log', 'w'))
Ancestors (in MRO)
- MultiWriter
- builtins.object
Static methods
def __init__(
self, *args)
Create a new MultiWriter
object to wrap one or more file-like
objects.
Parameters
args
(iterable): One or more file-like objects to wrap
def close(
self)
Close all contained files.
def flush(
self)
Force a flush.
def write(
self, buf)
Write the specified buffer to the wrapped files.
Parameters
buf
(str
or bytes): buffer to write
class PushbackFile
A file-like wrapper object that permits pushback.
Ancestors (in MRO)
- PushbackFile
- builtins.object
Static methods
def __init__(
self, f)
Create a new PushbackFile
object to wrap a file-like object.
Parameters
f
(file-like object): A file-like object that contains both awrite()
method and aflush()
method.
def close(
self)
Close the file. A no-op in this class.
def fileno(
self)
Return the integer file descriptor used by the underlying file. This method always returns -1.
def flush(
self)
Force a flush. This method throws an unconditional exception, since
PushbackFile
objects are read-only.
Raises
NotImplementedError
: unconditionally
def 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
def read(
self, n=-1)
Read n bytes from the open file as a string.
Parameters
n
(int
): Number of bytes to read. A negative number instructsread()
to read all remaining bytes.
Returns
the bytes read, joined into a string
def readline(
self)
Read the next line from the file.
def readlines(
self)
Read all remaining lines in the file.
def seek(
self, offset, whence=0)
Set the file's current position. This method throws an unconditional
exception, since PushbackFile
objects are not seekable.
Raises
NotImplementedError
: unconditionally
def tell(
self)
Return the file's current position, if applicable. This method throws
an unconditional exception, since PushbackFile
objects are
read-only.
Raises
NotImplementedError
: unconditionally
def 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
def 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
def 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
orbytes
): buffer to write
Raises
NotImplementedError
: unconditionally
Sub-modules
This module provides portable advisory file locking primitives that operate on file descriptors. POSIX-like systems and Windows systems use different primitives to perform file locking, and these different primitives are modeled by incompatible (and different) modules in the Python standard library....