Andrew Channels Dexter Pinion

Wherein I write some stuff that you may like to read. Or not, its up to you really.

February 27, 2006

Wither PyCon 2006 on the Web?

Pycon 2006 has been going on for the past couple of days in Texas (,USA for those who insist that place names have two parts) and traces on the web have been rather thin.

In previous years I have followed the proceedings from afar via blog posts and other assorted web pages. Apart from this post by AMK stating that it's all over and has gone well I haven't seen a lot of information over the last few days. Have I been looking in the wrong places? Or was it really not that interesting and didn't need reporting? Or, perish the thought, is blogging so 2002 that no one is posting anything about this year's conference?

Posted by Andy Todd at 10:25 PM | Comments (8)

February 16, 2006

Returning Database Rows as Dicts

A recent poster on the Python DB-Sig mailing list wanted to refer to the individual elements of his fetched rows by name.

By definition this isn't possible with the Python DB-API 2.0 as it specifies that query results be returned as sequences, which most module writer interpret as tuples.

A couple of the usual smart and erudite members of the list provided ways to do this though, and I'm putting them here mainly as a reminder to myself. First, Andy Dustman suggested;

>>> import MySQLdb
>>> db=MySQLdb.connect(db="mysql",read_default_file="~/.my.cnf")
>>> c=db.cursor()
>>> c.execute("select * from user")
6L
>>> for row in c.fetchall():
...     d = dict( [ (c.description[i][0], j) for i,j in enumerate(row) ] )
...     print d
...
{'Drop_priv': 'Y', 'Execute_priv': 'Y', 'Create_routine_priv': 'Y',
'Repl_client_priv': 'Y', 'Create_user_priv': 'Y', 'Create_priv': 'Y',
'References_priv': 'Y', 'max_user_connections': 0L, 'Shutdown_priv':
'Y', 'Grant_priv': 'Y', 'max_updates': 0L, 'max_connections': 0L,
'Show_db_priv': 'Y', 'Reload_priv': 'Y', 'Super_priv': 'Y', 'Host':
'localhost', 'User': 'root', 'Alter_priv': 'Y', 'ssl_cipher':
array('c'), 'Password': 'xxx', 'Delete_priv': 'Y', 'Repl_slave_priv':
'Y', 'Insert_priv': 'Y', 'x509_subject': array('c'), 'ssl_type': '',
'Index_priv': 'Y', 'Create_tmp_table_priv': 'Y', 'x509_issuer':
array('c'), 'Create_view_priv': 'Y', 'Select_priv': 'Y',
'Show_view_priv': 'Y', 'Update_priv': 'Y', 'Lock_tables_priv': 'Y',
'Process_priv': 'Y', 'Alter_routine_priv': 'Y', 'File_priv': 'Y',
'max_questions': 0L}
...

Then Carsten Haese, along much the same lines but perhaps slightly more succinctly, suggested;

class RowObject(object):
  def __init__(self, data, description):
    self.__dict__.update(dict(zip([col[0] for col in description], data)))  

for rowdata in cursor.fetchall():
row = RowObject(rowdata, cursor.description)
# now you can do stuff with row.PID, row.FIRSTNAME, or however the columns
# in the query are named.

Of course a little light Googling shows me that there's nothing new in the world and that this topic is covered quite well in the Python Cookbook, with a version of the code above provided in this recipe and an alternative version using Greg Stein's dtuple module in this recipe. Alternatively you can always use db_row.py.

Posted by Andy Todd at 10:36 AM | Comments (7)

February 15, 2006

Different Wiki

I've given up on hosting my own wiki because dealing with the spam was too much effort. But I've not given up on wikis in general. I've been trying out TiddlyWiki for personal notes and now I've got my own PBWiki.

First impressions are only good, and as I actually add content I'll probably start linking to it from here. Wish me luck.

Posted by Andy Todd at 06:31 PM | Comments (0)