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


Groups > comp.lang.python > #29767 > unrolled thread

Re: Redirecting STDOUT to a Python Variable

Started byross.marsden@gmail.com
First post2012-09-22 14:57 -0700
Last post2012-09-27 17:09 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#29767 — Re: Redirecting STDOUT to a Python Variable

Fromross.marsden@gmail.com
Date2012-09-22 14:57 -0700
SubjectRe: Redirecting STDOUT to a Python Variable
Message-ID<8cabc9bb-5ae0-41fa-86b6-0a184192e601@googlegroups.com>
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)) 


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)

[toc] | [next] | [standalone]


#29798

FromHans Mulder <hansmu@xs4all.nl>
Date2012-09-23 09:38 +0200
Message-ID<505ebc65$0$6891$e4fe514c@news2.news.xs4all.nl>
In reply to#29767
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()))

> 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)



Hope this helps,

-- HansM

[toc] | [prev] | [next] | [standalone]


#30311

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-09-27 17:09 +0000
Message-ID<mailman.1501.1348765906.27098.python-list@python.org>
In reply to#29798
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.  

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web