Re: Trapping MySQLdb warnings

2011-06-16 Thread Tim Johnson
* Tim Johnson t...@johnsons-web.com [110615 18:53]:
 * geremy condra debat...@gmail.com [110615 18:03]:
  On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson t...@johnsons-web.com wrote:
   Using Python 2.6.5 on linux.
  
   When using MySQLdb I am getting warnings printed to stdout, but I would
   like to trap, display and log those warnings.
 . 
  Have you tried 
  http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings
   Hi Geremy:
   I just looked at the docs there. This is a new module (to me), and
   I am unsure of the implementation or whether this is what I should
   use. 
   I tried the following :
   try :
   self.__rdb.execute(S)
   except warnings.catch_warnings:
   std.mrk('',0,mysql.py:196)  ## DEBUG call 
 ## and it does not look like my code modification
 ## is catching the warning.
  Well, I am so far, absolutely baffled. I've looked at the docs for
  the `warning' module. No real examples that I can see. and why has
  the _mysql_exceptions.Warning exception no longer working?
  grrr..
  Be good to hear some other experiences here.
  thanks
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trapping MySQLdb warnings

2011-06-16 Thread Terry Reedy

On 6/16/2011 11:55 AM, Tim Johnson wrote:

* Tim Johnsont...@johnsons-web.com  [110615 18:53]:

* geremy condradebat...@gmail.com  [110615 18:03]:

On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnsont...@johnsons-web.com  wrote:

Using Python 2.6.5 on linux.

When using MySQLdb I am getting warnings printed to stdout, but I would
like to trap, display and log those warnings.

.


The machinery in the warnings module is only for instances of 
subsclasses of Warning. Are the warnings from MySQLdb properly such 
objects? If so, what class are they?



Have you tried 
http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings

   Hi Geremy:
   I just looked at the docs there. This is a new module (to me), and
   I am unsure of the implementation or whether this is what I should
   use.
   I tried the following :
try :
self.__rdb.execute(S)
except warnings.catch_warnings:


warnings.catch_warnings is a context manager, not an exception.
This is a TypeError in 3.x, which requires that exceptions be instances 
of BaseException. Try

 except warnings.Warning, warn:

Substitute specific MySQLdb warning class, whatever it is, for Warning.

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Trapping MySQLdb warnings

2011-06-16 Thread srinivas hn
Hi Tim,

Use this method it will sort tour problem.

def do_query(insert_query):
   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)


you need to use the with statement and then you need to catch the warnings
hope it helps
CHEERS
CNA
9986229891


On Thu, Jun 16, 2011 at 9:25 PM, Tim Johnson t...@johnsons-web.com wrote:

 * Tim Johnson t...@johnsons-web.com [110615 18:53]:
  * geremy condra debat...@gmail.com [110615 18:03]:
   On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson t...@johnsons-web.com
 wrote:
Using Python 2.6.5 on linux.
   
When using MySQLdb I am getting warnings printed to stdout, but I
 would
like to trap, display and log those warnings.
  .
   Have you tried
 http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings
Hi Geremy:
I just looked at the docs there. This is a new module (to me), and
I am unsure of the implementation or whether this is what I should
use.
I tried the following :
try :
self.__rdb.execute(S)
except warnings.catch_warnings:
std.mrk('',0,mysql.py:196)  ## DEBUG call
  ## and it does not look like my code modification
  ## is catching the warning.
   Well, I am so far, absolutely baffled. I've looked at the docs for
  the `warning' module. No real examples that I can see. and why has
  the _mysql_exceptions.Warning exception no longer working?
  grrr..
  Be good to hear some other experiences here.
  thanks
 --
 Tim
 tim at johnsons-web dot com or akwebsoft dot com
 http://www.akwebsoft.com
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trapping MySQLdb warnings

2011-06-16 Thread Tim Johnson
* Terry Reedy tjre...@udel.edu [110616 10:50]:
 On 6/16/2011 11:55 AM, Tim Johnson wrote:
 * Tim Johnsont...@johnsons-web.com  [110615 18:53]:
 * geremy condradebat...@gmail.com  [110615 18:03]:
 On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnsont...@johnsons-web.com  wrote:
 Using Python 2.6.5 on linux.
 
 When using MySQLdb I am getting warnings printed to stdout, but I would
 like to trap, display and log those warnings.
 .
 
 The machinery in the warnings module is only for instances of
 subsclasses of Warning. Are the warnings from MySQLdb properly such
 objects? If so, what class are they?
 The warnings are sent directly to stdout. No way that I know of to
 find out what classes they are..
 Have you tried 
 http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings
Hi Geremy:
I just looked at the docs there. This is a new module (to me), and
I am unsure of the implementation or whether this is what I should
use.
I tried the following :
 try :
 self.__rdb.execute(S)
 except warnings.catch_warnings:
 
 warnings.catch_warnings is a context manager, not an exception.
 Thanks for that.
 This is a TypeError in 3.x, which requires that exceptions be
 instances of BaseException. Try
  except warnings.Warning, warn:
 
 Substitute specific MySQLdb warning class, whatever it is, for Warning.
  OK. I'll stack 'em up and see what happens. Must look at docs
  first.
  thanks again
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trapping MySQLdb warnings

