#๐Ÿ”’ Dynamically parse arguments to function through another function

44 messages ยท Page 1 of 1 (latest)

slim zenith
#
def handleTriggers(args=()):
    try:
        function() # How do i pass the args into the function here?
    except TypeError:
        function() # Here i just call the function if it takes no arguments

This is a simplified version of my code, i essentially have a bunch of functions that will be called from the handleTriggers function which decides which one to call. If i give the function any arguments i want to parse it into the next function. But i dont want to directly parse the args variable, i want to parse the arguments inside the args variable ๐Ÿ™‚ Thanks in advance ๐Ÿ˜‰

sacred slateBOT
#

@slim zenith

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

spark glade
#

You should consider using *args instead

slim zenith
# spark glade You should consider using *args instead

Like this?

def function(hello, world):
    print(hello, world)

def handleTriggers(args=()):
    try:
        function(*args) # How do i pass the args into the function here?
    except TypeError:
        function() # Here i just call the function if it takes no arguments

handleTriggers(args=("goodmorning", "friend"))
red wigeon
#

Typical wrapper look like:

def wrapper(*a, **kw):
    .... func(*a, **kw) .....

Can you elaborate on what you're trying to do in more detail?

slim zenith
# red wigeon Typical wrapper look like: ```py def wrapper(*a, **kw): .... func(*a, **kw) ...

The handleTriggers function tries to execute another function based on a neuralnetwork prediction, i want to be able to pass multiple arguments to the functions that handleTriggers will run. The most recent code i posted works but there is an issue when i as an example pass 2 arguments to handleTriggers but the function that it runs only takes 1, like this:

def function(arg1):
    print(arg1)

def handleTriggers(args=()):
    try:
        function(*args) # How do i pass the args into the function here?
    except TypeError:
        function() # Here i just call the function if it takes no arguments

handleTriggers(args=("goodmorning", "friend"))

So if i pass 2 arguments i still want to be able to just take one of them like in the example here.

#

The handleTriggers function is in a library that im writing and the function called function is an example of what a users function could look like ๐Ÿ˜‰

red wigeon
#

Do you have any control over the target functions? i.e. do you know what they accept? Because obviously there's some grey area around arguments, what if the function takes 1 arg, etc?

slim zenith
#

The problem here being that there might be multiple different functions that handleTriggers will be able to run, but not all of them will take the same arguments

red wigeon
#

... and do they take the same kind of arguments, too?

slim zenith
red wigeon
#

Turn it on its head: what do the functions expect to do?

slim zenith
red wigeon
#

... to be able to do?

#

Usually something like this dicates the arguments a user's function must accept. And the user writes some kind of shim which takes exactly that kind of argument and calls their own stuff as needed.

slim zenith
red wigeon
#

So what calls handleTriggers? Where do the args come from? This is kind of important. Surely the args must match what the user's function is expecting?

#

Of do you offer the user a choice of func(some_arg) and func() as the signature, and others are not supported?

slim zenith
#

This is the full code in my library:

def handleTriggers(prediction: dict, probability_threshold: float, trigger_functions: list, args: tuple):
    if float(prediction["probability"]) > probability_threshold:
        for function in trigger_functions:
            if function.__name__ == prediction["intent"]:
                try:
                    return function(*args)
                except TypeError:
                    return function()
#

The user will parse a list of functions called trigger_functions in this code here. Then the user can parse args as a tuple, and then the different function will be able to take the args. But the functions might not take the same amount of args

red wigeon
#

Well, what you've got will work. If it were me I'd be:

  • having the registration function where users add their function to trigger_functions supply the intent string, then you could just have a mapping and call them all for that intent
  • type check the function at registration time eg using @typechecked from the typeguard package
  • require the user function to accept exactly 1 argument, the args tuple itself. Let them unpack it.
slim zenith
#

Oh so let the user unpack the args instead

#

I could just pass a dict of arguments instead right?

red wigeon
#

Sure. Then they can ignore them if they like, or use them.

slim zenith
#

exactly

red wigeon
#

Sure.

slim zenith
#

Hmm good idea

#

Ill do that

red wigeon
#

But either way, their function outght to adhere to your spec: always take the argument (or arguments). And use it or not as desired.

slim zenith
#

Thanks

slim zenith
#

Like in the code example i provided

red wigeon
#

The problem with this is that TypeError is pretty vague. What if the user's code raises one? IMO better to require the correct signature in the registration end, to accept exactly what you're handing out.

#

Make it simple so it is easy for the user to write the function, b ut mandatory.

slim zenith
#

So in my documentation i just say that the functions provided in trigger functions always takes 1 argument called args, and then the user can decide to use it or not, right?

red wigeon
#

That's what I'd do.

#

Of course describe what's in args, but yes do that.

slim zenith
#

Okay, thank you ๐Ÿ™‚

#

Your help was great man ๐Ÿ˜„

red wigeon
#

Any time.

slim zenith
#

!close

sacred slateBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.