<commit_before>from collections import OrderedDict
import logging
from math import ceil
import re
import uuid
import warnings

from django.conf import settings
import requests
from six.moves.urllib.parse import quote, urljoin

VERSION = (0, 4)
__version__ = '.'.join(map(str, VERSION))
__all__ = ('GAErrorReportingMixin', 'GARequestErrorReportingMixin')

logger = logging.getLogger(__name__)


class OrderedQueryDict(OrderedDict):
    """
    A simplified version of django.http.request.QueryDict
    that preserves key order
    """

    def urlencode(self):
        """
        Convert dictionary into a query string; keys are
        assumed to always be str
        """
        output = ('%s=%s' % (k, quote(v)) for k, v in self.items())
        return '&'.join(output)


class GAErrorReportingMixin(object):
    """
    Form mixin that reports form errors to Google Analytics with events
    """
    ga_endpoint_base = 'https://ssl.google-analytics.com/'
    # NB: non-ssl version is http://www.google-analytics.com/
    ga_tracking_id = None
    ga_client_id = None
    ga_event_category = None
    ga_batch_hits = True

    def is_valid(self):
        """
        Error reporting is triggered when a form is checked for validity
        """
        is_valid = super(GAErrorReportingMixin, self).is_valid()
        if not is_valid:
            try:
                self.report_errors_to_ga(self.errors)
            except:
                logger.exception('Failed to report form errors to Google Analytics')
        return is_valid

    def get_ga_single_endpoint(self):
        """
        URL for collecting a single hit
        """
        return urljoin(self.ga_endpoint_base, 'collect')

    def get_ga_batch_endpoint(self):
        """
        URL for collecting multiple hits
        """
        return urljoin(self.ga_endpoint_base, 'batch')<commit_msg>Only report errors on bound forms<commit_after>from collections import OrderedDict
import logging
from math import ceil
import re
import uuid
import warnings

from django.conf import settings
import requests
from six.moves.urllib.parse import quote, urljoin

VERSION = (0, 4)
__version__ = '.'.join(map(str, VERSION))
__all__ = ('GAErrorReportingMixin', 'GARequestErrorReportingMixin')

logger = logging.getLogger(__name__)


class OrderedQueryDict(OrderedDict):
    """
    A simplified version of django.http.request.QueryDict
    that preserves key order
    """

    def urlencode(self):
        """
        Convert dictionary into a query string; keys are
        assumed to always be str
        """
        output = ('%s=%s' % (k, quote(v)) for k, v in self.items())
        return '&'.join(output)


class GAErrorReportingMixin(object):
    """
    Form mixin that reports form errors to Google Analytics with events
    """
    ga_endpoint_base = 'https://ssl.google-analytics.com/'
    # NB: non-ssl version is http://www.google-analytics.com/
    ga_tracking_id = None
    ga_client_id = None
    ga_event_category = None
    ga_batch_hits = True

    def is_valid(self):
        """
        Error reporting is triggered when a form is checked for validity
        """
        is_valid = super(GAErrorReportingMixin, self).is_valid()
        if self.is_bound and not is_valid:
            try:
                self.report_errors_to_ga(self.errors)
            except:
                logger.exception('Failed to report form errors to Google Analytics')
        return is_valid

    def get_ga_single_endpoint(self):
        """
        URL for collecting a single hit
        """
        return urljoin(self.ga_endpoint_base, 'collect')

    def get_ga_batch_endpoint(self):
        """
        URL for collecting multiple hits
        """
        return urljoin(self.ga_endpoint_base, 'batch')
{'fraction_non_alphanumeric': 0.09093319194061505, 'fraction_numerical': 0.0010604453870625664, 'mean_word_length': 2.69539666993144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '4787629', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1069, 'n_words': 318}