Basic Python Question - Function calls with dictionary

I’m working on getting commands from Twitch chat to do things in TD. I’ve got the chat connection functional, and I’m able to parse the incoming message, check for my escape character at the beginning, and set the command to a current_command variable using that. Then I check the current_command against a dictionary of possible commands that lives in another DAT, which works. Now I’m trying to return and call the function calls that I want associated with that command. I can get the function calls back from the dictionary, I’m just not sure how to actually call them at that point. Toe file without Twitch credentials attached. Most of the setup comes from Elburz’s tutorial here: https://interactiveimmersive.io/blog/content-inputs/twitch-chat-in-touchdesigner/twitchConnect_upload.1.toe (6.8 KB)

Are you planing on Twitch Plays Touchdesigner?

check my python tips and tricks . https://derivative.ca/community-post/python-tipsntricks
pass the dictionary as the argument using **

def do_something( argument1, argument2 = 'Hello'):

    debug(argument1, argument2)

argument_dict = {   "argument1" : "World",

                    "argument2" : "Not Hello"}

do_something(**argument_dict)

Just reread your question. You want to call function you saved in adictionary?

Thats not to hard. Just put the function in the dictionary without the ( ) and call it just like you would call it directly

def do_something( argument1, argument2 = 'Hello'):

    debug(argument1, argument2)

argument_dict = {   "argument1" : "World",

                    "argument2" : "Not Hello"}

function_dict = {   "function1" : do_something }

function_dict['function1'](**argument_dict)

Yeah, effectively doing Twitch plays. I plan to let users have some control over some audio and visuals functions on my modular synths streams. I think this is close to what I’m trying to get at, but dictionary and functions are each living all in their own DAT. I’m trying to place the relevant calls in dictionary entries so that I can call multiple functions from each command or share some functions between different commands and things like that. If you check out the Dictionary and Functions DATs in my Toe, that might help to explain. Does that make sense?

I highly suggest that you take a look at extension. They will make your live much easier. You can call functions defined in the extension by just addressing the component operator you are extending.
Check this tutorial by Markus Heckmann: https://www.youtube.com/watch?v=TaNzAfXEQew

For the specific setup you have (and if you want to stick to it) use the Module on Demand functionality.
In your datexecute, use the following line
from commands import commands_dict
you can then directly acces the dictionary in your code. (You will have to run the script once using Ctrl + R after changine stuff inside.) like:
commands_dict['#hit']()
Then do the same in the functions dat so it would look like

import function
commands_dict = {

	"#hit": functions.hit,
	"#drums": functions.drums

}
2 Likes

Awesome, thanks so much! I got the feeling in my research getting to this point that extensions might be helpful later, so I’ll definitely go and check that out now. I have no reason to stick to anything I’m doing here, just trying to figure it all out, I’ve only really used python for references in TD until now. Kinda just trying to set it up modular from the beginning so it’s flexible for future! Thanks again!