I'm trying to write a wrapper in which I want to do something before an event and do something afterwards. (for example, a regular time interval monitor) The specifics of the two functions can vary, I just want to be able to pass in two functions, and for one to be called before and one to be called afterwards, each with their specific arguments. Since the functions aren't defined beforehands, they can have any types of arguments, so I can't specify them beforehand:
def function1(*args_for_function1):
print("function 1 called with ", *args_for_function1)
def function2(*args_for_function2):
print("function2 called with ", *args_for_function2)
Now, I want to be able to use these while doing some other process, in a way where I can pass in arbitrary function1 and function2 to the wrapping function:
def wrapper(function1, function2):
@wraps(function1, function2): #This is obviously wrong, but the result I want to achieve
def async _helper(*args_for_1, *args_for_2):
while True:
function1(*args_for_1)
await next(some_previously_defined_process)()
function2(*args_for_2)
await some_timeout()
return _helper
Is wrapping the right way to go about this?
What I want is to be able to define some concrete implementations of function1 and function2, such as
def log_before(time, things_to_log, status_log):
status_log.append(things_to_log)
def log_after(time, event_status, event_log):
log2.append(event_status)
and rather than having to rewrite the wrapper function every time I have new functions I want to add, just to be able to put the functions into the wrapper:
real_function_to_call = wrapper(log_before, log_after, params?)
What happens with the parameters here? How can I keep them separate and not have to rewrite the wrapper function every time I use two new functions?