1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 10:42:34 +01:00

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
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
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":

View File

@ -123,6 +123,37 @@ class TestText(unittest.TestCase):
result = ["c", "b", "a", "d"]
self.assertEqual(list(text.extract_iter(txt, "[", "]")), result)
def test_parse_query(self):
# standard stuff
self.assertEqual(
text.parse_query(""), {})
self.assertEqual(
text.parse_query("foo=1"), {"foo": "1"})
self.assertEqual(
text.parse_query("foo=1&bar=2"), {"foo": "1", "bar": "2"})
# missing value
self.assertEqual(
text.parse_query("bar"), {})
self.assertEqual(
text.parse_query("foo=1&bar"), {"foo": "1"})
self.assertEqual(
text.parse_query("foo=1&bar&baz=3"), {"foo": "1", "baz": "3"})
# keys with identical names
self.assertEqual(
text.parse_query("foo=1&foo=2"), {"foo": "1"})
self.assertEqual(
text.parse_query("foo=1&bar=2&foo=3&bar=4"),
{"foo": "1", "bar": "2"},
)
# non-string arguments
self.assertEqual(text.parse_query(()), {})
self.assertEqual(text.parse_query([]), {})
self.assertEqual(text.parse_query({}), {})
self.assertEqual(text.parse_query(None), {})
if __name__ == '__main__':
unittest.main()