import importlib


def get_function(function_path):
    """ find and import a function dynamically.

    Parameters
    ----------
    function_path : string
        A string with the path to the function. The expected format is:

            - `<function>`
            - `<packages>.<module>.<function>`
            - `<packages>.<module>.<class>.<method>`


    """
    try:
        return importlib.import_module(function_path)
    except ImportError:
        components = function_path.split('.')
        try:
            module = importlib.import_module('.'.join(components[:-1]))
        except ImportError:
            module = importlib.import_module('.'.join(components[:-2]))
            class_object = getattr(module, components[-2])
            return getattr(class_object, components[-1])
        else:
            return getattr(module, components[-1])