1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 18:53:21 +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 5c487300ee
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
3 changed files with 37 additions and 2 deletions

View File

@ -24,7 +24,7 @@ class HitomiGalleryExtractor(ChapterExtractor):
test = [
("https://hitomi.la/galleries/867789.html", {
"url": "cb759868d090fe0e2655c3e29ebf146054322b6d",
"keyword": "b1e66ff971fc8cb80240a687f508f3b74053f799",
"keyword": "85e453d01ee7f137669e75a764ccdc65ca092ad2",
}),
("https://hitomi.la/reader/867789.html", None),
]

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()