comparison roundup/cgi/accept_language.py @ 6347:3b945aee0919

accept_language parse; fix priority order; preserve insertion order There are conditions under which the order is not correctly returned. Use hashq.hashpop in a loop to get items out of hash (list) in proper priority order. Make 3 element tuples in hashq include insertion order. This makes tuples inserted later lower in priority when priority q values are the same. Makes: "zn;q=1.0;q= 1.0,pt-br,en-US; q=0.5" return ['zn', 'pt_br', 'en_US'] (pt_br is default prio of 1 like zn). Otherwise we get ['pt_br', 'zn', 'en_US'] since 'p' > 'z'.
author John Rouillard <rouilj@ieee.org>
date Sun, 21 Mar 2021 18:39:43 -0400
parents ed8a9974c1bd
children 63c9680eed20
comparison
equal deleted inserted replaced
6346:7c6713e801d9 6347:3b945aee0919
62 qls = [x[0] for x in pls if len(x) > 0] 62 qls = [x[0] for x in pls if len(x) > 0]
63 63
64 # use a heap queue to sort by quality values. 64 # use a heap queue to sort by quality values.
65 # the value of each item is 1.0 complement. 65 # the value of each item is 1.0 complement.
66 pq = [] 66 pq = []
67 order=0
67 for l in qls: 68 for l in qls:
69 order +=1
68 if l[0] != '': 70 if l[0] != '':
69 heapq.heappush(pq, (0.0, l[0])) 71 heapq.heappush(pq, (0.0, order, l[0]))
70 else: 72 else:
71 heapq.heappush(pq, (1.0-float(l[2]), l[1])) 73 heapq.heappush(pq, (1.0-float(l[2]), order, l[1]))
72 74
73 # get the languages ordered by quality 75 # get the languages ordered by quality
74 # and replace - by _ 76 # and replace - by _
75 return [x[1].replace('-', '_') for x in pq] 77 return [ heapq.heappop(pq)[2].replace('-','_')
78 for x in range(len(pq)) ]
76 79
77 if __name__ == "__main__": 80 if __name__ == "__main__":
78 import doctest 81 import doctest
79 doctest.testmod() 82 doctest.testmod()
80 83

Roundup Issue Tracker: http://roundup-tracker.org/