<commit_before>from __future__ import absolute_import
from __future__ import unicode_literals

from requests.exceptions import HTTPError, ConnectionError
from pypuppetdb.errors import EmptyResponseError, ExperimentalDisabledError

from flask import abort, flash

def get_or_abort(func, *args, **kwargs):
    """Execute the function with its arguments and handle the possible
    errors that might occur.

    In this case, if we get an exception we simply abort the request.
    """
    try:
        return func(*args, **kwargs)
    except HTTPError, e:
        abort(e.response.status_code)
    except ConnectionError:
        abort(500)
    except ExperimentalDisabledError:
        abort(412)
    except EmptyResponseError:
        abort(204)


def ten_reports(reports):
    """Helper to yield the first then reports from the reports generator.

    This is an ugly solution at best...
    """
    for count, report in enumerate(reports):
        if count == 10:
            raise StopIteration
        yield report


def yield_or_stop(generator):
    """Similar in intent to get_or_abort this helper will iterate over our
    generators and handle certain errors.

    Since this is also used in streaming responses where we can't just abort
    a request we always yield empty and then raise StopIteration.
    """
    while True:
        try:
            yield next(generator)
        except StopIteration:
            raise
        except ExperimentalDisabledError:
            yield
            raise StopIteration
        except EmptyResponseError:
            yield
            raise StopIteration
        except ConnectionError:
            yield
            raise StopIteration
        except HTTPError:
            yield
            raise StopIteration<commit_msg>utils: Group the exceptions in yield_or_abort.<commit_after>from __future__ import absolute_import
from __future__ import unicode_literals

from requests.exceptions import HTTPError, ConnectionError
from pypuppetdb.errors import EmptyResponseError, ExperimentalDisabledError

from flask import abort, flash

def get_or_abort(func, *args, **kwargs):
    """Execute the function with its arguments and handle the possible
    errors that might occur.

    In this case, if we get an exception we simply abort the request.
    """
    try:
        return func(*args, **kwargs)
    except HTTPError, e:
        abort(e.response.status_code)
    except ConnectionError:
        abort(500)
    except ExperimentalDisabledError:
        abort(412)
    except EmptyResponseError:
        abort(204)


def ten_reports(reports):
    """Helper to yield the first then reports from the reports generator.

    This is an ugly solution at best...
    """
    for count, report in enumerate(reports):
        if count == 10:
            raise StopIteration
        yield report


def yield_or_stop(generator):
    """Similar in intent to get_or_abort this helper will iterate over our
    generators and handle certain errors.

    Since this is also used in streaming responses where we can't just abort
    a request we raise StopIteration.
    """
    while True:
        try:
            yield next(generator)
        except StopIteration:
            raise
        except (ExperimentalDisabledError, EmptyResponseError,
            ConnectionError, HTTPError):
            raise StopIteration
{'fraction_non_alphanumeric': 0.05357142857142857, 'fraction_numerical': 0.006547619047619048, 'mean_word_length': 2.689352360043908, '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': '2184433', 'n_tokens_mistral': 862, 'n_tokens_neox': 830, 'n_words': 330}