[Python-Dev] [Python-ideas] Proposed addtion to urllib.parse in 3.1 (and urlparse in 2.7)

Mart Sõmermaa mrts.pydev at gmail.com
Mon Apr 13 11:29:46 CEST 2009


On Mon, Apr 13, 2009 at 12:56 AM, Antoine Pitrou <solipsis at pitrou.net>wrote:

> Mart Sõmermaa <mrts.pydev <at> gmail.com> writes:
> >
> > Proposal: add add_query_params() for appending query parameters to an URL
> to
> urllib.parse and urlparse.
>
> Is there anything to /remove/ a query parameter?


I'd say this is outside the scope of add_query_params().

As for the duplicate handling, I've implemented a threefold strategy that
should address all use cases raised before:

 def add_query_params(*args, **kwargs):
    """
    add_query_parms(url, [allow_dups, [args_dict, [separator]]], **kwargs)

    Appends query parameters to an URL and returns the result.

    :param url: the URL to update, a string.
    :param allow_dups: if
        * True: plainly append new parameters, allowing all duplicates
          (default),
        * False: disallow duplicates in values and regroup keys so that
          different values for the same key are adjacent,
        * None: disallow duplicates in keys -- each key can have a single
          value and later values override the value (like dict.update()).
    :param args_dict: optional dictionary of parameters, default is {}.
    :param separator: either ';' or '&', the separator between key-value
        pairs, default is '&'.
    :param kwargs: parameters as keyword arguments.

    :return: original URL with updated query parameters or the original URL
        unchanged if no parameters given.
    """

The commit is

http://github.com/mrts/qparams/blob/b9bdbec46bf919d142ff63e6b2b822b5d57b6f89/qparams.py

extensive description of the behaviour is in the doctests.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20090413/46a27373/attachment.htm>


More information about the Python-Dev mailing list