import importlib


def import_class(path):
    """Import a class from pattern like : path.to.the.Class

    Tests:
        >>> import_class('django.db.models.Model')
        <class 'django.db.models.base.Model'>

        >>> import_class('wrong.module.Class')
        Traceback (most recent call last):
        ImportError: No module named wrong.module

        >>> import_class('algolia.utils.WrongClass')
        Traceback (most recent call last):
        ImportError: The Python module 'algolia.utils' has no 'WrongClass' class.

        >>> import_class()
        Traceback (most recent call last):
        TypeError: import_class() takes exactly 1 argument (0 given)
    """

    path_bits = path.split('.')
    class_name = path_bits.pop()
    module_path = '.'.join(path_bits)
    module_itself = importlib.import_module(module_path)

    if not hasattr(module_itself, class_name):
        raise ImportError("The Python module '%s' has no '%s' class." % (module_path, class_name))

    return getattr(module_itself, class_name)