<commit_before>import os
import sys
from types import ModuleType
from contextlib import contextmanager


@contextmanager
def path_context(path):
    """
    A context manager that allows you to enter a given path
    into sys.path and then safely guarantee that the path
    was removed from sys.path once the block exits.
    """
    sys.path.insert(1, path)
    try:
        yield
    finally:
        sys.path.remove(path)


def filename_to_module(path):
    """
    Convert a filename to a module. Replaces the path
    separators with dots and then removes the '.py' in the
    end.
    """
    return path[:-3].replace(os.path.sep, '.')


def import_module(root, package):
    """
    Import a module and then return the module object itself.
    Note that this function does some ``getattr`` behind the
    scenes in order to fetch the "real" module, and not the
    root module.

    :param package: The module to import.
    """
    module_name = '%s.%s' % (root, package)
    module = __import__(module_name, {}, {})
    for item in package.split('.'):
        module = getattr(module, item)
        if not isinstance(module, ModuleType):
            raise ImportError("No module named '%s'" % (item))
    return module


class Loader(object):
    """
    Create a loader object with the given finder class and
    options.
<commit_msg>let ImportError handle invalid imports for us<commit_after>import os
import sys
from types import ModuleType
from contextlib import contextmanager


@contextmanager
def path_context(path):
    """
    A context manager that allows you to enter a given path
    into sys.path and then safely guarantee that the path
    was removed from sys.path once the block exits.
    """
    sys.path.insert(1, path)
    try:
        yield
    finally:
        sys.path.remove(path)


def filename_to_module(path):
    """
    Convert a filename to a module. Replaces the path
    separators with dots and then removes the '.py' in the
    end.
    """
    return path[:-3].replace(os.path.sep, '.')


def import_module(root, package):
    """
    Import a module and then return the module object itself.
    Note that this function does some ``getattr`` behind the
    scenes in order to fetch the "real" module, and not the
    root module.

    :param package: The module to import.
    """
    module_name = '%s.%s' % (root, package)
    module = __import__(module_name, {}, {})
    for item in package.split('.'):
        module = getattr(module, item, None)
        if not isinstance(module, ModuleType):
            raise ImportError("No module named '%s'" % (item))
    return module


class Loader(object):
    """
    Create a loader object with the given finder class and
    options.

{'fraction_non_alphanumeric': 0.09340659340659341, 'fraction_numerical': 0.0014652014652014652, 'mean_word_length': 2.814245810055866, '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': '4696864', 'n_tokens_mistral': 774, 'n_tokens_neox': 759, 'n_words': 311}