[IPython-user] Modifying stdout

Dan Yamins dyamins@gmail....
Mon Feb 9 16:45:12 CST 2009


On Mon, Feb 9, 2009 at 10:40 AM, Laurent Dufréchou <
laurent.dufrechou@gmail.com> wrote:

>  Hi Dan,
>
>
>
> From what I've seen of ipython code, seems right for me, so the idea is to
> keep sending to sys.stdout  because ipython redefine it.
>
> So, the only problem I see with your code is that you use sys.__stdout__
> that always return the python initial stdout and not the one ipython use.
>
> So on your code you where sending your log to the original stdout without
> passing by ipython function, that's why you get the messy output.
>
>
>
> Have you tried something like (not tested yet):
>
> ipython_sys_stdout = sys.stdout
>
> sys.stdout = multicaster(,'LogFile.txt', ipython_sys_stdout)
>
> ?
>


Laurent, thanks for your message.  Unfortunately I think this suggestion
doesn't work.  (I did try it, and the same result occurs  -- that is,
pressing the "up" key gives \[[A, etc....).   I also tried using slightly
more sophisticated multicaster definition (code included below) which
catches all attributes of the old sys.stdout and (except for the "write"
method) directs to the to the old one.  Even using this, I sitll get the
_same_ resulting weird behavior.

I think that actually sys.stdout and sys.__stdout__ are the same in an
ipython session, and that something else is going on.   (But i'm not sure
what.)



class multicaster():
    def __init__(self,filename,OldObject,New=False):
        self.file = filename
        self.old = OldObject

        if New:
            F = open(filename,'w')

F.write('\n\n------------------------------------------------------------------------------------------------------------------------------------------------------\n')
            F.write('STARTING LOG: ' + time.strftime('%c %Z') + '\n')

F.write('------------------------------------------------------------------------------------------------------------------------------------------------------\n\n')
            F.close()

    def __getattr__(self,name):
        if name != 'write':
            return self.old.__getattribute__(name)

    def write(self,s):
        F = open(self.file,'a')
        F.write(s)
        F.close()
        return self.old.write(s)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ipython.scipy.org/pipermail/ipython-user/attachments/20090209/b5c239e2/attachment.html 


More information about the IPython-user mailing list