I have a function like so:
def ease_bounce_out(t, bounces=None):
if bounces is None:
bounces = [4 / 11, 6 / 11, 8 / 11, 3 / 4, 9 / 11, 10 / 11, 15 / 16, 21 / 22, 63 / 64]
bounces.insert(0, 1 / bounces[0] / bounces[0])
if t < bounces[1]:
return bounces[0] * t * t
else:
for i in range(3, len(bounces) - 2, 3):
if t < bounces[i]:
t -= bounces[i - 1]
return bounces[0] * t * t + bounces[i + 1]
t -= bounces[len(bounces) - 2]
return bounces[0] * t * t + bounces[len(bounces) - 1]
and I would like to compact it all down into 1 string so that I can use the eval()
function to get an output for any value of t
. I have an example of an easier function:
def ease_poly(t, power=2):
t *= 2
if t < 1:
return t ** power
else:
return 2 - ((2 - t) ** power)
would become:
def ease_poly(power=2):
return f"(t * 2) ** {power} if (t * 2) < 1 else 2 - ((2 - (t * 2)) ** {power})"
This way, I could use this string and evaluate the function for any value of t
by simply doing:
ease = ease_poly(power=3)
t = 0.4 # 0 <= t <= 1
print(eval(ease))
Now to get started with my question, it doesn't actually have to be 1 line, this is what I've been thinking of:
def ease_bounce_out(bounces=None):
if bounces is None:
bounces = [4 / 11, 6 / 11, 8 / 11, 3 / 4, 9 / 11, 10 / 11, 15 / 16, 21 / 22, 63 / 64]
return # some code here that compiles the rest into a string
A small tip,
bounces[ -1 ] = Last item
bounces[ -2 ] = Last second item
Don't use bounces[ len(bounces) - 1 ]
Answer: You can't have that string answer with eval. Because your function is making decisions based on t. You have to pass t.
If evaluating for all t is your primary concern then other way could help. Forget about eval. Don't pass t but use t in function then it will look up in global scope and uses t in that scope.
Example:
This function requires t to be passed.
def mathuer(t, b = 23 ):
x = t
if b > t:
x = t
return x
This function uses global t.
def mathuer(b = 23 ):
x = t
if b > t:
x = t
return x
This is how it works,
t = 34
obj = mathuer() // Uses t defined above