Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 16th, 2012, 10:56 AM
norb norb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 7 norb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 38 sec
Reputation 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

Reply With Quote
  #2  
Old December 16th, 2012, 12:20 PM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 297 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 19 h 31 m 11 sec
Reputation Power: 7
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) 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Concatenate strings to existing variables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap