[Zope] Great How-To on using ZSQL methods from an External me thod

Jeffrey Shell Jeffrey@digicool.com
Tue, 2 Nov 1999 13:33:07 -0500


> I'm still fairly new to Python, but the only part of your 
> How-To that, IMHO,
> might be written a little more simply is the code fragment in 
> the "What is
> res?" section:
> 
>     fields2index={}
>     fieldnames=res._schema.items()
>     for i in range(len(fieldnames)):
>         fields2index[fieldnames[i][0]]=fieldnames[i][1]
> 
> which might read more clearly as:
> 
>     fields2index = {}
>     for name, index in res._schema.items():
>         fields2index[ name ] = index
> 

Don't forget that you can call a field directly by both getitem and
getattr:

res=someSqlMethod(name='bob', relation='uncle')

for r in res:
   #loop through the result set, r is a Record object
   if r.age < 12:
     #do something for young uncles here
   else:
     #do something else for everyone older.

AGE is a column in the results set.  There's a lot of meta-data in
Record objects that can generally be avoided in common programming.