#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Concatenate strings to existing variables


    Hi all,

    I have a for loop in place which I would like to use to trigger an action for each of my variables, something similar to an eval() in javascript.
    What I am looking for from the below is if setpinnumberorall == all & setpinshighorlow == high, then:
    pinout2.write(1)
    pinout3.write(1)
    pinout4.write(1)
    pinout5.write(1)
    etc... to be executed


    Code:
        global pinout2
        global pinout3
        global pinout4
        global pinout5
        global pinout6
        global pinout7
        global pinout8
        global pinout9
        global pinout10
        global pinout11
        setpinnumberorall = request.args.get('setpinsmessage')
        setpinshighorlow = request.args.get('setpinmessagehighorlow')
        if setpinnumberorall == "all":
    	if setpinshighorlow == "high":
    	    for x in range(2, 12):
    		digitalpintomodify = "pinout" + x
    		print eval(digitalpintomodify)
    		eval(digitalpintomodify).write(1)
    		
    	else:
    	    for x in range(2, 12):
    		print x + 2
    		digitalpintomodify = "pinout" + x
    		print eval(digitalpintomodify)
    		eval(digitalpintomodify).write(0)
        else:
    	print setpinnumberorall
    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    Globals are not necessary here. You can store the function calls in a list instead of jumping through hoops.
    Code:
    def test_funct1():
        print "test_funct one executed"
    
    def test_funct2():
        print "test_funct two executed"
    
    some_list = [test_funct1(), test_funct2(), test_funct1()]
    for ctr in range(len(some_list)):
        some_list[ctr]
    or use one function to call the others
    Code:
    def test_funct1():
        print "test_funct one executed"
    def test_funct2():
        print "test_funct two executed"
    
    def call_all():
        test_funct1()
        test_funct2()
    
    call_all()
    Finally, there is redundant code
    Code:
        if setpinnumberorall == "all":
            el = 0
    	if setpinshighorlow == "high":
                el = 1
            for x in range(2, 12):
    		digitalpintomodify = "pinout" + x
    		print eval(digitalpintomodify)
    		eval(digitalpintomodify).write(el)

IMN logo majestic logo threadwatch logo seochat tools logo