urllib.urlencode small improvement

Oleg Broytmann phd at phd.russ.ru
Thu Oct 28 10:07:43 EDT 1999


Hello!

   Currently urllib.urlencode takes dictionary {"x": "y", "u": "v"} and
produces encoded string as "x=y&u=v". But wat if I want to repeat some
keys: "x=y1&x=y2&u=v"? I cannot pass this to urlencode using dictionaries!

   The following patch adds the feature to urlencode: either dict or a list
(or tuple) of pairs (lists or tuples) can be passed to urlencode:

--- urllib.py-orig	Thu Apr 15 14:44:08 1999
+++ urllib.py	Thu Oct 28 18:01:22 1999
@@ -914,9 +914,15 @@
     else:
         return quote(s, safe)
 
-def urlencode(dict):
+
+dictType = type({})
+
+# obj must be either dict or list of tuples [(k1, v1), (k2, v2), ...] or list of lists [[k1, v1], [k2, v2], ...]
+def urlencode(obj):
      l = []
-     for k, v in dict.items():
+     if type(obj) == dictType:
+         obj = obj.items()
+     for k, v in obj:
          k = quote_plus(str(k))
          v = quote_plus(str(v))
          l.append(k + '=' + v)

Oleg.
---- 
    Oleg Broytmann      Foundation for Effective Policies      phd at phd.russ.ru
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list