def split_keys(adict, separator='.', maxsplit=None):
    """
    This function is best explained by an example::

        d['some.very.long.key'] = 'value'
        split_keys(d)['some']['very']['long']['key']  # 'value'


    It splits all keys in `adict` by `separator`, then for each part of the
    split key but the last it inserts an inner dictionary into the result.

    .. warning:: The function assumes that all keys in `adict` are strings.

    If `maxsplit` is passed, then keys would be split at most `maxsplit` times.

    .. note:: If `maxsplit` is `N` then the keys would be split on at most
        `N+1` parts.
    """
    result = {}
    if maxsplit:
        split = lambda x: x.split(separator, maxsplit)
    else:
        split = lambda x: x.split(separator)
    for key, value in adict.items():
        parts = split(key)
        container = result
        for part in parts[:-1]:
            container = container.setdefault(part, dict())
        container[parts[-1]] = value
    return result
