开发者

Queries with JDOQL Unique Result Set

开发者 https://www.devze.com 2023-01-29 23:48 出处:网络
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?I dont know about JDOQL,

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消