By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,747 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

non OO behaviour of file

P: n/a
I recently tried to create a line flushing version of sys.stdout using

class LineFlusherFile(file):
def write(self,s):
file.write(self,s)
if '\n' in s:
self.flush()

but it seems that an 'optimization' prevents the overriden write method from
being used. I had thought python was more regular than it appears to be.

Is there a better way to accomplish the intention of the above than

class LineFlusherFile:
def __init__(self,*args):
self._f = open(*args)
def __getattr__(self,a):
return getattr(self._f,a)
def write(self,s):
self._f.write(s)
if '\n' in s:
self._f.flush()

I wondered if I could make a file subclass somehow fail the PyFile_Check which
allows the optimization.
--
Robin Becker

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Robin Becker wrote:
I recently tried to create a line flushing version of sys.stdout using

class LineFlusherFile(file):
def write(self,s):
file.write(self,s)
if '\n' in s:
self.flush()

but it seems that an 'optimization' prevents the overriden write method
from being used. I had thought python was more regular than it appears
to be.

Is there a better way to accomplish the intention of the above than

class LineFlusherFile:
def __init__(self,*args):
self._f = open(*args)
def __getattr__(self,a):
return getattr(self._f,a)
def write(self,s):
self._f.write(s)
if '\n' in s:
self._f.flush()


Well, you could use python -u:

"""-u Force stdin, stdout and stderr to be totally unbuffered.
On systems where it matters, also put stdin, stdout and stderr in binary
mode. Note that there is internal buffering in xreadlines(),
readlines() and file-object iterators ("for line in sys.stdin") which
is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline()" inside a "while 1:" loop."""

Within pure Python there's not a better way that I know of. I keep a
slightly-more generalized Surrogate class around to deal with this
pattern, and then my LineFlusherFile would be a subclass of that. But
it's the same thing, really.
--
Michael Hoffman
Jul 19 '05 #2

P: n/a
Michael Hoffman wrote:
......

Well, you could use python -u:

unfortunately this is in a detached process and I am just reopening stdout
as an ordinary file so another process can do tail -F on it. I imagine ther
ought to be an os dependant way to set the file as unbuffered, but can't
remember/find out what it ought to be.
"""-u Force stdin, stdout and stderr to be totally unbuffered.
On systems where it matters, also put stdin, stdout and stderr in binary
mode. Note that there is internal buffering in xreadlines(),
readlines() and file-object iterators ("for line in sys.stdin") which
is not influenced by this option. To work around this, you will want
to use "sys.stdin.readline()" inside a "while 1:" loop."""

Within pure Python there's not a better way that I know of. I keep a
slightly-more generalized Surrogate class around to deal with this
pattern, and then my LineFlusherFile would be a subclass of that. But
it's the same thing, really.

--
Robin Becker

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.