human
Complete this python program:
def _TR56(rv, f, g, h, max, pow):
assistant
#Here is the Python code you requested.

    """Helper for TR5 and TR6 to replace f**2 with h(g**2)
    Options
    max :   controls size of exponent that can appear on f
    e.g. if max=4 then f**4 will be changed to h(g**2)**2.
    pow :   controls whether the exponent must be a perfect power of 2
    e.g. if pow=True (and max >= 6) then f**6 will not be changed
    but f**8 will be changed to h(g**2)**4
    >>> from sympy.simplify.fu import _TR56 as T
    >>> from sympy.abc import x
    >>> from sympy import sin, cos
    >>> h = lambda x: 1 - x
    >>> T(sin(x)**3, sin, cos, h, 4, False)
    sin(x)**3
    >>> T(sin(x)**6, sin, cos, h, 6, False)
    (-cos(x)**2 + 1)**3
    >>> T(sin(x)**6, sin, cos, h, 6, True)
    sin(x)**6
    >>> T(sin(x)**8, sin, cos, h, 10, True)
    (-cos(x)**2 + 1)**4
    """
    def _f(rv):
        if (not (rv.is_Pow and (rv.base.func == f))):
            return rv
        if ((rv.exp < 0) == True):
            return rv
        if ((rv.exp > max) == True):
            return rv
        if (rv.exp == 2):
            return h((g(rv.base.args[0]) ** 2))
        else:
            if (rv.exp == 4):
                e = 2
            elif (not pow):
                if (rv.exp % 2):
                    return rv
                e = (rv.exp // 2)
            else:
                p = perfect_power(rv.exp)
                if (not p):
                    return rv
                e = (rv.exp // 2)
            return (h((g(rv.base.args[0]) ** 2)) ** e)
    return bottom_up(rv, _f)