import glob


def _parse_batch_params(params):
    """Transform batch parameter strings into lists of tuples.

    Parameters
    ----------
    params : list of str
        The string should have the form "key=val1,val2,val3".

    Returns
    -------
    A generator that, for each key, yields a list of key-value tuple pairs.
    """
    def maybe_glob(x):
        return glob.glob(x) if glob.has_magic(x) else [x]

    seen_keys = set()
    for param in params:
        if "=" not in param:
            raise ValueError(
                "param value should be formatted as 'key=value,...'")
        key, value_str = param.split("=", maxsplit=1)
        if key in seen_keys:
            raise ValueError("Key '{}' was given more than once".format(key))
        seen_keys.add(key)
        yield [(key, v)
               for v_unexpanded in value_str.split(",")
               for v in maybe_glob(v_unexpanded)]