Skip to content

Instantly share code, notes, and snippets.

@nicolas-lair
Last active September 1, 2022 14:11
Show Gist options
  • Save nicolas-lair/28858b293c06456bac2388cd6b8994bc to your computer and use it in GitHub Desktop.
Save nicolas-lair/28858b293c06456bac2388cd6b8994bc to your computer and use it in GitHub Desktop.
Factory for chaining the runs of a function between two month-year dates
def chain_run_decorator(start_date, end_date):
""" Chain run of a function between two month-year dates"""
def Inner(func):
def wrapper(*args):
begin_m, begin_y = map(int, start_date.split("-"))
end_m, end_y = map(int, end_date.split("-"))
launch_dates = (
[(m, begin_y) for m in range(begin_m, 13)] +
[(m, y) for y, m in product(range(begin_y + 1, end_y), range(1, 13))] +
[(m, end_y) for m in range(1, end_m)]
)
result = dict()
for m, y in launch_dates:
result[(m,y)] = func(m, y, *args)
return result
return wrapper
return Inner
def chain_run_function_factory(func):
def chained_func(start_date, end_date, *args, **kwargs):
return chain_run_decorator(start_date, end_date)(func)(*args, **kwargs)
return chained_func
def my_fun(m,y, *args):
"""dumb function"""
print(m, y, *args)
my_fun_runs = chain_run_function_factory(my_fun)
if __name__=="__main__":
SECTOR = "PRO"
START_DATE = "11-2018"
END_DATE = "01-2022"
result = my_fun_runs(START_DATE, END_DATE, SECTOR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment