import typing
import importlib


def import_object(path: str, default=None) -> typing.Any:
    """Import object from path.

    Paths have the format ``module_path:object_path``.

    The ``default`` value is returned when ``path`` is ``None``. This is
    a convenience for passing in settings that may be ``None``.

    Examples::

        >>> import_object('dijkstar.graph:Graph')
        <class 'dijkstar.graph.Graph'>
        >>> import_object('dijkstar.graph:Graph.load')
        <bound method Graph.load of <class 'dijkstar.graph.Graph'>>

    """
    if path is None:
        return default
    module_path, object_path = path.split(":")
    module = importlib.import_module(module_path)
    names = object_path.split(".")
    obj = module
    for name in names:
        obj = getattr(obj, name)
    return obj