<commit_before>"""
Methods related to notifications.
"""

import warnings

from cupyx import _ufunc_config


def _init_warnings():
    FallbackWarning = type('FallbackWarning', (Warning,), {})
    warnings.simplefilter(action='always', category=FallbackWarning)
    return FallbackWarning


def _dispatch_notification(func):
    """
    Dispatch notifications using appropriate dispatch type.
    """

    dispatch_type = _ufunc_config.get_config_fallback_mode()
    callback_func = _ufunc_config.get_fallback_mode_callback()

    msg = "'{}' method not in cupy, falling back to '{}.{}'".format(
        func.__name__, func.__module__, func.__name__)

    raise_msg = "'{}' method not found in cupy".format(
        func.__name__)

    if dispatch_type == 'print':
        print("Warning: {}".format(msg))

    elif dispatch_type == 'warn':
        warnings.warn(msg, FallbackWarning, stacklevel=3)

    elif dispatch_type == 'ignore':
        pass

    elif dispatch_type == 'log':
        if hasattr(callback_func, 'write') and callable(callback_func.write):
            callback_func.write(msg)
        else:
            raise ValueError(
                "Callback object must have a callable 'write' method, " +
                "if it is to be used for 'log'")

    elif dispatch_type == 'raise':
        raise AttributeError(raise_msg)

    elif dispatch_type == 'call':
        if callable(callback_func):
            callback_func(func)<commit_msg>Handle methods not having __module__<commit_after>"""
Methods related to notifications.
"""

import warnings

from cupyx import _ufunc_config


def _init_warnings():
    FallbackWarning = type('FallbackWarning', (Warning,), {})
    warnings.simplefilter(action='always', category=FallbackWarning)
    return FallbackWarning


def _dispatch_notification(func):
    """
    Dispatch notifications using appropriate dispatch type.
    """

    dispatch_type = _ufunc_config.get_config_fallback_mode()
    callback_func = _ufunc_config.get_fallback_mode_callback()

    _module = hasattr(func, '__module__')
    _name = hasattr(func, '__name__')

    if _name and _module:
        msg = "'{}' method not in cupy, falling back to '{}.{}'".format(
            func.__name__, func.__module__, func.__name__)
    elif _name:
        msg = "'{}' method not in cupy, ".format(func.__name__)
        msg += "falling back to numpy implementation of '{}'".format(
            func.__name__)
    else:
        msg = "This method is not available in cupy, "
        msg += "falling back to numpy"

    if _name:
        raise_msg = "'{}' method not found in cupy".format(func.__name__)
    else:
        raise_msg = "This method is not available in cupy"

    if dispatch_type == 'print':
        print("Warning: {}".format(msg))

    elif dispatch_type == 'warn':
        warnings.warn(msg, FallbackWarning, stacklevel=3)

    elif dispatch_type == 'ignore':
        pass

    elif dispatch_type == 'log':
        if hasattr(callback_func, 'write') and callable(callback_func.write):
            callback_func.write(msg)
        else:
            raise ValueError(
                "Callback object must have a callable 'write' method, " +
                "if it is to be used for 'log'")

    elif dispatch_type == 'raise':
        raise AttributeError(raise_msg)

    elif dispatch_type == 'call':
        if callable(callback_func):
            callback_func(func)
{'fraction_non_alphanumeric': 0.11094804813619019, 'fraction_numerical': 0.000587026709715292, 'mean_word_length': 2.9172413793103447, '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': '1347444', 'n_tokens_mistral': 989, 'n_tokens_neox': 960, 'n_words': 264}