Wednesday, December 20, 2006

Python MySQLdb, warnings, mix-ins

I've been working on a side project that uses MySQLdb. Database work. Munging imperfect data inevitably spawns warnings and I want to supress the obviously unimportant ones.

Originally, one would call MySQLdb.connect with "cursorclass=MySQLdb.cursors.CursorNW" as an argument. (NW=No Warnings). That hasn't worked for years, though, so if you google and find that, you're SOL.

The next approach was to derive a new cursor class using mix-ins; the default cursor has a "Warning" mix-in that we simply want to exclude:

import MySQLdb.cursors
class EricsCursor(MySQLdb.cursors.CursorStoreResultMixIn,
db = MySQLdb.Connect( user=uname, passwd=pword, db=dbase, cursorclass=EricsCursor)

Unfortunately, that documentation is also out of date. The CursorWarningMixIn is no more and in fact the EricsCursor is exactly what the default cursor is, now. So that doesn't work either... still SOL.

Finally, the actual, correct way to supress these messages is with the stock python warning module. You can either turn off all warnings (which is probably a bad idea) or filter exactly those you know are incorrect out, as follows:

import warnings
Class Blah:
def supress_warnings():
# I get a warning on "DROP TABLE IF EXISTS" query,
# that the temp table (something_temp) is unknown.
warnings.filterwarnings("ignore", "Unknown table.*_temp")
supress_warnings = staticmethod(supress_warnings)

This actually works, and I can piecewise supress any warnings I know are irrelevant. I made it a static method to keep the code in the same module as the Database class, but I want to call it exactly once early in my program initialization routines.

Friday, December 15, 2006

Dick's "The Man in the High Castle"

Having fallen into a rut of reading the same author's books, I found a couple of top-(whatever) lists for fiction, and I'm working my way through them at the library. I just finished Philip K. Dick's "The Man in the High Castle." It was OK, but I was rather unsatisfied with the ending. Two of the main characters just lose it, then the whole alternate 'real' reality thing seemed to come out of nowhere.

I completely missed that Tagomi was supposed to be visiting our reality. I thought it was just being pointed out that Japanese control wasn't total.

Did Juliana actually swallow a razorblade? If so, how was she able to just continue on with the story (eating, drinking, sleeping, etc) with no ensuing problems?

Hawthorne's family's bland acceptance that Juliana had killed a man seemed too much to accept.

All in all, an OK book. But mix in a little ignorance of German and history around WWII, that this was the first PKD book I've read, and the way it ended, I can't say I'd recommend it.