improve 'parse_query()' and add tests

- another irrelevant micro-optimization !
- use urllib.parse.parse_qsl directly instead of parse_qs, which
  just packs the results of parse_qsl in a different data structure
- reduced memory requirements since no additional dict and lists are
  created
This commit is contained in:
Mike Fährmann
2018-04-13 19:21:32 +02:00
parent 51ea699083
commit 6d8b191ea7
2 changed files with 36 additions and 1 deletions

View File

@@ -130,7 +130,11 @@ def extract_iter(txt, begin, end, pos=0):
def parse_query(qs):
"""Parse a query string into key-value pairs"""
return {key: vlist[0] for key, vlist in urllib.parse.parse_qs(qs).items()}
result = {}
for key, value in urllib.parse.parse_qsl(qs):
if key not in result:
result[key] = value
return result
if os.name == "nt":