<commit_before>from __future__ import print_function
import sys
import json

HEADER = {'User-Agent': 'RealTimeWeb Geocode library for educational purposes'}
PYTHON_3 = sys.version_info >= (3, 0)

if PYTHON_3:
    import urllib.request as request
    from urllib.parse import quote_plus
else:
    import urllib2
    from urllib import quote_plus


class GeocodeException(Exception):
    pass


def _iteritems(dict_):
    """
    Internal method to factor-out Py2-to-3 differences in dictionary item
        iterator methods
    :param dictionary dict_: the dictionary to parse
    :returns: the iterable dictionary
    """
    if PYTHON_3:
        return dict_.items()
    else:
        return dict_.iteritems()


def _get(url):
    """
    Internal method to convert a URL into it's response (a *str*).
    :param str url: the url to request a response from
    :returns: the *str* response
    """
    if PYTHON_3:
        req = request.Request(url, headers=HEADER)
        response = request.urlopen(req)
        return response.read().decode('utf-8')
    else:
        req = urllib2.Request(url, headers=HEADER)
        response = urllib2.urlopen(req)
        return response.read()


def _urlencode(query, params):
    """
    Internal method to combine the url and params into a single url string.
    :param str query: the base url to query
    :param dict params: the parameters to send to the url
    :returns: a *str* of the full url
    """
    return query + '?' + '&'.join(key+'='+quote_plus(str(value))
                                  for key, value in _iteritems(params))


def _get_coords(json_res):
    """
    Internal method to get a *dict* of the latitude and longitude from the JSON
    :param dict json_res: the JSON response to parse
    :returns: a *dict* containing the coordinates
    """
    location = json_res['results'][0]['geometry']['location']
    lat = location['lat']
    lon = location['lng']
    coords = {'latitude': lat, 'longitude': lon}
    return coords


def _check_status(json_res):
    """
    Internal method to check the status of the API call and raise the
        relevant exception
    :param dict json_res: the JSON response to parse
    """
    status = json_res['status']
    if status:
        if 'ZERO_RESULTS' in status:
            raise GeocodeException('Sorry no results found')
        elif not 'OK' in status:
            raise GeocodeException(json_res['error_message'])


def _form_query(params):
    """
    Internal method to form and query the server
    :param dict params: the parameters to pass to the server
    :returns: a *dict* of the JSON response
    """
    baseurl = 'https://maps.googleapis.com/maps/api/geocode/json'
    query = _urlencode(baseurl, params)
    query = ''.join((query, '&sensor=false'))

    response = _get(query)
    json_res = json.loads(response)
    return json_res


def code(address):
    """
    Convert an address to it's respective longitude and latitude
    :param str address: the address to get coordinates of
    :returns: the GPS coordinates of the location
    """
    if not address or not isinstance(address, str):
        raise GeocodeException('No valid address was given')

    params = {'address': address}

    json_res = _form_query(params)

    _check_status(json_res)
    return _get_coords(json_res)<commit_msg>Added newlines in file<commit_after>from __future__ import print_function
import sys
import json

HEADER = {'User-Agent': 'RealTimeWeb Geocode library for educational purposes'}
PYTHON_3 = sys.version_info >= (3, 0)

if PYTHON_3:
    import urllib.request as request
    from urllib.parse import quote_plus
else:
    import urllib2
    from urllib import quote_plus


class GeocodeException(Exception):
    pass


def _iteritems(dict_):
    """
    Internal method to factor-out Py2-to-3 differences in dictionary item
        iterator methods

    :param dictionary dict_: the dictionary to parse
    :returns: the iterable dictionary
    """
    if PYTHON_3:
        return dict_.items()
    else:
        return dict_.iteritems()


def _get(url):
    """
    Internal method to convert a URL into it's response (a *str*).

    :param str url: the url to request a response from
    :returns: the *str* response
    """
    if PYTHON_3:
        req = request.Request(url, headers=HEADER)
        response = request.urlopen(req)
        return response.read().decode('utf-8')
    else:
        req = urllib2.Request(url, headers=HEADER)
        response = urllib2.urlopen(req)
        return response.read()


def _urlencode(query, params):
    """
    Internal method to combine the url and params into a single url string.

    :param str query: the base url to query
    :param dict params: the parameters to send to the url
    :returns: a *str* of the full url
    """
    return query + '?' + '&'.join(key+'='+quote_plus(str(value))
                                  for key, value in _iteritems(params))


def _get_coords(json_res):
    """
    Internal method to get a *dict* of the latitude and longitude from the JSON

    :param dict json_res: the JSON response to parse
    :returns: a *dict* containing the coordinates
    """
    location = json_res['results'][0]['geometry']['location']
    lat = location['lat']
    lon = location['lng']
    coords = {'latitude': lat, 'longitude': lon}
    return coords


def _check_status(json_res):
    """
    Internal method to check the status of the API call and raise the
        relevant exception

    :param dict json_res: the JSON response to parse
    """
    status = json_res['status']
    if status:
        if 'ZERO_RESULTS' in status:
            raise GeocodeException('Sorry no results found')
        elif not 'OK' in status:
            raise GeocodeException(json_res['error_message'])


def _form_query(params):
    """
    Internal method to form and query the server

    :param dict params: the parameters to pass to the server
    :returns: a *dict* of the JSON response
    """
    baseurl = 'https://maps.googleapis.com/maps/api/geocode/json'
    query = _urlencode(baseurl, params)
    query = ''.join((query, '&sensor=false'))

    response = _get(query)
    json_res = json.loads(response)
    return json_res


def code(address):
    """
    Convert an address to it's respective longitude and latitude

    :param str address: the address to get coordinates of
    :returns: the GPS coordinates of the location
    """
    if not address or not isinstance(address, str):
        raise GeocodeException('No valid address was given')

    params = {'address': address}

    json_res = _form_query(params)

    _check_status(json_res)
    return _get_coords(json_res)
{'fraction_non_alphanumeric': 0.09515260323159784, 'fraction_numerical': 0.0038898862956313583, 'mean_word_length': 2.943952802359882, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '325953', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1927, 'n_words': 684}