1

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?

Allen Wang
  • 2,426
  • 2
  • 24
  • 48
  • Why a *decorator*? – user2357112 Dec 07 '17 at 21:36
  • 2
    *"how would you decorate them"* - which "them", `function1` and `function2`? You don't decorate *them*, you decorate *some other function* with `@wrapper(function1, function2)`; note e.g. https://stackoverflow.com/q/5929107/3001761 for decorators with arguments. Also your use of arguments doesn't make much sense. – jonrsharpe Dec 07 '17 at 21:36
  • Edited, I guess I'm a bit confused about the terminology around wrap and decorate, I mean to wrap – Allen Wang Dec 07 '17 at 21:37
  • 1
    I guess my main question is that when wrapping the two functions, how would you separate the arguments, so that you can call function1 (with function1 arguments) and function2 (with function2) arguments separately – Allen Wang Dec 07 '17 at 21:41
  • 1
    Wrapping isn't required ya know, you could do exactly what you have here and just get rid of the `@wrap` and it would run fine. – Nick Chapman Dec 07 '17 at 21:52
  • What about separation of arguments, some specific to function1 and some specific to function2? – Allen Wang Dec 07 '17 at 21:55
  • 1
    You can't really do that with decoration. The arguments that get passed to the wrapper are the ones for the wrapped function, not function1 or function2. The point of using the decorator is *not* to change the interface of the wrapped function, so if function1 and function2 need further configuration at call time this is probably the wrong approach. – jonrsharpe Dec 08 '17 at 10:47

0 Answers0