0

In my GAE application I want to make a JDOQL which returns a List where every element exist at most once even in the database there are more. How can I do this?

metdos
  • 13,411
  • 17
  • 77
  • 120

1 Answers1

0

I dont know about JDOQL, but if you want a list where each entity exists utmost once i.e each list element is unique, then you could do the following:

Asume you have an entit type / model class that we call Type, with attributes att1,attr2. You want to have a list of unique elements based on one or more of the attributes, say attr2.

you can use the following method that I adapted from a good source on the issue:

def unique(seq, idfun=None):  
        ''' A function that returns a list of unique items in a very efficient manner  
        Refer to : http://www.peterbe.com/plog/uniqifiers-benchmark ''' 
        # order preserving 
        if idfun is None: 
                def idfun(x): return x 
        seen = {} 
        result = [] 
        for item in seq: 
                marker = idfun(item) 
                # in old Python versions: 
                # if seen.has_key(marker) 
                # but in new ones: 
                if marker in seen: continue 
                seen[marker] = 1 
                result.append(item) 
        return result 

the to get a list of unique elements from the datastore type Type based on attr2 i could do something like:

list = Type.all()

unique_list = unique(list,lambda t: t.attr2)

Hope this helps because it has been the best method for me so far.

JWL
  • 13,591
  • 7
  • 57
  • 63