def highlevel_graded(f,d): return Polynomial(f.set().intersect(all_monomials_of_degree_d(d,f.varsAsMonomial())))This solution build on the fast intersection algorithm and decomposes the task in just two set operations, which is very good.
However it can be quite inefficient, when f has many variables. This can increase the number of steps in the intersection algorithm (which takes with high probability the else branch of the second argument in each step).