473,500 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Intercept and Tag STDOUT values

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
Jul 18 '05 #1
4 1910
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


Jul 18 '05 #2
On Wed, 13 Oct 2004 04:06:54 GMT, Wes S.
<mo***************@nospamverizon.net> 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.
Jul 18 '05 #3
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.
Jul 18 '05 #4
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" <ad*****@gmail.com> wrote in message
news:ma**************************************@pyth on.org...
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.

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4123
by: zoltix | last post by:
Hi, I am beginner in JavaScript. I would like to intercept all click events on my document. I use this function for that document.onmousedown=click;. It works well. But I would like to...
10
21108
by: Michael Gaab | last post by:
If I redirect stdout by using freopen("afile", "w", stdout); and then I closed stdout using, fclose(stdout), essentially I am just closing "afile". I have to reestablish what stdout originally...
1
6101
by: Imran | last post by:
Hi, Please bear with me as I have only 1 weeks .NET experience. I am using VB.NET to write a stand-alone client application that connects to a Web service. I successfully send a request for a...
0
1334
by: zoltix | last post by:
Hi, I am beginner in aspx. I would like to intercept all click events on my document. I use this function in javascipt for that document.onmousedown=click;. It works well. But I would...
2
2805
by: Abhishek | last post by:
what are the STDUPDATE, STDERR, STDOUT and STDIN streams and how does one access these streams in C language. I am aware of the function fprintf(FILE *fp, char * format, char *s) which puts the...
5
16183
by: Dick Watson | last post by:
I'm trying to debug some code I haven't been in for years. It's in the 1and1 linux hosting environment. Under PHP 4.4.8 (cgi) (built: Mar 6 2008 18:09:06) This code <?php fwrite(STDOUT,...
7
4674
by: ADN | last post by:
Hi, I am creating a custom HTTPModule to intercept the request of when the user is attempting to retrieve a session variable. For instance, if I set a session variable in my code like so: ...
1
3301
by: n00b | last post by:
greetings, i would like to redirect stdout/err to a mysql table and would like a) some peer review and b) suggestions for hardening the approach for a general purpose class. thank you very much....
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.