Created
January 21, 2010 02:04
-
-
Save batok/282514 to your computer and use it in GitHub Desktop.
This idea can be used in turbogears
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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