Re: Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)
Ron Garret schreef: But this topic does bring up a legitimate question: I have a bunch of code that generates HTML using PRINT statements. I need to convert all this code to return strings rather than actually printing them (so I can use the results to populate templates). In Lisp I could do this: (with-output-to-string (s) (let ( (*standard-output* s) ) (call-html-generating-code) s)) Is there an equivalent Python trick to capture a function call's output as a string? Something like this: py import cStringIO py import sys py py def foo(): ... print test ... py f = cStringIO.StringIO() py sys.stdout = f py foo() py s = f.getvalue() py sys.stdout = sys.__stdout__ py f.close() py print s.capitalize() Test -- JanC Be strict when sending and tolerant when receiving. RFC 1958 - Architectural Principles of the Internet - section 3.9 -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)
In article [EMAIL PROTECTED], JanC [EMAIL PROTECTED] wrote: Something like this: py import cStringIO py import sys py py def foo(): ... print test ... py f = cStringIO.StringIO() py sys.stdout = f py foo() py s = f.getvalue() py sys.stdout = sys.__stdout__ You should always save stdout instead of using __stdout__. It may not be the same! I don't think anyone should *ever* use __stdout__ except when debugging. See also: http://mail.python.org/pipermail/python-dev/2000-October/010144.html py f.close() py print s.capitalize() Test Just -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)
Ron Garret [EMAIL PROTECTED] writes: (with-output-to-string (s) (let ( (*standard-output* s) ) (call-html-generating-code) s)) Is there an equivalent Python trick to capture a function call's output as a string? I've sometimes replaced sys.stdout (and/or sys.stderr) to capture/redirect debugging information in existing code that has unwisely just printed error and warning messages, instead of using sys.stderr or error logging modules. py def with_output_to_string (func): ... try: ... sys.stdout = StringIO.StringIO () ... func () ... return sys.stdout.getvalue () ... finally: ... sys.stdout = sys.__stdout__ ... py def printing_code (): ... print Foobar ... py with_output_to_string (printing_code) 'Foobar\n' py br, S -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)
In article [EMAIL PROTECTED], Simo Melenius [EMAIL PROTECTED] wrote: I've sometimes replaced sys.stdout (and/or sys.stderr) to capture/redirect debugging information in existing code that has unwisely just printed error and warning messages, instead of using sys.stderr or error logging modules. py def with_output_to_string (func): ... try: ... sys.stdout = StringIO.StringIO () ... func () ... return sys.stdout.getvalue () ... finally: ... sys.stdout = sys.__stdout__ Aargh, I can't believe how widespread this idiom is :-(. See my other reply in this thread: DON'T use sys.__stdout__. Ever. Just -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)
Just wrote: In article [EMAIL PROTECTED], Simo Melenius [EMAIL PROTECTED] wrote: I've sometimes replaced sys.stdout (and/or sys.stderr) to capture/redirect debugging information in existing code that has unwisely just printed error and warning messages, instead of using sys.stderr or error logging modules. py def with_output_to_string (func): ... try: ... sys.stdout = StringIO.StringIO () ... func () ... return sys.stdout.getvalue () ... finally: ... sys.stdout = sys.__stdout__ Aargh, I can't believe how widespread this idiom is :-(. See my other reply in this thread: DON'T use sys.__stdout__. Ever. Just -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)
(Not sure if my other message arrived) I am guilty of using this idiom, too. The standard library http://www.python.org/dev/doc/devel/lib/module-sys.html#l2h-396 says: __stdin__ __stdout__ __stderr__ These objects contain the original values of stdin, stderr and stdout at the start of the program. They are used during finalization, and could be useful to restore the actual files to known working file objects in case they have been overwritten with a broken object. Notice the during finalization sentence. Maybe you should change the doc and explain what __stdout__ is intended for? Michele Simionato -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)
In article [EMAIL PROTECTED], Just [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED], Simo Melenius [EMAIL PROTECTED] wrote: I've sometimes replaced sys.stdout (and/or sys.stderr) to capture/redirect debugging information in existing code that has unwisely just printed error and warning messages, instead of using sys.stderr or error logging modules. py def with_output_to_string (func): ... try: ... sys.stdout = StringIO.StringIO () ... func () ... return sys.stdout.getvalue () ... finally: ... sys.stdout = sys.__stdout__ Aargh, I can't believe how widespread this idiom is :-(. See my other reply in this thread: DON'T use sys.__stdout__. Ever. It's helpful to provide an explanation when saying things like this. In this case, it's best to save the original value of sys.stdout and restore that, otherwise nested calls to with_output_to_string can fail, e.g. def f(): print 123 def g(): print 456 x = with_output_to_string(f) print 789 with_outupt_to_string(g) # Will miss the 789 rg -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)
Just schreef: You should always save stdout instead of using __stdout__. It may not be the same! You're right, especially when this code would execute in an (at programming time) unknown context. -- JanC Be strict when sending and tolerant when receiving. RFC 1958 - Architectural Principles of the Internet - section 3.9 -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)
Just [EMAIL PROTECTED] writes: In article [EMAIL PROTECTED], Simo Melenius [EMAIL PROTECTED] wrote: ... sys.stdout = sys.__stdout__ Aargh, I can't believe how widespread this idiom is :-(. See my other reply in this thread: DON'T use sys.__stdout__. Ever. It probably does the right thing, if someone is copypasting code off the Usenet and tries it out at the Python prompt (and possibly gets stuck in getting nothing printed on his tty). If we're talking about real code it might be advisable to start by first introducing the import statements omitted from the above snippet, then worry about someone copypasting code into a serious program instead of copythinkpasting it. :) br, S -- http://mail.python.org/mailman/listinfo/python-list
Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)
In article [EMAIL PROTECTED], [EMAIL PROTECTED] (Aahz) wrote: In article [EMAIL PROTECTED], Roy Smith [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED], Ron Garret [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED], Erik Bethke [EMAIL PROTECTED] wrote: I have NEVER experienced this kind of programming joy. Just wait until you discover Lisp! Taking this more seriously than it deserves, I've tried poking at Lisp a couple of times -- each time, I walk away shaking my head in disgust. Lisp just ain't as *READABLE* as Python. Readability is in the eye of the beholder, but this is not the place to argue this. But this topic does bring up a legitimate question: I have a bunch of code that generates HTML using PRINT statements. I need to convert all this code to return strings rather than actually printing them (so I can use the results to populate templates). In Lisp I could do this: (with-output-to-string (s) (let ( (*standard-output* s) ) (call-html-generating-code) s)) Is there an equivalent Python trick to capture a function call's output as a string? Thanks, rg -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout
Ron Garret wrote: But this topic does bring up a legitimate question: I have a bunch of code that generates HTML using PRINT statements. I need to convert all this code to return strings rather than actually printing them (so I can use the results to populate templates). In Lisp I could do this: (with-output-to-string (s) (let ( (*standard-output* s) ) (call-html-generating-code) s)) Is there an equivalent Python trick to capture a function call's output as a string? Just to make sure I understand, I'm going to restate your question: Is there a way to capture stdout? The answer: Sure, replace it with something file-like: import sys, StringIO default = sys.stdout writer = StringIO.StringIO() sys.stdout = writer print 'Whatever' sys.stdout = default print writer.getvalue() Whatever // m -- http://mail.python.org/mailman/listinfo/python-list
Re: Rebinding stdout
In article [EMAIL PROTECTED], Mark McEahern [EMAIL PROTECTED] wrote: Ron Garret wrote: But this topic does bring up a legitimate question: I have a bunch of code that generates HTML using PRINT statements. I need to convert all this code to return strings rather than actually printing them (so I can use the results to populate templates). In Lisp I could do this: (with-output-to-string (s) (let ( (*standard-output* s) ) (call-html-generating-code) s)) Is there an equivalent Python trick to capture a function call's output as a string? Just to make sure I understand, I'm going to restate your question: Is there a way to capture stdout? The answer: Sure, replace it with something file-like: import sys, StringIO default = sys.stdout writer = StringIO.StringIO() sys.stdout = writer print 'Whatever' sys.stdout = default print writer.getvalue() Whatever // m That's exactly what I was looking for. Thanks! rg -- http://mail.python.org/mailman/listinfo/python-list