[Twisted-Python] DB dictionary/tuple result wrapper for woven models

James Y Knight foom at fuhm.net
Tue Oct 14 23:21:30 MDT 2003


I took dtuple.py (a wrapper that makes database results look like both 
a tuple and a dictionary simultaneously, which I found on the web) and 
wrapped it as a woven model, so you can use your database results and 
get submodels with both integer keys and column-name keys. It seems 
like it's probably useful to a bunch of people, so I'm making it 
available.

Currently living at:
http://www.ai.mit.edu/people/jknight/dtuple.py

Usage:
dbtupleize(curs, *args, **kw):
   Use this function as the argument to adbapi.runInteraction.

   E.g.:
   return dbpool.runInteraction(dbtupleize, "select * from table")

   It returns a DatabaseList of DatabaseTuples.

   A DatabaseList is just like a normal list execpt it has a getKeys()
   method.

   A DatabaseTuple is both a tuple and dictionary. If used as a tuple,
   it is ordered in the same order as the enclosing DatabaseList's
   getKeys() method.


   One use case is with a template like so:
       <table model="users" view="List">
	<tr pattern="listHeader" view="List" model="keys">
	  <th pattern="listItem" view="Text" model="."></th>
	</tr>
	<tr pattern="listItem" view="List" model=".">
	  <td pattern="listItem" view="Text" model="."></td>
	</tr>
     </table>

    and a model like:
       wmfactory_users(self, request):
         return dbpool.runInteraction(dbtupleize, "select * from users")

   Another is a template like:
        Userid: <span model="curuser/userid" view="Text" /><br>
        Email: <span model="curuser/email" view="Text" /><br>

    with a model like:
      wmfactory_curuser(self, request):
        return dbpool.runInteraction(dbtupleize, "select * from users 
where userid=%(self.curuserid)s", {'curuserid':self.curuserid})


As you can see from the comments in the file, it is doing a bit of 
excessive wrapping (wrapping a model around a dbtuple around the actual 
results), but since IMO woven models need a rewrite anyways, I'm not 
too concerned about integrating them more tightly into things at the 
moment, now that this works the way it is.

James





More information about the Twisted-Python mailing list