Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; '[],': 0.07; '__name__': 0.07; 'completeness': 0.07; 'except:': 0.07; 'suppose': 0.07; 'sys,': 0.07; 'try:': 0.07; 'received:155': 0.09; 'def': 0.10; "'%s'": 0.16; "'__main__':": 0.16; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'exc_type,': 0.16; 'exc_value,': 0.16; 'from:addr:jpmorgan.com': 0.16; 'received:155.180': 0.16; 'received:159.53': 0.16; 'received:159.53.110': 0.16; 'received:159.53.110.174': 0.16; 'received:bankone.net': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'received:sj2.jpmchase.com': 0.16; 'received:svr.bankone.net': 0.16; 'securities,': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'wrote:': 0.17; 'to:name:python-list@python.org': 0.20; 'import': 0.21; "skip:' 40": 0.22; 'received:169.254': 0.24; 'script': 0.24; 'header:In- Reply-To:1': 0.25; 'accuracy': 0.27; 'run': 0.28; 'coded': 0.29; "skip:' 50": 0.29; 'use?': 0.29; 'received:169': 0.29; "skip:' 10": 0.30; 'header:Received:8': 0.30; 'could': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'text': 0.34; 'charset:us- ascii': 0.36; 'problems': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'email addr:gmail.com': 0.63; 'information,': 0.63; 'url:email': 0.63; 'more': 0.63; 'legal': 0.65; 'subject': 0.66; 'purchase': 0.67; 'sale': 0.76; 'captures': 0.84; 'received:169.254.8': 0.84 X-DKIM: OpenDKIM Filter v2.1.3 sj2.jpmchase.com q8RH9PA8031349 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1348765766; bh=kFLshUTy+0JjQ1FOKXu8p7l7yvQjx0Jzw2inF6x7hKo=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Transfer-Encoding:MIME-Version:Content-Type; b=em1r/MMGjipBEwwVsvsqsfrBwGPUZuR4ULRdlJvl8Zag9mrb943RJT2oAfHYtZKTC S+bTZ/UBhtavxfU+etoxOjvwdtFHrsAzb9JsagaCWaHQMv0W7Za8SzXr+Z3ydLkV/q yKDhTDdGJKvEGBAKyA06JYwwpp17idiz/dqxc4f0= From: "Prasad, Ramit" To: "python-list@python.org" Subject: RE: Redirecting STDOUT to a Python Variable Thread-Topic: Redirecting STDOUT to a Python Variable Thread-Index: AQHNmQ2pBN1iJzn9x0Se6I1X13ZmY5eXzbiAgAalRPA= Date: Thu, 27 Sep 2012 17:09:05 +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> In-Reply-To: <505ebc65$0$6891$e4fe514c@news2.news.xs4all.nl> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.67.79.47] Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-DLP-FWD: Yes Content-Type: text/plain; charset="us-ascii" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348765906 news.xs4all.nl 6900 [2001:888:2000:d::a6]:42296 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30311 Hans Mulder wrote:=0D=0A> On 22/09/12 23:57:52, ross=2Emarsden@gmail=2Ecom = wrote:=0D=0A> > To capture the traceback, so to put it in a log, I use this= =0D=0A> >=0D=0A> > import traceback=0D=0A> >=0D=0A> > def get_traceback(): = # obtain and return the traceback=0D=0A> > exc_type, exc_value, exc_tra= ceback =3D sys=2Eexc_info()=0D=0A> > return ''=2Ejoin(traceback=2Eforma= t_exception(exc_type, exc_value,=0D=0A> exc_traceback))=0D=0A> =0D=0A> This= could be coded more succinctly as=0D=0A> =0D=0A> import sys, traceback=0D= =0A> =0D=0A> def get_traceback(): # obtain and return the traceback=0D=0A> = return ''=2Ejoin(traceback=2Eformat_exception(*sys=2Eexc_info()))=0D=0A= =0D=0AWhy not just use?=0D=0A=0D=0A return traceback=2Eformat_exc()=0D= =0A=0D=0A> =0D=0A> > Suppose I have a script run by the scheduler, this cap= tures the traceback=0D=0A> form any problems and emails them=2E=0D=0A> >=0D= =0A> > if __name__ =3D=3D '__main__':=0D=0A> > try:=0D=0A> > Ru= ns_unattended()=0D=0A> > except:=0D=0A> > send_mail(send_from = =3D yourEmailAddress,=0D=0A> > send_to =3D [ yourEmailAddress= ], subject =3D 'Runs_unattended',=0D=0A> > text =3D '%s' % g= et_traceback(),=0D=0A> > files =3D [], server=3DyourLocalSMTP= )=0D=0A> =0D=0A> Errhm, '%s' % get_traceback() is equiavalent to get_traceb= ack()=0D=0A> How about:=0D=0A> =0D=0A> if __name__ =3D=3D '__main__':=0D=0A= > try:=0D=0A> Runs_unattended()=0D=0A> except:=0D=0A> = send_mail(send_from =3D yourEmailAddress,=0D=0A> send_to = =3D [ yourEmailAddress ],=0D=0A> subject =3D 'Runs_unattended= ',=0D=0A> text =3D get_traceback(),=0D=0A> file= s =3D [],=0D=0A> server=3DyourLocalSMTP)=0D=0A> =0D=0A=0D=0AT= his email is confidential and subject to important disclaimers and=0D=0Acon= ditions including on offers for the purchase or sale of=0D=0Asecurities, ac= curacy and completeness of information, viruses,=0D=0Aconfidentiality, lega= l privilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ej= pmorgan=2Ecom/pages/disclosures/email=2E