def factoring_visitor(state, primes):
    f, lpart, pstack = state
    factoring = []
    for i in range(lpart + 1):
        factor = 1
        for ps in pstack[f[i]: f[i + 1]]:
            if ps.v > 0:
                factor *= primes[ps.c] ** ps.v
        factoring.append(factor)
    return factoring
Use with multiset_partitions_taocp to enumerate the ways a
dataset/ETHPy150Open sympy/sympy/sympy/utilities/enumerative.py/factoring_visitor