473,624 Members | 2,577 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("R EDIRECTED 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 1919
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.wri te("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************ ***@nospamveriz on.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.wri te("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.wri te("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_redire ct" or something,
as calling
f = cStdOutRedirect ()
sys.stdout.clos e()
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_fi le" 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.w rapped_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******** *************** *************** @python.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_fi le" 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.w rapped_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
4177
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 continue the execution code on a other button or anything else. Example 1) Click anywhere intercept click->Execute the code in function click().
10
21129
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 pointed to? thanks
1
6111
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 list of items (i.e. getItemList), and successfully receive the list. I then send a request for individual items from the list (i.e. getItem), and successfully receive the item.
0
1346
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 like to continue the execution code on a other button or anything else. Example 1) Click anywhere intercept click->Execute the code in function click().
2
2834
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 string into the corresponding streams. But can you please tellme where does the content go exactly when we put it into the above streams. In which cases can we see the outpt and in which cases cant we see and why so? It would be great if you can...
5
16195
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, "trythis\r\n"); ?>
7
4682
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: session = "Hello World"; When the request for that session variable "myString" is attempted to be retrieved, I would like to intercept that request like so: if (Request is for session variable "myString")
1
3315
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. import sys import MySQLdb class DBLogger(object): def __init__(self):
0
8249
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8633
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8493
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2613
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1797
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1493
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.