def set_chain(base, key_chain, value):
    """Sets a value in a chain of nested dictionaries. """
    if base is None:
        base = {}
    cur = base
    n = len(key_chain)
    for i, key in enumerate(key_chain):
        if i + 1 < n:
            if key not in cur:
                cur[key] = {}
                cur = cur[key]
        else:
            cur[key] = value
    return base