Redirecting STDOUT to a Python Variable

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Sep 27 13:09:05 EDT 2012


Hans Mulder wrote:
> On 22/09/12 23:57:52, ross.marsden at gmail.com wrote:
> > To capture the traceback, so to put it in a log, I use this
> >
> > import traceback
> >
> > def get_traceback(): # obtain and return the traceback
> >     exc_type, exc_value, exc_traceback = sys.exc_info()
> >     return ''.join(traceback.format_exception(exc_type, exc_value,
> exc_traceback))
> 
> This could be coded more succinctly as
> 
> import sys, traceback
> 
> def get_traceback(): # obtain and return the traceback
>     return ''.join(traceback.format_exception(*sys.exc_info()))

Why not just use?

      return traceback.format_exc()

> 
> > Suppose I have a script run by the scheduler, this captures the traceback
> form any problems and emails them.
> >
> > if __name__ == '__main__':
> >     try:
> >         Runs_unattended()
> >     except:
> >         send_mail(send_from = yourEmailAddress,
> >               send_to = [ yourEmailAddress ], subject = 'Runs_unattended',
> >               text = '%s' % get_traceback(),
> >               files = [], server=yourLocalSMTP)
> 
> Errhm, '%s' % get_traceback() is equiavalent to get_traceback()
> How about:
> 
> if __name__ == '__main__':
>     try:
>         Runs_unattended()
>     except:
>         send_mail(send_from = yourEmailAddress,
>               send_to = [ yourEmailAddress ],
>               subject = 'Runs_unattended',
>               text = get_traceback(),
>               files = [],
>               server=yourLocalSMTP)
> 

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list