[New-bugs-announce] [issue5877] Add a function for updating URL query parameters

Mart Sõmermaa <report@bugs.python.org> at psf.upfronthosting.co.za Mart Sõmermaa <report@bugs.python.org> at psf.upfronthosting.co.za
Wed Apr 29 15:32:15 CEST 2009

New submission from Mart Sõmermaa <mrts at mrts.pri.ee>:


Add update_query_params() for updating or adding URL query parameters to
urllib.parse and urlparse.



Previously in Python-ideas:

The consensus seems to be that this is needed, opinions on how the API
should look like vary.


The following features were requested by different people:
- ability to order parameters (by passing them in a ordered data structure)
- "plain" behaviour that would keep the existing parameters as is and
add passed in parameters, retaining duplicates
- behaviour that removes duplicated values
- dict.update()-like behaviour that would not allow duplicate keys
(updating values for existing keys)
- removal of existing parameters


in particular

See the docstring in qparams.py for 

Currently, positional arguments are used to avoid name collisions in
keyword arguments that were initially devised as the default, easy way
to pass parameters. As the function signature has grown quite complex,
this looks suboptimal.

Also, the general sentiment was that the current three-valued logic for
choosing key update strategy is also suboptimal. Instead, the strategy
should be passed via a function and the strategies listed above should
be provided in the module.

Finally, removing keys is currently missing. It was proposed that
passing foo=None would remove the parameter foo. Currently, None is used
to mark parameters that do not take values, another marker is needed for
that if removal is to be marked with None.

Following on Nick Coghlan's suggestion in
http://mail.python.org/pipermail/python-dev/2009-April/088866.html and
considering the preference for a strategy function, the API could look
as follows:

def add_query_params(url, params, strategy=allow_dulicates, sep='&')

I.e. keyword arguments would not be supported and the "default" use
would be as follows:

>>> add_query_params('foo', dict(bar='baz'))

components: Library (Lib)
messages: 86802
nosy: mrts
severity: normal
status: open
title: Add a function for updating URL query parameters
type: feature request
versions: Python 2.7, Python 3.1

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list