2011-06-16 Thread Tim Johnson
* Terry Reedy tjre...@udel.edu [110616 10:50]:
... 
 Substitute specific MySQLdb warning class, whatever it is, for Warning.
  Hmm! Consider the following code:
try :
  self.__rdb.execute(S)
  raise MySQLdb.Warning('danger, danger Monte Python') ## just for grins
except MySQLdb.Warning,e:
  std.debug(e,e,0,0,'mysql.py:195',0)  ## DEBUG GLOBAL
## result:
INSPECTING `e'  (F:mysql.py:195):
danger, danger Monte Python
## Question:

Am I initializing the cursor to raise warnings? I *so* rusty at this, but I
suspect that something needs to be done with either the instantiation of the
'parent' connection object or the cursor object itself.

-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trapping MySQLdb warnings

2011-06-16 Thread Terry Reedy

On 6/16/2011 3:01 PM, Tim Johnson wrote:

* Terry Reedytjre...@udel.edu  [110616 10:50]:



The machinery in the warnings module is only for instances of
subsclasses of Warning. Are the warnings from MySQLdb properly such
objects? If so, what class are they?



  The warnings are sent directly to stdout. No way that I know of to
  find out what classes they are..


The doc for any python module 'should' tell you about custom exception 
and warning classes. Srinivas' says in his post MySQLdb.Exception and 
MySQLdb.Warning, which would be sensible.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Trapping MySQLdb warnings

2011-06-16 Thread Tim Johnson
* srinivas hn hnsr...@gmail.com [110616 11:06]:
 Hi Tim,
 
 Use this method it will sort tour problem.
 
 def do_query(insert_query):
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)
 
 
 you need to use the with statement and then you need to catch the warnings
 hope it helps
  Yeah! 
  Got some tweaking to do, but will post back again on working code.
  At least I am now raising errors.
  thanks *very* much.
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trapping MySQLdb warnings

2011-06-16 Thread Tim Johnson
* srinivas hn hnsr...@gmail.com [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
-- 
http://mail.python.org/mailman/listinfo/python-list


Trapping MySQLdb warnings

2011-06-15 Thread Tim Johnson
Using Python 2.6.5 on linux.

When using MySQLdb I am getting warnings printed to stdout, but I would
like to trap, display and log those warnings.

In the past I have used _mysql_exceptions.Warning, but that approach
is not working in this case.

My cursor is created with the relevant following code:

## connection object
self.__conn = MySQLdb.connect(db = self.__db,
  host = self.__host, user = self.__user,
  passwd = self.__passwd)

## cursor object
self.__rdb = self.__conn.cursor()
## And implemented as :
try :
self.__rdb.execute(S)
except _mysql_exceptions.Warning,e:
raise e ## replace with log(e)

What else needs to be done?
TIA
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trapping MySQLdb warnings

2011-06-15 Thread geremy condra
On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson t...@johnsons-web.com wrote:
 Using Python 2.6.5 on linux.

 When using MySQLdb I am getting warnings printed to stdout, but I would
 like to trap, display and log those warnings.

 In the past I have used _mysql_exceptions.Warning, but that approach
 is not working in this case.

 My cursor is created with the relevant following code:

 ## connection object
 self.__conn = MySQLdb.connect(db = self.__db,
  host = self.__host, user = self.__user,
  passwd = self.__passwd)

 ## cursor object
 self.__rdb = self.__conn.cursor()
 ## And implemented as :
 try :
    self.__rdb.execute(S)
 except _mysql_exceptions.Warning,e:
    raise e ## replace with log(e)

 What else needs to be done?

Have you tried 
http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings
?

Geremy Condra
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trapping MySQLdb warnings

2011-06-15 Thread Tim Johnson
* geremy condra debat...@gmail.com [110615 18:03]:
 On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson t...@johnsons-web.com wrote:
  Using Python 2.6.5 on linux.
 
  When using MySQLdb I am getting warnings printed to stdout, but I would
  like to trap, display and log those warnings.
. 
 Have you tried 
 http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings
  Hi Geremy:
  I just looked at the docs there. This is a new module (to me), and
  I am unsure of the implementation or whether this is what I should
  use. 
  I tried the following :
try :
self.__rdb.execute(S)
except warnings.catch_warnings:
std.mrk('',0,mysql.py:196)  ## DEBUG call 
## and it does not look like my code modification
## is catching the warning.
Thanks for introducing me to this new module, but I'm not
sure if it is what I should be using.

BTW: End of day in this part of the world. I look forward to any other comments
on this subject tomorrow.
chees
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com
-- 
http://mail.python.org/mailman/listinfo/python-list