Project Chanology

The Project Chanology phenomenon is pretty interesting on a number of levels.  While primarily being the never-ending source of lulz that Anonymous never fails to deliver, it shows itself as being something potentially much greater.The organization of the Protest/’war’ on the Church of Scientology appears to be a non-centrally directed protest movement.  I don’t have the time to setup the Tor and irc bounce proxies needed to safely join the IRC channels, but everything on the web points to Anonymous truly functioning as a collective.The planned 2/10/2008 street protests will either definitely take it to a new level, or in their potential failure, will turn this fascinating phenomenon into just another Internet joke, attempting to get the lulz.  I’m following this story intently - I wonder if the late 200x’s will be like the 70’s, with a new generation of kids coming of age and starting to protest against a bunch of messed up crap… 

Add commentFebruary 1st, 2008

Little Box of Fun

Mary has started a new site to keep up with our burgeoning aquatic ecosystem - ‘www.littleboxoffun.com‘.  She’s got much more recent pictures, and bits about some of the different creatures we’re currently keeping in it.

Add commentJanuary 15th, 2008

Python MySQLdb snippet

So, for the past few weeks, I’ve been hacking around with Python’s MySQLdb DBAPI v2 driver. After getting so used to mysqli in PHP, the move to MySQLdb was a mixed bag - some things are awesome, others not so much.

First the awesome - it’s so much cleaner to do safe, simple queries with python’s dbapi, than with the PHP mysqli stuff. Just call cursor.execute(query, arguments) and pass in your SQL and it’s arguments, and all the escaping and safety stuff is taken care of behind the scenes. No ugly hacks to check to see if something is a digit or a string and to properly escape/format the final query for you. In addition to that, connecting and setting the character set and forcing the driver to use unicode is all just 2 little options passed in the connect() method.

Now, the not so awesome - I really liked mysqli_fetch_assoc() for returning data from the database in a easy to use form - an associate array (in case it wasn’t obvious!). DBAPI drivers for python do not provide such a feature out of the box - your data comes back as a tuple, rather than a dict. While this makes some sense, the lack of a way to get a dict back seems to be rather un-pythonic. It violates the ‘Explicit is better than implicit’ and ‘Beautiful is better than ugly’ Zen of Python aphorisms. It requires implicit accessing of your data, as you must use tuple slices/subscripts instead of keywords to the dict, and that requires ugly numbers dropped into the otherwise easily readable code.

In an effort to save myself some trouble, here’s a quick way to emulate the fetch_assoc functionality, built in to a generator function so it can be used easily in ‘for’ statements and the like:

#!/usr/bin/env python
# Requires a version of python that supports generator functions (2.3+)
import  MySQLdb

connection = MySQLdb.connect(host="localhost", user="username", passwd="passwd", db="myInstance", use_unicode=True, charset="utf8")
cursor = connection.cursor()
def query(query, args):
        numRows = cursor.execute(query, args)
        tries = 0
        dbKeys = cursor.description
        row = {}
        while tries < numRows:
                tries = tries + 1
                dbRow = cursor.fetchone()
                for item, key in zip(dbRow, dbKeys):
                        row[key[0]] = item
                yield row

sql = """SELECT foo, bar FROM baz WHERE id = %(id)s"""
args = {'id':'adam'}

for rows in query(sql, args):
        print rows

That outputs:

	{'foo': u'fooValue', 'bar':'barValue'}
	{'foo': u'fooValue2', 'bar':'barValue2'}

3 commentsJanuary 15th, 2008

Happy new years!


Happy new years!

Add commentJanuary 1st, 2008

The upgrade treadmill

Sometimes I wish Wordpress would stop releasing security fixes.  Or at least release a differential patch for an upgrade, instead of a ‘carefully move all your hacks and changes elsewhere and then copy them back one by one’ kind of thing :( 

Add commentDecember 31st, 2007

Next Posts Previous Posts


Categories

Links

Recent Posts

Meta