from typing import OrderedDict


def as_ordered_dict(v, ctx, value_transform=None, additional_params=None):
    """Expects v to be a list containing single key/value pair entries, and
    transforms it into an ordered dictionary."""
    if not isinstance(v, list):
        raise Exception("Expected list of key/value pairs, but got: %s (%s)" % (type(v), ctx))
    i = 0
    result = OrderedDict()
    if additional_params is None:
        additional_params = dict()
    for item in v:
        if not isinstance(item, dict) or len(item) == 0:
            raise Exception("Expected item %d to be a single key/value pair (%s)" % (i, ctx))
        k, = item.keys()
        v, = item.values()
        result[k] = value_transform(v, "item %d, %s" % (i, ctx), **additional_params) if callable(value_transform) else v
        i += 1
    return result