<commit_before># -*- coding: utf-8 -*-
import json
import re
import urllib2

STATUS_OK = 'OK'
STATUS_ZERO_RESULTS = 'ZERO_RESULTS'
STATUS_OVER_QUERY_LIMIT = 'OVER_QUERY_LIMIT'
STATUS_REQUEST_DENIED = 'REQUEST_DENIED'
STATUS_INVALID_REQUEST = 'INVALID_REQUEST'
STATUS_UNKNOWN_ERROR = 'UNKNOWN_ERROR'

STATUS_CODES = (
    (STATUS_OK, 'Successfully parsed.'),
    (STATUS_ZERO_RESULTS, 'Successful parsed but returned no results.'),
    (STATUS_OVER_QUERY_LIMIT, 'Over your quota.'),
    (STATUS_REQUEST_DENIED, 'Request was denied.'),
    (STATUS_INVALID_REQUEST, 'Query is missing.'),
    (STATUS_UNKNOWN_ERROR, 'Request could not be processed due to a server error. Try again.'),
)


class GeocodeApi(object):
    STATUS_OK = 'OK'

    _json_api_address = 'https://maps.googleapis.com/maps/api/geocode/json?address='

    location = None

    def __init__(self, api_key):
        self._api_key = "&key=%s" % api_key

    def __repr__(self):
        return '<Geocode: %s>' % self.location

    def query(self, location):
        """Main method should returns json results."""
        self.set_location(location)

        request = self._prepare_request(location)

        return self._get_json_data(request)

    def set_location(self, location):
        """Method sets location variable."""
        self.location = location

    def _prepare_request(self, query_):
        """Method prepares query to request for google api."""
        prepare = re.sub('\s+', ' ', query_).strip()
        prepare = ',+'.join(prepare.split())

        prepare = "%s%s%s" % (self._json_api_address, prepare, self._api_key)

        return prepare

    @staticmethod
    def _get_status_code(status):
        """Method should returns information about status code."""
        return dict(STATUS_CODES)[status]

    def _get_json_data(self, request):
        """Method sends request to google geocode api and returns json data."""
        json_data = urllib2.urlopen(request).read()

        json_results = json.loads(json_data)

        status = json_results['status']<commit_msg>using requests to avoid problems with py2 and py3<commit_after># -*- coding: utf-8 -*-
import re
import requests


STATUS_OK = 'OK'
STATUS_ZERO_RESULTS = 'ZERO_RESULTS'
STATUS_OVER_QUERY_LIMIT = 'OVER_QUERY_LIMIT'
STATUS_REQUEST_DENIED = 'REQUEST_DENIED'
STATUS_INVALID_REQUEST = 'INVALID_REQUEST'
STATUS_UNKNOWN_ERROR = 'UNKNOWN_ERROR'

STATUS_CODES = (
    (STATUS_OK, 'Successfully parsed.'),
    (STATUS_ZERO_RESULTS, 'Successful parsed but returned no results.'),
    (STATUS_OVER_QUERY_LIMIT, 'Over your quota.'),
    (STATUS_REQUEST_DENIED, 'Request was denied.'),
    (STATUS_INVALID_REQUEST, 'Query is missing.'),
    (STATUS_UNKNOWN_ERROR, 'Request could not be processed due to a server'
                           'error. Try again.'),
)


class GeocodeApi(object):
    STATUS_OK = 'OK'

    _json_api_address =\
        'https://maps.googleapis.com/maps/api/geocode/json?address='

    location = None

    def __init__(self, api_key):
        self._api_key = "&key=%s" % api_key

    def __repr__(self):
        return '<Geocode: %s>' % self.location

    def query(self, location):
        """Main method should returns json results."""
        self.set_location(location)

        request = self._prepare_request(location)

        return self._get_json_data(request)

    def set_location(self, location):
        """Method sets location variable."""
        self.location = location

    def _prepare_request(self, query_):
        """Method prepares query to request for google api."""
        prepare = re.sub('\s+', ' ', query_).strip()
        prepare = ',+'.join(prepare.split())

        prepare = "%s%s%s" % (self._json_api_address, prepare, self._api_key)

        return prepare

    @staticmethod
    def _get_status_code(status):
        """Method should returns information about status code."""
        return dict(STATUS_CODES)[status]

    def _get_json_data(self, request):
        """Method sends request to google geocode api and returns json data."""
        # with requests lib we don't need a if/else for urlllib py2/py3
        json_results = requests.get(request).json()

        status = json_results['status']
{'fraction_non_alphanumeric': 0.1174655908875178, 'fraction_numerical': 0.0018984337921214998, 'mean_word_length': 3.3142272262026613, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1258733', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1263, 'n_words': 324}