The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
How to dynamically assign variables
Discuss How to dynamically assign variables in the Python Programming forum on Dev Shed. How to dynamically assign variables Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 15th, 2004, 11:34 AM
|
|
Registered User
|
|
Join Date: May 2004
Location: London UK
Posts: 4
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
|

May 15th, 2004, 12:13 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
Can you show me an example of what you want, cause i really didnt understand.. sometimes i have this problem understanding users 
|

May 15th, 2004, 12:39 PM
|
|
Registered User
|
|
Join Date: May 2004
Location: London UK
Posts: 4
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
|

May 15th, 2004, 12:49 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
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
|

May 15th, 2004, 01:15 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
|
What's wrong with having them in a dict? That's the easiest way (and probably the best way) to do it.
|

May 15th, 2004, 01:15 PM
|
|
Registered User
|
|
Join Date: May 2004
Location: London UK
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
The file will look like that yes (but without duplicates  )
|

May 15th, 2004, 01:23 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
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
|

May 15th, 2004, 01:27 PM
|
|
Registered User
|
|
Join Date: May 2004
Location: London UK
Posts: 4
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
|

May 15th, 2004, 02:38 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
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(' '):]
|

May 15th, 2004, 04:15 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|