[Zope] single iterations over duplicate values

Dennis Allison allison at sumeru.stanford.EDU
Sat Jan 3 17:15:25 EST 2004


If the query is fast, you could do both and use the result from the one
with the DISTINCT option for loop control and the other for processing.

Alternatively, you could post process the query to create a list to use
in the <dtml-in>.  Take a look at the database interface system code in
/home/zopesys/lib/python/Shared/DC/ZRDB.  Applied to a SQL_query_result
object, tuples() produces a list of of rows, dictionaries() produces a 
list of dictionaries with the column_names as keys.

I assume you have one particular column that contains the values you are
iterating.  The following python script will return an appropriate list
for <dtml-in>.  The sort is optional. 

# Script (Python) "QueryIterationList"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=query, columnName
##title=
##
dlist = Q.dictionaries()
iterdict = {}
for d in dlist: iterdict[d[columnName]]=1
iterlist = interdict.keys()
return iterlist.sort()

I've not checked this code so YMMV.  You can do the same thing with
tuples() and the columnIndex and gain a bit of speed.


On Sat, 3 Jan Garry Saccington replied:

>The query must return duplicates so DISTINCT is not suitable, thanks
>anyway.

On Sat, 3 Jan 2004, Dennis Allison wrote:

> Probably the right thing to do is to fix the SQL query--look at 
> the 'DISTINCT' option on the SELECT is probably what you want.
> 
> On Sat, 3 Jan 2004, garry saddington wrote:
> 
> > I have a dtml method that accepts input from a zsql method and dtml-in is used 
> > to loop over the result set. However, there are duplicate values in the query 
> > result set(by design) but i want to list only one iteration of each value in 
> > my dtml method. Does anyone know how to do this, ie skip over duplicate 
> > results?
> > regards
> > garry





More information about the Zope mailing list