<commit_before>
"""
   runtest - Numerically tolerant test library

   Author:
      <NAME>

   License:
      BSD-3
      https://github.com/bast/runtest/blob/master/LICENSE

   Documentation:
      http://runtest.readthedocs.org

   Source:
      https://github.com/bast/runtest
"""

import re
import os
import sys
import subprocess
import shlex
import shutil
import string
from optparse import OptionParser


__version__ = '1.3.4'  # http://semver.org


class FilterKeywordError(Exception):
    pass


class TestFailedError(Exception):
    pass


class BadFilterError(Exception):
    pass


class AcceptedError(Exception):
    pass


class SubprocessError(Exception):
    pass

# ------------------------------------------------------------------------------


def is_float(x):
    return isinstance(x, float)

# ------------------------------------------------------------------------------


def is_int(n):
    return isinstance(n, int)

# ------------------------------------------------------------------------------


def compare_tuple(f, tup):

    x, x_ref = tup

    if f.ignore_sign:
        # if ignore sign take absolute values
        x = abs(x)
        x_ref = abs(x_ref)

    if is_int(x) and is_int(x_ref):
        # we compare integers
        return x == x_ref
    else:
        # we compare floats
        if (abs(x_ref) > f.ignore_below) and (abs(x_ref) < f.ignore_above):
            # calculate relative error only for
            # significant ('nonzero') numbers
            error = x - x_ref
            if f.tolerance_is_relative:
                error /= x_ref
            return abs(error) <= f.tolerance
        else:
            return True

# ------------------------------------------------------------------------------


def compare_lists(f, l1, l2):
    """
    Input:
        - f -- filter task
        - l1 -- list of numbers
        - l2 -- another list of numbers

    Returns:
        - res -- list that contains ones (pass) or zeros (failures)
                 l1, l2, and res have same length

    Raises:
        - FilterKeywordError
    """

    assert len(l1) == len(l2)

    # FIXME this should happen further up
    # if we have any float in there then tolerance must be set
    if not f.tolerance_is_set and (any(map(is_float, l1)) or any(map(is_float, l2))):
        raise FilterKeywordError('ERROR: for floats you have to specify either rel_tolerance or abs_tolerance\n')

    return map(lambda t: compare_tuple(f, t), zip(l1, l2))

# ------------------------------------------------------------------------------


def extract_numbers(f, text):
    """
    Input:
        - f -- filter task<commit_msg>refactor<commit_after>
"""
   runtest - Numerically tolerant test library

   Author:
      <NAME>

   License:
      BSD-3
      https://github.com/bast/runtest/blob/master/LICENSE

   Documentation:
      http://runtest.readthedocs.org

   Source:
      https://github.com/bast/runtest
"""

import re
import os
import sys
import subprocess
import shlex
import shutil
import string
from optparse import OptionParser


__version__ = '1.3.5'  # http://semver.org


class FilterKeywordError(Exception):
    pass


class TestFailedError(Exception):
    pass


class BadFilterError(Exception):
    pass


class AcceptedError(Exception):
    pass


class SubprocessError(Exception):
    pass

# ------------------------------------------------------------------------------


def is_float(x):
    return isinstance(x, float)

# ------------------------------------------------------------------------------


def is_int(n):
    return isinstance(n, int)

# ------------------------------------------------------------------------------


def tuple_matches(f, tup):

    x, x_ref = tup

    if f.ignore_sign:
        # if ignore sign take absolute values
        x = abs(x)
        x_ref = abs(x_ref)

    if is_int(x) and is_int(x_ref):
        return x == x_ref

    if abs(x_ref) < f.ignore_below:
        return True

    if abs(x_ref) > f.ignore_above:
        return True

    error = x - x_ref
    if f.tolerance_is_relative:
        error /= x_ref
    return abs(error) <= f.tolerance

# ------------------------------------------------------------------------------


def compare_lists(f, l1, l2):
    """
    Input:
        - f -- filter task
        - l1 -- list of numbers
        - l2 -- another list of numbers

    Returns:
        - res -- list that contains ones (pass) or zeros (failures)
                 l1, l2, and res have same length

    Raises:
        - FilterKeywordError
    """

    # FIXME this will move up once the caller is unit-tested
    # if we have any float in there then tolerance must be set
    if not f.tolerance_is_set and (any(map(is_float, l1)) or any(map(is_float, l2))):
        raise FilterKeywordError('ERROR: for floats you have to specify either rel_tolerance or abs_tolerance\n')
    return map(lambda t: tuple_matches(f, t), zip(l1, l2))

# ------------------------------------------------------------------------------


def extract_numbers(f, text):
    """
    Input:
        - f -- filter task
{'fraction_non_alphanumeric': 0.23455579525397136, 'fraction_numerical': 0.0058835065699156695, 'mean_word_length': 1.427415516420752, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6764050', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1398, 'n_words': 410}