<commit_before>import math
import sys

import py

from _pytest.compat import isclass, izip
from _pytest.runner import fail
import _pytest._code

# builtin pytest.approx helper


class ApproxBase(object):
    """
    Provide shared utilities for making approximate comparisons between numbers
    or sequences of numbers.
    """

    def __init__(self, expected, rel=None, abs=None, nan_ok=False):
        self.expected = expected
        self.abs = abs
        self.rel = rel
        self.nan_ok = nan_ok

    def __repr__(self):
        raise NotImplementedError

    def __eq__(self, actual):
        return all(
            a == self._approx_scalar(x)
            for a, x in self._yield_comparisons(actual))

    __hash__ = None

    def __ne__(self, actual):
        return not (actual == self)

    def _approx_scalar(self, x):
        return ApproxScalar(x, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)

    def _yield_comparisons(self, actual):
        """
        Yield all the pairs of numbers to be compared.  This is used to
        implement the `__eq__` method.
        """
        raise NotImplementedError


class ApproxNumpy(ApproxBase):
    """
    Perform approximate comparisons for numpy arrays.
    """

    # Tell numpy to use our `__eq__` operator instead of its.
    __array_priority__ = 100

    def __repr__(self):
        # It might be nice to rewrite this function to account for the
        # shape of the array...
        return "approx({0!r})".format(list(
            self._approx_scalar(x) for x in self.expected))

    def __eq__(self, actual):
        import numpy as np

        try:
            actual = np.asarray(actual)
        except:
            raise ValueError("cannot compare '{0}' to numpy.ndarray".format(actual))

        if actual.shape != self.expected.shape:
            return False

        return ApproxBase.__eq__(self, actual)

    def _yield_comparisons(self, actual):<commit_msg>Raise TypeError for types that can't be compared to arrays.<commit_after>import math
import sys

import py

from _pytest.compat import isclass, izip
from _pytest.runner import fail
import _pytest._code

# builtin pytest.approx helper


class ApproxBase(object):
    """
    Provide shared utilities for making approximate comparisons between numbers
    or sequences of numbers.
    """

    def __init__(self, expected, rel=None, abs=None, nan_ok=False):
        self.expected = expected
        self.abs = abs
        self.rel = rel
        self.nan_ok = nan_ok

    def __repr__(self):
        raise NotImplementedError

    def __eq__(self, actual):
        return all(
            a == self._approx_scalar(x)
            for a, x in self._yield_comparisons(actual))

    __hash__ = None

    def __ne__(self, actual):
        return not (actual == self)

    def _approx_scalar(self, x):
        return ApproxScalar(x, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)

    def _yield_comparisons(self, actual):
        """
        Yield all the pairs of numbers to be compared.  This is used to
        implement the `__eq__` method.
        """
        raise NotImplementedError


class ApproxNumpy(ApproxBase):
    """
    Perform approximate comparisons for numpy arrays.
    """

    # Tell numpy to use our `__eq__` operator instead of its.
    __array_priority__ = 100

    def __repr__(self):
        # It might be nice to rewrite this function to account for the
        # shape of the array...
        return "approx({0!r})".format(list(
            self._approx_scalar(x) for x in self.expected))

    def __eq__(self, actual):
        import numpy as np

        try:
            actual = np.asarray(actual)
        except:
            raise TypeError("cannot compare '{0}' to numpy.ndarray".format(actual))

        if actual.shape != self.expected.shape:
            return False

        return ApproxBase.__eq__(self, actual)

    def _yield_comparisons(self, actual):
{'fraction_non_alphanumeric': 0.08602697887503181, 'fraction_numerical': 0.0025451768897938407, 'mean_word_length': 2.4443470639789657, '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': '2445981', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1124, 'n_words': 362}