Using variable to call a function
  • Invisible Ray
    member
    Posts: 46
    Joined: Mon May 16, 2011 11:50 pm

    Using variable to call a function

    by Invisible Ray » Wed Nov 11, 2015 1:18 am

    I'd like to dynamically call a function from the Message() module.

    In pseudo-code:
    function x() - does x thing
    function y() - does y thing
    I have a select list in my module with items 'x' and 'y'.
    When one of the items is selected in the module, the function with that name runs.

    in other words:
    if msg == <select list selection>:
    myFunc = <selection>
    myFunc()
    (then either function x or function y runs, depending on which one is selected)

    I've tried a few variations of this, and I keep getting the error "TypeError: 'unicode' object is not callable".
  • Invisible Ray
    member
    Posts: 46
    Joined: Mon May 16, 2011 11:50 pm

    Re: Using variable to call a function

    by Invisible Ray » Wed Nov 11, 2015 2:17 am

    The next thing I tried is creating a dictionary where each item in the select list is the key and the selection +'()' is the value.

    When I try to call a function with this construction, I get the error "TypeError: 'str' object is not callable"

    Is there any way to change a 'str' object or 'unicode' object into a function object?
  • anomad
    master
    Posts: 412
    Joined: Sun Oct 21, 2007 10:07 pm
    Location: north cakalacky, usa
    Contact:

    Re: Using variable to call a function

    by anomad » Wed Nov 11, 2015 7:35 pm

    . you might be able to store a reference to the function in a variable, but i'm not sure…

    . something like this ?

    https://www.daniweb.com/programming/sof ... a-variable


    -james
    (a nomad. )
  • Invisible Ray
    member
    Posts: 46
    Joined: Mon May 16, 2011 11:50 pm

    Re: Using variable to call a function

    by Invisible Ray » Wed Nov 11, 2015 9:38 pm

    That looks like it might work. I'll give it a try.
  • Invisible Ray
    member
    Posts: 46
    Joined: Mon May 16, 2011 11:50 pm

    Re: Using variable to call a function

    by Invisible Ray » Sat Nov 14, 2015 7:21 pm

    I got it to work, but I had to use a slight workaround.

    Function of the script: Use a select list to trigger predefined sets of layer effects via functions

    variables used in the script:

    # list used to populate a text list control
    fxList = ['none','scale','scale_w']

    # assigning variables to functions
    fx0 = nofx
    fx1 = fscale
    fx2 = fscale_w

    # ordered list of the variables
    funcList = [fx0,fx1,fx2]

    # control name and Script Connect message of the text list control
    'lfxL'

    lfx = 0 #index position of low freq fx selected
    mfx = 0 #index position of mid freq fx selected
    hfx = 0 #index position of hi freq fx selected

    # list used to store params for the three FX functions
    # is low active, lfx, is mid active, mfx, is high active, hfx
    actFX = [0,lfx,0,mfx,0,hfx]

    <script snippet in 'MessageEvent() module>

    if msg == 'lfxL':
    myFunc = module.getValue('lfxL', 'selection') #get item selected in list
    listPos = fxList.index(myFunc)
    actFX[1] = listPos

    ********************************

    What the code does:
    1) user selects an item in the list
    2) the selection sends a message: 'lfxL'
    3) variable myFunc is set to the text of the item selected in the list
    4) listPos is set to the index position of myFunc when found in the list 'fxList'
    5) actFX list item at position [1] is updated with the value of listPos


    What I would like to do to make the code more efficient:
    I should be able to directly assign the index position of the item selected in lfxL to the item actFX[1]

    My problem is that I can't figure out how to return the index number of an item selected in a list control. I can return the text of the selected item, but not the index number, so I have to search for the text string in a list of the items, then assign that number to the actFX list.

    ** using this built in function: module.getValue('lfxL', 'selection') **
    It doesn't matter what I choose for the Value of the control attribute.
    If I set it to "Text" I get the text of the item selected
    If I set it to "Abs. Index" I get 0, no matter which item I select
    If I set it to "Prop. Index" I get 0, no matter which item I select
  • anomad
    master
    Posts: 412
    Joined: Sun Oct 21, 2007 10:07 pm
    Location: north cakalacky, usa
    Contact:

    Re: Using variable to call a function

    by anomad » Sat Nov 14, 2015 10:24 pm

    . have you considered using dictionaries ?

    . then create your fxList like myFX = {'nofx' : 'None', 'scale' : 'Scale Stuff..' }

    . the your list could send 'nofx' and you would reference myFX( text returned from list )

    . if you're tracking multiple variables :

    myFX = {
    'nofx' : { 'name' : 'None', 'action' : 'so something here ' } ,
    'fx1' : { 'name' : 'FX 1 ', 'action' : 'do something cool here', 'lfx' : 0, ',mfx' : 0, 'hfx' : 0 },
    'fx2' : { 'name' : 'FX 2 ', 'action' : 'do something else cool here', 'lfx' : 0.4, ',mfx' : 0.7, 'hfx' : 0.25555553 }
    }

    . then you reference things like this : myFX['fx1']['mfx']


    . might be something to look into


    -james
    (a nomad. )

Who is online

Users browsing this forum: No registered users and 20 guests