Skip to content

Commit

Permalink
Fix decorator performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzczb committed Jan 13, 2024
1 parent 858bb81 commit b9b5ecc
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions moviepy/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,28 @@ def audio_video_fx(func, clip, *args, **kwargs):
return func(clip, *args, **kwargs)


def preprocess_args(fun, varnames):
"""Applies fun to variables in varnames before launching the function."""
def preprocess_args(preprocess_func, varnames):
"""Applies preprocess_func to variables in varnames before launching
the function.
"""

def wrapper(func, *args, **kwargs):
names = inspect.getfullargspec(func).args
new_args = [
fun(arg) if (name in varnames) and (arg is not None) else arg
for (arg, name) in zip(args, names)
]
new_kwargs = {
kwarg: fun(value) if kwarg in varnames else value
for (kwarg, value) in kwargs.items()
}
return func(*new_args, **new_kwargs)
def decor(func):
argnames = inspect.getfullargspec(func).args

def wrapper(func, *args, **kwargs):
new_args = [
preprocess_func(arg) if (name in varnames) and (arg is not None) else arg
for (arg, name) in zip(args, argnames)
]
new_kwargs = {
kwarg: preprocess_func(value) if kwarg in varnames else value
for (kwarg, value) in kwargs.items()
}
return func(*new_args, **new_kwargs)

return decorator.decorate(func, wrapper)

return decorator.decorator(wrapper)
return decor


def convert_parameter_to_seconds(varnames):
Expand All @@ -114,11 +120,12 @@ def add_mask_if_none(func, clip, *args, **kwargs):
return func(clip, *args, **kwargs)


@decorator.decorator
def use_clip_fps_by_default(func, clip, *args, **kwargs):
def use_clip_fps_by_default(func):
"""Will use ``clip.fps`` if no ``fps=...`` is provided in **kwargs**."""

def find_fps(fps):
argnames = inspect.getfullargspec(func).args[1:]

def find_fps(clip, fps):
if fps is not None:
return fps
elif getattr(clip, "fps", None):
Expand All @@ -130,14 +137,16 @@ def find_fps(fps):
" the clip's fps with `clip.fps=24`" % func.__name__
)

names = inspect.getfullargspec(func).args[1:]
def wrapper(func, clip, *args, **kwargs):
new_args = [
find_fps(clip, arg) if name == "fps" else arg
for (arg, name) in zip(args, argnames)
]
new_kwargs = {
kwarg: find_fps(clip, kwarg) if kwarg == "fps" else value
for (kwarg, value) in kwargs.items()
}

new_args = [
find_fps(arg) if (name == "fps") else arg for (arg, name) in zip(args, names)
]
new_kwargs = {
kwarg: find_fps(value) if kwarg == "fps" else value
for (kwarg, value) in kwargs.items()
}
return func(clip, *new_args, **new_kwargs)

return func(clip, *new_args, **new_kwargs)
return decorator.decorate(func, wrapper)

0 comments on commit b9b5ecc

Please sign in to comment.