Intercept and Tag STDOUT values

W

Wes S.

How can I redirect and tag sys.stdout values. I tried creating a class
module that saved the old stdout and replaced it, but I can't seem to figure
out how to do such.

Here is what I got...

class cStdOutRedirect :
stdOld = ""
def __main__(self) :
stdOld = sys.stdout
sys.stdout = self
return True
def write(self,str) :
stdOld.write("REDIRECTED OUTPUT: " + str)
return True
def close(self) :
sys.stdout = stdOld
return True

then in the main code...

c = cStdOutRedirect()
print "hello world"
c.close()

I'm in a high school Python class and this is well beyond where the
instructor is now and he isn't too fond of answering advanced questions. I
have programmed in quite a few other languages, so I understand quite a bit
of Python.

-Wes
 
W

Wes S.

Ok, so I've solved one piece of the puzzle, now the class itself works
(errors...sad...). Nevertheless, it's not actually replacing the sys.stdout
like I have intended all along...

class cStdOutRedirect :
stdOld = sys.stdout
def __main__(self) :
self.stdOld = sys.stdout
sys.stdout = self
return True
def write(self,str) :
self.stdOld.write("REDIRECTED OUTPUT: " + str)
return True
def close(self) :
sys.stdout = self.stdOld
return True
 
A

Andrew Durdin

class cStdOutRedirect :
stdOld = sys.stdout

You don't need this line -- that would try and save stdout when the
class is created, not when an instance is created.
def __main__(self) :

I believe you mean __init__, not __main__ (which is not a meaningful
method name)
self.stdOld = sys.stdout
sys.stdout = self
return True

Any return value from __init__ is never used, so there's no point having one.
def write(self,str) :

It's probably not a good idea to name a variable "str", as that will
override the builtin str() function.
self.stdOld.write("REDIRECTED OUTPUT: " + str)
return True

The write() function is not expected to return a value, so again omit it.
def close(self) :
sys.stdout = self.stdOld
return True

Ditto regarding return value for close().

With these changes, here's an example:
.... def __init__(self):
.... self.stdOld = sys.stdout
.... sys.stdout = self
.... def write(self, s):
.... self.stdOld.write("REDIRECTED OUTPUT: " + s)
.... def close(self):
.... sys.stdout = self.stdOld
....Hello

This shows a couple of pitfalls: first, you might not want to have
stdout redirected as soon as the class is instantiated. Secondly, the
tag is put on every write() call, not just at the start of lines.
Thirdly, this may not work nicely if you try to redirect more than
once. Finally, you might need to override another few methods for
better compatibility (e.g. flush()).
I would also suggest renaming "close" to "end_redirect" or something,
as calling
f = cStdOutRedirect()
sys.stdout.close()
comes across strangely.
 
A

Andrew Durdin

Further to my last reply: perhaps a better solution overall would be
to create a class (let's call it TaggedFile) that supports the
appropriate methods for file-like behaviour, and wraps any file
(keeping a reference to it in the "wrapped_file" attribute) with the
tagging you want. Then to do the actual redirection:

tagged_stdout = TaggedFile(sys.stdout)
sys.stdout = tagged_stdout
# other code here
sys.stdout = tagged_stdout.wrapped_file

It will then be much clearer that the redirection is occurring.
 
W

Wes S.

Many thanks for the first reply of help.

On this second part, I don't fully understand what you mean...I am, as I
have previously stated, pretty new to Python and have not yet learned
anything about file operations.

-Wes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top