<commit_before>"""
My own variation on function-specific inspect-like features.
"""

# Author: <NAME> <gael dot varoquaux at normalesup dot org>
# Copyright (c) 2009 <NAME>
# License: BSD Style, 3 clauses.

from itertools import islice
import inspect
import warnings
import os

from ._compat import _basestring


def get_func_code(func):
    """ Attempts to retrieve a reliable function code hash.

        The reason we don't use inspect.getsource is that it caches the
        source, whereas we want this to be modified on the fly when the
        function is modified.

        Returns
        -------
        func_code: string
            The function code
        source_file: string
            The path to the file in which the function is defined.
        first_line: int
            The first line of the code in the source file.

        Notes
        ------
        This function does a bit more magic than inspect, and is thus
        more robust.
    """
    source_file = None
    try:
        if func.__name__ == '<lambda>':
            # On recent Python version, inspect is reliable with lambda
            source_file = func.__code__.co_filename
            return ''.join(inspect.getsourcelines(func)[0]), source_file, 1
        # Try to retrieve the source code.
        code = func.__code__
        source_file = code.co_filename
        with open(source_file) as source_file_obj:
            first_line = code.co_firstlineno
            # All the lines after the function definition:
            source_lines = list(islice(source_file_obj, first_line - 1, None))
        return ''.join(inspect.getblock(source_lines)), source_file, first_line
    except:
        # If the source code fails, we use the hash. This is fragile and
        # might change from one session to another.
        if hasattr(func, '__code__'):
            # Python 3.X
            return str(func.__code__.__hash__()), source_file, -1
        else:
            # Weird objects like numpy ufunc don't have __code__
            # This is fragile, as quite often the id of the object is
            # in the repr, so it might not persist accross sessions,
            # however it will work for ufuncs.
            return repr(func), source_file, -1


def _clean_win_chars(string):
    "Windows cannot encode some characters in filenames"
    import urllib
    if hasattr(urllib, 'quote'):
        quote = urllib.quote
    else:
        # In Python 3, quote is elsewhere
        quote = urllib.parse.quote
    for char in ('<', '>', '!', ':', '\\'):
        string = string.replace(char, quote(char))
    return string


def get_func_name(func, resolv_alias=True, win_characters=True):
    """ Return the function import path (as a list of module names), and
        a name for the function.

        Parameters<commit_msg>typos in joblib/func_inspect.py<commit_after>"""
My own variation on function-specific inspect-like features.
"""

# Author: <NAME> <gael dot varoquaux at normalesup dot org>
# Copyright (c) 2009 <NAME>
# License: BSD Style, 3 clauses.

from itertools import islice
import inspect
import warnings
import os

from ._compat import _basestring


def get_func_code(func):
    """ Attempts to retrieve a reliable function code hash.

        The reason we don't use inspect.getsource is that it caches the
        source, whereas we want this to be modified on the fly when the
        function is modified.

        Returns
        -------
        func_code: string
            The function code
        source_file: string
            The path to the file in which the function is defined.
        first_line: int
            The first line of the code in the source file.

        Notes
        ------
        This function does a bit more magic than inspect, and is thus
        more robust.
    """
    source_file = None
    try:
        if func.__name__ == '<lambda>':
            # On recent Python version, inspect is reliable with lambda
            source_file = func.__code__.co_filename
            return ''.join(inspect.getsourcelines(func)[0]), source_file, 1
        # Try to retrieve the source code.
        code = func.__code__
        source_file = code.co_filename
        with open(source_file) as source_file_obj:
            first_line = code.co_firstlineno
            # All the lines after the function definition:
            source_lines = list(islice(source_file_obj, first_line - 1, None))
        return ''.join(inspect.getblock(source_lines)), source_file, first_line
    except:
        # If the source code fails, we use the hash. This is fragile and
        # might change from one session to another.
        if hasattr(func, '__code__'):
            # Python 3.X
            return str(func.__code__.__hash__()), source_file, -1
        else:
            # Weird objects like numpy ufunc don't have __code__
            # This is fragile, as quite often the id of the object is
            # in the repr, so it might not persist across sessions,
            # however it will work for ufuncs.
            return repr(func), source_file, -1


def _clean_win_chars(string):
    "Windows cannot encode some characters in filenames"
    import urllib
    if hasattr(urllib, 'quote'):
        quote = urllib.quote
    else:
        # In Python 3, quote is elsewhere
        quote = urllib.parse.quote
    for char in ('<', '>', '!', ':', '\\'):
        string = string.replace(char, quote(char))
    return string


def get_func_name(func, resolv_alias=True, win_characters=True):
    """ Return the function import path (as a list of module names), and
        a name for the function.

        Parameters
{'fraction_non_alphanumeric': 0.07857016457264202, 'fraction_numerical': 0.004247035922845514, 'mean_word_length': 2.257636887608069, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '6163412', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1522, 'n_words': 607}