comparison roundup/cgi/accept_language.py @ 6980:63c9680eed20

fake8 whitespace; variable name replacement
author John Rouillard <rouilj@ieee.org>
date Wed, 14 Sep 2022 20:19:59 -0400
parents 3b945aee0919
children
comparison
equal deleted inserted replaced
6979:0b6c54893ec5 6980:63c9680eed20
45 45
46 46
47 def parse(language_header): 47 def parse(language_header):
48 """parse(string_with_accept_header_content) -> languages list""" 48 """parse(string_with_accept_header_content) -> languages list"""
49 49
50 if language_header is None: return [] 50 if language_header is None: return [] # noqa: E701
51 51
52 # strip whitespaces. 52 # strip whitespaces.
53 lh = language_header.translate(*remove_ws) 53 lh = language_header.translate(*remove_ws)
54 54
55 # if nothing, return 55 # if nothing, return
56 if lh == "": return [] 56 if lh == "": return [] # noqa: E701
57 57
58 # split by commas and parse the quality values. 58 # split by commas and parse the quality values.
59 pls = [lre.findall(x) for x in lh.split(',')] 59 pls = [lre.findall(x) for x in lh.split(',')]
60 60
61 # drop uncomformant 61 # drop uncomformant
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 order = 0
68 for l in qls: 68 for lang in qls:
69 order +=1 69 order += 1
70 if l[0] != '': 70 if lang[0] != '':
71 heapq.heappush(pq, (0.0, order, l[0])) 71 heapq.heappush(pq, (0.0, order, lang[0]))
72 else: 72 else:
73 heapq.heappush(pq, (1.0-float(l[2]), order, l[1])) 73 heapq.heappush(pq, (1.0-float(lang[2]), order, lang[1]))
74 74
75 # get the languages ordered by quality 75 # get the languages ordered by quality
76 # and replace - by _ 76 # and replace - by _
77 return [ heapq.heappop(pq)[2].replace('-','_') 77 return [heapq.heappop(pq)[2].replace('-', '_') for x in range(len(pq))]
78 for x in range(len(pq)) ] 78
79 79
80 if __name__ == "__main__": 80 if __name__ == "__main__":
81 import doctest 81 import doctest
82 doctest.testmod() 82 doctest.testmod()
83 83

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