You are not logged in.
Pages: 1
Hey,
I have this list:
menu = [ ('Playlist', playlist_menu),
('Play/Pause', playpause) ]
At the moment to avoid errors when I run it I've made empty functions for playlist_menu and playpause but further down I have some code which will check a value and decide which function needs to be run but I'm not sure how I can run them. I could use exec() but is there a better way? This is where I do the work:
while 1:
cmd = dev.listen(sock)
if cmd == '*EAAI':
menu()
elif 'EAMI' in cmd:
sel = re.findall('*EAMI: ([0-9]+)', var)[0]
After that last line I'd like to run the function name in menu[sel][1], is there a "proper" way to do this or is exec() the way to go?
Any opinions appreciated
Offline
Um.... can you use a dictionary instead of a list of tuples?
menu = { 'Playlist', playlist_menu }
then you can do something like:
menu['Playlist']()
or
func = menu['Playlist']
func()
to execute the function
Dusty
Offline
items = { 1 : ('Playlist', playlist_menu),
2 : ('Play/Pause', playpause) }
while 1:
cmd = dev.listen(sock)
if cmd == '*EAAI':
menu(items)
elif 'EAMI' in cmd:
sel = re.findall('*EAMI: ([0-9]+)', cmd)[0]
items[ int(sel) ][1](c)
Okay awesome! That works good Thanks Dusty
Offline
Pages: 1