Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30311

RE: Redirecting STDOUT to a Python Variable

From "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Subject RE: Redirecting STDOUT to a Python Variable
Date 2012-09-27 17:09 +0000
References <7e934b64-ec84-48c8-a4bb-a2272f1449a4@u26g2000yqu.googlegroups.com> <4c206409$0$14147$c3e8da3@news.astraweb.com> <8cabc9bb-5ae0-41fa-86b6-0a184192e601@googlegroups.com> <505ebc65$0$6891$e4fe514c@news2.news.xs4all.nl>
Newsgroups comp.lang.python
Message-ID <mailman.1501.1348765906.27098.python-list@python.org> (permalink)

Show all headers | View raw


Hans Mulder wrote:
> On 22/09/12 23:57:52, ross.marsden@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.  

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Redirecting STDOUT to a Python Variable ross.marsden@gmail.com - 2012-09-22 14:57 -0700
  Re: Redirecting STDOUT to a Python Variable Hans Mulder <hansmu@xs4all.nl> - 2012-09-23 09:38 +0200
    RE: Redirecting STDOUT to a Python Variable "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-09-27 17:09 +0000

csiph-web