Intercept and Tag STDOUT values

Discussion in 'Python' started by Wes S., Oct 13, 2004.

  1. Wes S.

    Wes S. Guest

    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
    Wes S., Oct 13, 2004
    #1
    1. Advertising

  2. Wes S.

    Wes S. Guest

    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
    Wes S., Oct 13, 2004
    #2
    1. Advertising

  3. On Wed, 13 Oct 2004 04:06:54 GMT, Wes S.
    <> 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.
    Andrew Durdin, Oct 13, 2004
    #3
  4. 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.
    Andrew Durdin, Oct 13, 2004
    #4
  5. Wes S.

    Wes S. Guest

    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" <> wrote in message
    news:...
    > 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.
    Wes S., Oct 13, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Susan van Houen

    autopostback - javascript intercept and cancel

    Susan van Houen, Jul 17, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,600
    Marina
    Jul 17, 2003
  2. shruds
    Replies:
    1
    Views:
    779
    John C. Bollinger
    Jan 27, 2006
  3. Elad
    Replies:
    0
    Views:
    410
  4. Andreas S
    Replies:
    3
    Views:
    261
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    330
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page