Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'executing': 0.05; 'warnings': 0.05; 'srinivas': 0.07; 'finally:': 0.09; 'received:64.4': 0.09; 'statement.': 0.09; 'tim,': 0.09; 'def': 0.12; 'error:': 0.12; 'abort': 0.16; 'conn.close()': 0.16; 'count,': 0.16; 'cursor': 0.16; 'error)': 0.16; "function's": 0.16; 'input,': 0.16; 'thru': 0.16; 'trap': 0.16; 'insert': 0.19; 'header:In-Reply-To:1': 0.21; 'file,': 0.22; 'thanks': 0.28; 'skip:_ 20': 0.28; 'import': 0.29; 'catching': 0.30; 'url:library': 0.31; 'calling': 0.31; 'skip:- 40': 0.32; 'to:addr :python-list': 0.33; 'file': 0.34; 'done.': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'try:': 0.35; 'here,': 0.35; 'charset :us-ascii': 0.36; 'log': 0.36; 'something': 0.37; 'query': 0.37; 'url:docs': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'but': 0.38; 'docs': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'skip:s 20': 0.39; "i'd": 0.39; 'to:addr:python.org': 0.39; 'following:': 0.40; 'your': 0.60; 'back': 0.63; 'valuable': 0.66; 'unclear': 0.91 Date: Thu, 16 Jun 2011 13:56:22 -0800 From: Tim Johnson To: python-list@python.org Subject: Re: Trapping MySQLdb warnings References: <20110616015837.GA1885@johnsons-web.com> <20110616023829.GB1885@johnsons-web.com> <20110616155533.GD1885@johnsons-web.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Organization: AkWebsoft User-Agent: Mutt/1.5.20 (2009-06-14) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 54 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308261381 news.xs4all.nl 49048 [::ffff:82.94.164.166]:47140 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7769 * srinivas hn [110616 11:06]: > Hi Tim, > > import warnings > > with warnings.catch_warnings(): > warnings.simplefilter('error', MySQLdb.Warning) > try: > cursor.execute(insert_query) > conn.commit() > return 'Success' > except MySQLdb.Error, error: > logging.error("Error in insertion %s query is ", error) > return 'Failure' > finally: > conn.close() > > try: > xyz = do_query(insert_query) > except MySQLdb.Warning, warning: > logging.warning(warning) Hi Again srinavas: For an overview, I need to interate thru a file, executing each line as an insert statement. Based on your valuable input, I have the following: ## ------------------------------------------------ def __process_line(self): """For each line in the file ..""" try : self.db.simple(self.read()) except MySQLdb.Warning,e: ## catching warnings here, log, count, abort ## or some other action based on implementation print(e) ## and count, log .... etc ## ------------------------------------------------ def simple(self,insert_query): """Executing one line.""" cursor = self.__rdb with warnings.catch_warnings(): warnings.simplefilter('error', MySQLdb.Warning) cursor.execute(insert_query) ## This works well, but I'd like to do something further: Control is transfered back to the calling function's `except' block when the first warning is found. There can be and are multiple warning conditions on each line. Is there a way to trap all of them? I am unclear from the docs that I am reading at http://docs.python.org/library/warnings.html whether that can be done. Thanks again -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com