View Full Version : ok, i think it's done...
inkedmn
06-19-2002, 04:47 AM
i still have tiny bit of (mostly cosmetic) stuff to do, but this is about as functional as it's going to get...
the mysql wrapper class... (http://inkedmn.net/code/new_abook/mysqldblib.py)
enjoy :)
and please, comment away...
Strike
06-19-2002, 10:23 AM
There's generally no reason to have a pass in a finished implementation of anything. You should probably do something like throw a custom exception there (in the __init__ function) or something.
Also, if you want to try your hand at list comprehensions, you can change your tupleToList function to a one liner:
def tupleToList(self, tuple):
return [item for item in tuple]
This, in my opinion, doesn't sacrifice readability much at all, since the name of the function itself details what the list comprehension does. I also think that list comprehensions are optimized to be faster than loop sequences like the one you have.
Lastly, and I mentioned this to you on IRC, you might have some status check on if the DB connection is still alive or not. I don't know if MySQLdb always keeps persistent connections or not, but you should probably include some sort of mechanism for checking connection status. If the connection has gone sour, you can either: a) try to connect again and resume as if nothing had happened, or b) throw a custom exception.
inkedmn
06-19-2002, 12:22 PM
ok...
took out the pass statement, here's the updated code:
if database != '':
try:
self.cursor.execute("use %s" % self.database)
except Exception, e:
print "Error: ", e
also took care of the tupleToList method...
i guess i'm not quite sure how i would approach the persistent connection thing. i'll look around and see what i can come up with...
thanks for the feedback!!
jemfinch
06-23-2002, 05:39 PM
I haven't read the code (I'm on a Windows machine so the linebreaks are all messed up) so I'm just going off what I get from other people here...
Anyway, your "tupleToList" function shouldn't be iterating through the tuple at all. Just call "list(tuple)" and that will convert it to a list. It's sure to be the fastest way to so such a thing.
And I hate list comprehensions. They're an ugliness added to the syntax to cover a problem that doesn't exist anymore now that we have nested scopes. At the very least you'll never find them in my code.
That code fragment you're using is doing something very wrong -- you shouldn't be catching that exception at all. If you don't know how to handle an exception (that is, if you're just going to catch it and print it for lack of something better to do) then you shouldn't catch it at all. That's what's great about exceptions -- they propogate to client code that may know better how to handle them.
For instance, what if I use your code in an IRC bot that doesn't print anything to stdout, but logs all errors to a certain file? Then I'm up a creek if I want to find out what in the world happened inside that function because you caught the error and printed it. Take out the try clause entirely and let the client handle the exception, and there is no such problem.
Jeremy
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.