<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2009  Red Hat, Inc.
# This file is part of python-fedora
# 
# python-fedora is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# 
# python-fedora is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with python-fedora; if not, see <http://www.gnu.org/licenses/>
#
'''
Functions to manipulate urls.

.. versionadded:: 0.3.17

.. moduleauthor:: John (J5) Palmieri <<EMAIL>>
.. moduleauthor:: <NAME> <<EMAIL>>
'''

from urlparse import urlparse, urlunparse
from urllib import urlencode

from kitchen.iterutils import isiterable

try:
    from urlparse import parse_qs
except ImportError:
    from cgi import parse_qs


def update_qs(uri, new_params, overwrite=True):
    '''Helper function for updating query string values.
    
    Similar to calling update on a dictionary except we modify the query
    string of the uri instead of another dictionary.

    :arg uri: URI to modify
    :arg new_params: Dict of new query parameters to add.
    :kwarg overwrite: If True (default), any old query parameter with the same
        name as a new query parameter will be overwritten.  If False, the new
        query parameters will be appended to a list with the old parameters at
        the start of the list.
    :returns: URI with the new parameters added.
    '''
    loc = list(urlparse(uri))
    query_dict = parse_qs(loc[4])
    if overwrite:
        # Overwrite any existing values with the new values
        query_dict.update(new_params)
    else:
        # Add new values in addition to the existing parameters
        # For every new entry
        for key in new_params:
            # If the entry already is present
            if key in query_dict:
                if isiterable(new_params[key]):
                    # If the new entry is a non-string iterable<commit_msg>PEP8 urlutils.py<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2009  Red Hat, Inc.
# This file is part of python-fedora
#
# python-fedora is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# python-fedora is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with python-fedora; if not, see <http://www.gnu.org/licenses/>
#
'''
Functions to manipulate urls.

.. versionadded:: 0.3.17

.. moduleauthor:: John (J5) Palmieri <<EMAIL>>
.. moduleauthor:: <NAME> <<EMAIL>>
'''

from urlparse import urlparse, urlunparse
from urllib import urlencode

from kitchen.iterutils import isiterable

try:
    from urlparse import parse_qs
except ImportError:
    from cgi import parse_qs


def update_qs(uri, new_params, overwrite=True):
    '''Helper function for updating query string values.

    Similar to calling update on a dictionary except we modify the query
    string of the uri instead of another dictionary.

    :arg uri: URI to modify
    :arg new_params: Dict of new query parameters to add.
    :kwarg overwrite: If True (default), any old query parameter with the same
        name as a new query parameter will be overwritten.  If False, the new
        query parameters will be appended to a list with the old parameters at
        the start of the list.
    :returns: URI with the new parameters added.
    '''
    loc = list(urlparse(uri))
    query_dict = parse_qs(loc[4])
    if overwrite:
        # Overwrite any existing values with the new values
        query_dict.update(new_params)
    else:
        # Add new values in addition to the existing parameters
        # For every new entry
        for key in new_params:
            # If the entry already is present
            if key in query_dict:
                if isiterable(new_params[key]):
                    # If the new entry is a non-string iterable
{'fraction_non_alphanumeric': 0.06778552746294682, 'fraction_numerical': 0.005884917175239756, 'mean_word_length': 3.04673721340388, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6466787', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1211, 'n_words': 600}