'Returns level associated with the given generator.'

def _gen_to_level(f, gen):

    if isinstance(gen, int):
        length = len(f.gens)
        if ((- length) <= gen < length):
            if (gen < 0):
                return (length + gen)
            else:
                return gen
        else:
            raise PolynomialError(('-%s    <=    gen    <    %s    expected,    got    %s' % (length, length, gen)))
    else:
        try:
            return f.gens.index(sympify(gen))
        except ValueError:
            raise PolynomialError(('a    valid    generator    expected,    got    %s' % gen))
