Hi,<br><br>I'd like to redirect stdout for my own custom purposes while in an ipython session. To do this, I would like to issue the command:<br><br> > sys.stdout = multicaster(,'LogFile.txt', sys.__stdout__)<br>
<br>before going on do my business. The multicaster class (the code of which I provide below after this short explanation) is used to both log stdout output to a logfile ('Log.txt' in the example above) as _well_ as print to the screen. Basically when multicaster does is create a new object whose "write" method is like that of sys.__stdout__ but also includes writing to the log file. <br>
<br>When I do this at a regular python interpreter, it works fine as far as I can tell. However, in ipython it wrecks havoc on ipython's interactive before: the up, back, right, and left arrow keys produce output ('[[A' or the like) instead of their usual functions, the interactive warning after typing "quit()" fails, etc.... Before getting it to act normally, I have to issue the command:<br>
<br> > sys.stdout = sys.__stdout__<br><br>and once I do this, all is normal again (though of course I've lost my special logging feature).<br><br>It seems to me that the reason this is happening is probably that ipython has already replaced sys.stdout with an object having some special methods to achieve some of ipython's special interactive functionality, and my Multicaster class definition is overwriting those methods. (since after all I'm not subclassing anything, the object I create just has one "write" method.) So I have two questions: <br>
<br>1) Is this explanation basically right? If so, how do I properly "subclass" ipython's object to simply modify the write method without killing the other methods. <br><br>2) If this explanation is not basically right, what is the reason, and how do I get around it? <br>
<br>Thanks!<br>Dan<br><br><br>class multicaster():<br> def __init__(self,filename,OldObject,New=False):<br> self.file = filename<br> self.old = OldObject<br> if New:<br> F = open(filename,'w')<br>
F.write('\n\n------------------------------------------------------------------------------------------------------------------------------------------------------\n')<br> F.write('STARTING LOG: ' + time.strftime('%c %Z') + '\n')<br>
F.write('------------------------------------------------------------------------------------------------------------------------------------------------------\n\n')<br> F.close()<br> <br>
def write(self,s):<br> self.old.write(s)<br> F = open(self.file,'a')<br> F.write(s)<br> F.close()<br>