From: Anthony Papillion on 22 Jun 2010 02:10 I'm writing an application that uses the Google Storage Python library. When an error occurs, the error is printed on the terminal. What I need to do is intercept that text into a variable so I can run a re.search() against it and find out what's going on. I thought doing a output_text = method_name(parameters) would stuff the output in the output_text variable but it doesn't. How can I accomplish this?
From: Paul Rubin on 22 Jun 2010 02:30 Anthony Papillion <papillion(a)gmail.com> writes: > I'm writing an application that uses the Google Storage Python > library. When an error occurs, the error is printed on the terminal. > What I need to do is intercept that text into a variable so I can run > a re.search() against it and find out what's going on. I'm unfamiliar with that library, but if you mean it raises an exception, the thing to do is catch the exception and examine the response code that it should supply. Actually parsing the text error message is horrendous (think of internationalization, for example). Though it is sometimes necessary, if the code throwing the exception is well designed, it shouldn't require parsing the text. See the python docs about the try/except statement to learn about exceptions in Python.
From: James Mills on 22 Jun 2010 02:51 On Tue, Jun 22, 2010 at 4:10 PM, Anthony Papillion <papillion(a)gmail.com> wrote: > I'm writing an application that uses the Google Storage Python > library. Â When an error occurs, the error is printed on the terminal.. > What I need to do is intercept that text into a variable so I can run > a re.search() against it and find out what's going on. I'm not familiar with this library (either), however you would be better off digging through it's documentation and/or it's sources and find out how to change where it logs errors to. --James -- -- -- "Problems are solved by method"
From: Steven D'Aprano on 22 Jun 2010 03:19 On Mon, 21 Jun 2010 23:10:56 -0700, Anthony Papillion wrote: > I'm writing an application that uses the Google Storage Python library. > When an error occurs, the error is printed on the terminal. What I need > to do is intercept that text into a variable so I can run a re.search() > against it and find out what's going on. If the error is *only* printed to stdout (or stderr), then you can do this to capture it: >>> import sys >>> from StringIO import StringIO >>> capture = StringIO() >>> save_stdout = sys.stdout >>> sys.stdout = capture >>> print "hello world" >>> sys.stdout = save_stdout >>> print capture.getvalue() hello world >>> Don't forget to restore stdout, or you'll have no end of grief later. But if the error is a proper exception, and you're talking about the traceback, then wrap the call in a try...except block: try: method(args) except Exception, e: do_something_with(e) -- Steven
|
Pages: 1 Prev: Problem using pbzip2 with bz2.BZ2File().read() Next: Should I Learn Python or Ruby next? |