Skip to main content

Python port of the node.js querystring module

Project description

This repo is a port of the node.js querystring module.

It contains two methods, parse_qs and stringify_obj that are functionally equivalent to the querystring.parse and querystring.stringify node counterparts. It also contains default encode_uri_component and decode_uri_component functions that can be used for convenience.

Installation

pip install querystring

Usage

Parsing a query string

>>> querystring.parse_qs('foo=bar&baz=qux&baz=quux&corge=')
{u'foo': u'bar', u'baz': [u'qux', u'quux'], u'corge': u''}
>>> querystring.parse_qs('foo:bar;baz:qux;baz:quux;corge:', sep=';', eq=':')
{u'foo': u'bar', u'baz': [u'qux', u'quux'], u'corge': u''}
>>> def gbk_decode(s):
...     return urllib.unquote(s)
...
>>> querystring.parse_qs('foo=bar&w=%D6%D0%CE%C4', decode_fn=gbk_decode)
{'foo': 'bar', 'w': '\xd6\xd0\xce\xc4'}  # u'中文'

Converting an object back into a query string

>>> querystring.stringify_obj({'foo': 'bar', 'baz': ['qux', 'quux'], 'corge': ''})
'foo=bar&baz=qux&baz=quux&corge='
>>> querystring.stringify_obj({'foo': 'bar', 'baz': ['qux', 'quux'], 'corge': ''}, sep=';', eq=':')
'foo:bar;baz:qux;baz:quux;corge:'
>>> def gbk_encode(s):
...     return urllib.quote(unicode(s).encode('gbk'))
...
>>> querystring.stringify_obj({'foo': 'bar', 'w': u'中文'}, encode_fn=gbk_encode)
'foo=bar&w=%D6%D0%CE%C4'

Motivation

We were looking for a simple module to parse query parameters in a sensible way, and were unsatisfied by how urlparse handled params (putting all values in a list), so we made this.

License

Copyright Refinery29, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

querystring-0.1.0.tar.gz (3.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page