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 May 15th, 2004, 11:34 AM
tdw tdw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: London UK
Posts: 4 tdw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How to dynamically assign variables

[Python Newbie]

I want to iterate through a file to read lines of pairs of values and dynamically create and set variables.

eg

line = Variable1 Value1

to create Variable1 with a value of Value1

I don't know what the pairs will contain before reading the file.

ConfigParser looks like it will do the job, but I can't figure out how (I don't want the variable to be in a Dict). Using open() and stepping through the lines allows me to grab the 2 items, but I can't work out how to assign a variable based on the 2 items.

Any help appreciated

Reply With Quote
  #2  
Old May 15th, 2004, 12:13 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,573 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
Can you show me an example of what you want, cause i really didnt understand.. sometimes i have this problem understanding users

Reply With Quote
  #3  
Old May 15th, 2004, 12:39 PM
tdw tdw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: London UK
Posts: 4 tdw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0


try this

File (a portion of it)
Code:
Server_Port 8888
Max_Connect 100
Error_count 20


Code:
def load_from_file():
    f = open("c:\\myprog\\progdata.txt","r")
    r = f.readlines()
    f.close
    for line in r:
        var, val = line.split()[0], line.split()[1]


so for the first line read I have a variable called var containing Server_port and a variable called val containing 8888. From these I need to create a variable called Server_Port with the value 8888 for
Code:
def StartServer():
    server = HTTPServer((' ', Server_Port), RequestHandler)


In this case its an INI file, but not always, I don't want to have to list *all* the possible variable names within the prog itself so a dynamic method is preferable.

TIA

Reply With Quote
  #4  
Old May 15th, 2004, 12:49 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,573 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
so you are saing that you are going to have data like so
Code:
Server_Port 8888
Max_Connect 100
Error_count 20

Server_Port 8000
Max_Connect 100
Error_count 20


just want to make sure i fully understand, as im not a python guru yet

Reply With Quote
  #5  
Old May 15th, 2004, 01:15 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
What's wrong with having them in a dict? That's the easiest way (and probably the best way) to do it.
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #6  
Old May 15th, 2004, 01:15 PM
tdw tdw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: London UK
Posts: 4 tdw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The file will look like that yes (but without duplicates )

Reply With Quote
  #7  
Old May 15th, 2004, 01:23 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,573 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
Quote:
Originally Posted by tdw
The file will look like that yes (but without duplicates )


Ok give me a few min while i comeup with something for you

Reply With Quote
  #8  
Old May 15th, 2004, 01:27 PM
tdw tdw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: London UK
Posts: 4 tdw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Strike
What's wrong with having them in a dict? That's the easiest way (and probably the best way) to do it.

I'm beginning to think so too

Thanks All

Reply With Quote
  #9  
Old May 15th, 2004, 02:38 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,573 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
ok i got this little sample here.. but cant get rid of them \n see what you can do.. but it does give you the port numbers

Code:
for x in file('data.db'):
         lists  = x
         Server =  lists[lists.find('Server_Port'):20]
         print Server[Server.find(' '):]

Reply With Quote
  #10  
Old May 15th, 2004, 04:15 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
This was discussed in a thread here a few weeks ago.

You can assign them to the dictionary returned by the builtin function globals(), and they will become global variables.

Code:
def load_from_file():
    f = open("c:\\myprog\\progdata.txt","r")
    r = f.readlines()
    f.close
    for line in r:
        var, val = line.split()[0], line.split()[1]
        globals()[var] = val


However you cannot create local variables this way, since the locals() function creates a copy of the local namespace and any changes to it will have no effect. To create a local variable you have to use
Code:
exec "%s = %s" % (var, val)

instead.

You can also create an attribute on an object with

Code:
setattr(obj, var, val)


On the other hand, it may be simpler to use a dictionary, as others have said.

Dave - The Developers' Coach

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to dynamically assign 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