Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Intercept and Tag STDOUT values

Reply
Thread Tools

Intercept and Tag STDOUT values

 
 
Wes S.
Guest
Posts: n/a
 
      10-13-2004
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


 
Reply With Quote
 
 
 
 
Wes S.
Guest
Posts: n/a
 
      10-13-2004
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




 
Reply With Quote
 
 
 
 
Andrew Durdin
Guest
Posts: n/a
 
      10-13-2004
On Wed, 13 Oct 2004 04:06:54 GMT, Wes S.
<(E-Mail Removed)> wrote:
>
> 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:

>>> class cStdOutRedirect(object):

.... 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
....
>>> import sys
>>> f = cStdOutRedirect()
>>> print "Hello"

REDIRECTED OUTPUT: HelloREDIRECTED OUTPUT:
>>> f.close()
>>> print "Hello"

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.
 
Reply With Quote
 
Andrew Durdin
Guest
Posts: n/a
 
      10-13-2004
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.
 
Reply With Quote
 
Wes S.
Guest
Posts: n/a
 
      10-13-2004
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

"Andrew Durdin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
autopostback - javascript intercept and cancel Susan van Houen ASP .Net 2 07-17-2003 08:15 PM



Advertisments