def merge_dict(a, b):
    """Perform a merge of dictionaries a and b

    Any subdictionaries will be recursively merged.
    Any leaf elements in the form of a list or scalar will use the value from a
    """
    d = {}
    for k, v in a.items():
        if k not in b:
            d[k] = v
        elif isinstance(v, dict) and isinstance(b[k], dict):
            d[k] = merge_dict(v, b[k])
    for k, v in b.items():
        if k not in d:
            d[k] = v
    return d