Skip to content

Instantly share code, notes, and snippets.

@batok
Created January 21, 2010 02:04
Show Gist options
  • Save batok/282514 to your computer and use it in GitHub Desktop.
Save batok/282514 to your computer and use it in GitHub Desktop.
This idea can be used in turbogears
#This can be useful in controller methods of turbogears when you don't have a Table object defined
#if you know a query returns one tuple ( row ) this is useful
#assuming you have a globlal sqlalchemy or connection called conn2 and that you have python 2.6
from collections import namedtuple
@expose("bla.bla.template")
def mycontrollermethod(self)
fields = "username, real_name, email"
sql = "select %s from users where username = 'Whatever'" % fields
nt = namedtuple("anything here",fields)
row = nt( *conn2.execute(sql).fetchone() )
print row.username, row.real_name, row.email # this being displayed by logger using attribute like syntax
return row._asdict() # getting for free a dict from the namedtuple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment