def dict_to_string(d, order=[], exclude=[], entry_sep="_", kv_sep=""):
    """
    Turn a dictionary into a string by concatenating all key and values
    together, separated by specified delimiters.


    d: a dictionary. It is expected that key and values are simple literals
        e.g., strings, float, integers, not structured objects.
    order: a list of keys in the dictionary. Keys with low indices
        appear first in the output.
    exclude: a list of keys to exclude
    entry_sep: separator between key-value entries (a string)
    kv_sep: separator between a key and its value
    """
    # dict_keys = dict( (ordered_keys[i], i) for i in range(len(ordered_keys)) )
    keys = set(d.keys()) - set(exclude)
    list_out = []
    for k in order:
        if k in keys:
            entry = k + kv_sep + str(d[k])
            list_out.append(entry)
            keys.discard(k)
    # process the rest of the keys
    for k in sorted(list(keys)):
        entry = k + kv_sep + str(d[k])
        list_out.append(entry)

    s = entry_sep.join(list_out)
    return s