Thursday, May 27, 2010

templating engine cookoff updated

Ian wished for loop syntax examples in the Templating Engine Cookoff, so I just added them.

And none of you Django programmers have given me code to include Django templates in the cookoff. You can't throw an exception in this town without hitting a Django programmer, so what's up? C'mon, it's easy - I just need something analogous to this:

The answer to the Ultimate
Question is ${everything['universe'].answer}.

Among our weapons are:
% for weapon in weapons:
${weapon}
% endfor

import mako
import mako.template
class MakoDemo(Demo):
name = 'mako'
version = mako.__version__
def demo(self, vars):
self.template_name = 'mako.txt'
template = mako.template.Template(filename=self.template_name)
return template.render(**vars)


[EDIT: Well done, Django folks! Django is in the cookoff now. Except... hmm... it reacts to missing variables by leaving blanks instead of throwing an exception... so I need a new trick if I want to actually see the Django error stack.]

Tuesday, May 25, 2010

dbapiext.py

Richard Jones alerted me to Martin Blais' dbapiext.py, and I like it very much.

Richard's post focuses on the improvements to bind variable handling. I love dbapiext.py for another reason: it provides query results as namedtuples, a class so perfect for database resultsets that they make me sob with relief.

>>> import cx_Oracle, dbapiext
>>> connection = cx_Oracle.connect('username/password@SID')
>>> results = dbapiext.execute_obj(connection, 'SELECT * FROM crew')
>>> row = results.next()
>>> row
Row(ID=1, NAME='Kaylee Frye', ROLE='Mechanic')
>>> row[1]
'Kaylee Frye'
>>> row.ROLE
'Mechanic'

When memory isn't an issue, I like to transform the result into a list and tack on a simple equality filter method for convenience.

class Filterable(list):
def filter(self, **kw):
for itm in self:
for k in kw:
if not (getattr(itm, k) == kw[k]):
break
else:
yield itm
yield StopIteration


>>> result = Filterable(dbapiext.execute_obj(connection, 'SELECT * FROM crew'))
>>> result.filter(NAME='Jayne Cobb').next()
Row(ID=5, NAME='Jayne Cobb', ROLE='Public Relations')

It is, of course, no SQLAlchemy, and isn't meant to be. It's meant to be a roadmap toward a less awful DB-API for Python. (I still wake up in the middle of the night, gasping, "Five incompatible bind variable formats! It's heresy!") In the meantime, dbapiext.py is a nice lightweight layer for times when SQLAlchemy is too much. For instance, I write lots of database administration tools that poke around in lots of different views of Oracle's data dictionary, and setting up SQLAlchemy mappings for each of them is a pain. dbapiext.py is perfect for that. Thanks, Martin!

Monday, May 03, 2010

Penguicon wrap-up

Thanks to the wonderful audience at my Ren'Py talk. Your patience and your questions got us through the painful time of waiting for the projector to work... bless you all! I hope you have fun with Ren'Py. I've posted my slides and sample game in the Ren'Py forum * here *.

Oh, and I saw a flashbulb during the talk... if you're reading this, could you send me a copy of the photo? I don't have a good recent photo of myself presenting. Unless it sucks, that is. :)

Some of Penguicon's other highlights, from my POV:

Adrian Crenshaw's talk on Mutillidae was delightful. I am so tempted to quit my job and become a security researcher; they have all the fun. He tipped us off to some Firefox extensions for conveniently testing websites for some common vulnerabilities, too.

Matt Arnold's talk on Inform7, which showed me wonderful tools the Inform IDE comes with. It's amazing to see how much work has gone into a language that has such a specialty purpose. I hope I'll find time to put some more work into Inform7 games. I loved this exchange:
Attendee: Is there a limit to how many [options] there can be?
Matt: No! The limit is your sanity!

The dance. I love dancing with nerds. It's so much more fun when the cool kids aren't there. I miss the good old days, though, when we danced to songs, instead of to interminable dance beats. Ah well.

During Mark Ramm's talk about implementing Sourceforge's new TurboGears/MongoDB infrastructure, a questioner invited him to complain about strict schedules for completing a job. Instead, he said that he likes demanding, inflexible deadlines; having flexibility on a project's scope is how you get things done. I thought that was a wondefully wise point of view. A flexible timetable can be an excuse for not triaging a project's scope; a firm deadline helps you make those necessary decisions and actually get something done.