Anay Bose Anay Bose - 13 days ago 9
Python Question

python lambda if/else condition failed : 'int' and 'function' conflicts

I'm trying to use in-line, lambda function to perform some if/else conditions, where I'm getting

"unsupported operand type(s) for +: 'int' and 'function'"
error. Here I'm trying to set set_duration to 1.5 unless its first and last array iterations. Can you please take a look and offer any hints. Looking forward.

for idx, string in enumerate(lines):


duration = 10
clips = []

clips.append(ImageClip(os.path.join(folder,"gradient.png"))
.set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5)
)

clips.append(ImageClip(os.path.join(folder,"image-{0}.png"))
.resize(width=800)
.set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5)
.margin(right=60, opacity=0)
.set_pos(("right","center"))
)

clips.append(ImageClip(os.path.join(folder, "big-append.png"))
.resize(width=900)
.margin(left=60,opacity=0)
.set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5)
.set_pos(("left", "center"))
)



// rest of the code...


FullTraceback

Traceback (most recent call last):
File "C:\vhosts\phpsols\pymovie\FORMAT-4\three.py", line 90, in <module>
.set_duration(lambda idx: 3 if (idx == 0 and lines[len(lines) - 1]) else 1.5)
File "<decorator-gen-29>", line 2, in set_duration
File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 29, in apply_to_mask
newclip = f(clip, *a, **k)
File "<decorator-gen-28>", line 2, in set_duration
File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 41, in apply_to_audio
newclip = f(clip, *a, **k)
File "<decorator-gen-27>", line 2, in set_duration
File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "<decorator-gen-26>", line 2, in set_duration
File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 14, in outplace
f(newclip, *a, **k)
File "C:\anaconda32\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\Clip.py", line 288, in set_duration
self.end = None if (t is None) else (self.start + t)
TypeError: unsupported operand type(s) for +: 'int' and 'function'
Process terminated with an exit code of 1

Answer

You don't need a lambda. Just set the duration directly. The value is already recalculated every loop iteration.

             .set_duration(3.0 if idx == 0 and lines[-1] else 1.5)
Comments