<commit_before>"""
kyoukai.util
~~~~~~~~~~~~

Misc utilities for usage inside the framework.
"""
from werkzeug.wrappers import Response


def wrap_response(args, response_class: Response=Response) -> Response:
    """
    Wrap up a response, if applicable.
    This allows Flask-like `return ""`.

    :param args: The arguments that are being wrapped.
    :param response_class: The Response class that is being used.
    """
    if not args:
        # Return a 204 NO CONTENT.
        return response_class("", status=204)

    if isinstance(args, tuple):
        # We enforce ``tuple`` here instead of any iterable.
        if len(args) == 1:
            # Only body, use 200 for the response code.
            return response_class(args[0], status=200)

        if len(args) == 2:
            # Body and status code.
            return response_class(args[0], status=args[1])

        if len(args) == 3:
            # Body, status code, and headers.
            return response_class(args[0], status=args[1], headers=args[2])

        raise TypeError("Cannot return more than 3 arguments from a view")

    if isinstance(args, response_class):
        # Return the bare response, unmodified.
        return args

    # Otherwise, wrap it in a response.
    return response_class(args)<commit_msg>Stringify args before passing them to the Response class.<commit_after>"""
kyoukai.util
~~~~~~~~~~~~

Misc utilities for usage inside the framework.
"""
from werkzeug.wrappers import Response


def wrap_response(args, response_class: Response=Response) -> Response:
    """
    Wrap up a response, if applicable.
    This allows Flask-like `return ""`.

    :param args: The arguments that are being wrapped.
    :param response_class: The Response class that is being used.
    """
    if not args:
        # Return a 204 NO CONTENT.
        return response_class("", status=204)

    if isinstance(args, tuple):
        # We enforce ``tuple`` here instead of any iterable.
        if len(args) == 1:
            # Only body, use 200 for the response code.
            body = str(args[0])
            return response_class(args[0], status=200)

        if len(args) == 2:
            # Body and status code.
            body = str(args[0])
            return response_class(args[0], status=args[1])

        if len(args) == 3:
            # Body, status code, and headers.
            body = str(args[0])
            return response_class(args[0], status=args[1], headers=args[2])

        raise TypeError("Cannot return more than 3 arguments from a view")

    if isinstance(args, response_class):
        # Return the bare response, unmodified.
        return args

    # Otherwise, wrap it in a response.
    args = str(args)
    return response_class(args)
{'fraction_non_alphanumeric': 0.10612097066280334, 'fraction_numerical': 0.017022817819630567, 'mean_word_length': 2.5319693094629154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '714018', 'n_tokens_mistral': 798, 'n_tokens_neox': 748, 'n_words': 277}