<commit_before>"""
Utility functions used by more than module within pywind.
"""

import requests


def get_or_post_a_url(url, post=False, **kwargs):
    """
    Use the requests library to either get or post to a specified URL. The return code is checked
    and exceptions raised if there has been a redirect or the status code is not 200.

    Args:
         url: The URL to be used.
         post: True if the request should be a POST. Default is False which results in a GET request.
         kwargs: Optional keyword arguments that are passed directly to the requests call.
    Returns:
        The requests object is returned if all checks pass.
    Raises:
        Raises Exception for various errors.
    """
    try:
        if post:
            req = requests.post(url, **kwargs)
        else:
            req = requests.get(url, **kwargs)
    except requests.exceptions.SSLError as err:
        raise Exception("SSL Error\n  Error: {}\n    URL: {}".
                        format(err.message[0], url))
    except requests.exceptions.ConnectionError:
        raise Exception("Unable to connect to the server.\nURL: {}".
                        format(url))
    if req.status_code != 200:
        raise Exception("Request was completed, but status code is not 200.\n"+
                        "URL: {}\nStatus Code: {}".format(url, req.status_code))
    if req.url != url:
        raise Exception("Returned URL was from a different URL than requested.\n" +
                        "Requested: {}\nActual:  {}".format(url, req.url))
    return req<commit_msg>Update the utils class with extra functionality.<commit_after>"""
Utility functions used by more than module within pywind.
"""
import argparse
import requests


def get_or_post_a_url(url, post=False, **kwargs):
    """
    Use the requests library to either get or post to a specified URL. The return code is checked
    and exceptions raised if there has been a redirect or the status code is not 200.

    Args:
         url: The URL to be used.
         post: True if the request should be a POST. Default is False which results in a GET request.
         kwargs: Optional keyword arguments that are passed directly to the requests call.
    Returns:
        The requests object is returned if all checks pass.
    Raises:
        Raises Exception for various errors.
    """
    try:
        if post:
            req = requests.post(url, **kwargs)
        else:
            req = requests.get(url, **kwargs)
    except requests.exceptions.SSLError as err:
        raise Exception("SSL Error\n  Error: {}\n    URL: {}".
                        format(err.message[0], url))
    except requests.exceptions.ConnectionError:
        raise Exception("Unable to connect to the server.\nURL: {}".
                        format(url))
    if req.status_code != 200:
        raise Exception("Request was completed, but status code is not 200.\n"+
                        "URL: {}\nStatus Code: {}".format(url, req.status_code))
    if req.url != url:
        raise Exception("Returned URL was from a different URL than requested.\n" +
                        "Requested: {}\nActual:  {}".format(url, req.url))
    return req


def commandline_parser(help_text):
    """
    Simple function to create a command line parser with some generic options.
    """
    parser = argparse.ArgumentParser(description=help_text)
    parser.add_argument('--debug', action='store_true', help='Enable debugging')
    parser.add_argument('--request-debug', action='store_true', help='Enable debugging of requests')
    parser.add_argument('--log-filename', action='store', help='Filename to write logging to')
    return parser
{'fraction_non_alphanumeric': 0.09496598639455782, 'fraction_numerical': 0.005442176870748299, 'mean_word_length': 2.4419475655430714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5167764', 'n_tokens_mistral': 981, 'n_tokens_neox': 946, 'n_words': 386}