[Tutor] creating variables from the 'key' of a dictionary. Fu rther explainations (original) (raw)

Larry Blair lbblair at adaptisinc.com
Fri Jul 2 16:59:50 EDT 2004


These are great ideas and starting to get me in the direction I need to go - I think :) What we have are multiple parameter files. Each has the same parameters but with different values. i.e. p_file1 will have source_server = 'cm05' and target_server = 'ts05' - we are backing up data from cm05 to ts05. p_file2 will have source_server = 'pd01' and target_server = 'bk01'. We give the specific parameter file to the script at run time for backing up different sets of servers. As we add new equipment, SANs, databases etc. our list of parameters grows. Now we have to send a session_name, snapshot_name database names, usernames, passwords etc. which are different for the different servers and databases.

I like the idea of creating a module but that would be a single set of values wouldn't it? Or is there a way to read the specified parameter file and then 'compile' and import the module on the fly?

My intent is

  1. to have a function to read the parameters file - create a list or dictionary of each parameter and value
  2. and pass it back to the calling script and then have a for loop that would dynamically create all variables( these are the keys in the dictionary) with the matching value.

This way if we added a parameter I could add the code only to the function to add the key and value to the dictionary. Then part 2 would automatically create the new variable and I would not have to change code but in one place.

the code would look like

for pr, item in para.item(): some-magic-function where a variable would appear with a value. i.e. source_server = 'cm05' etc until the end of the dictionary.

Thanks Larry

-----Original Message----- From: Jeff Shannon [mailto:jeff at ccvcorp.com] Sent: Friday, July 02, 2004 12:13 PM To: tutor at python.org Cc: Larry Blair Subject: Re: [Tutor] creating variables from the 'key' of a dictionary.

Larry Blair wrote:

I think what I want to do is create a function that would load a dictionary with all parameters from the file. Then in the original script use that dictionary in a for loop to create the variables from the key.

My question is there a way to create a variable with the name of the key in a dictionary i.e. para = {} para['sourceserver'] = 'cm05'

Yes, you can do that. The following three code fragments all result in an identical dictionary:

 para = { 'source_server':'cm05' }

 para = {}
 para['source_server'] = 'cm05'

 key = 'source_server'
 value = 'cm05'
 para = {}
 para[key] = value

In addition, there's a couple of things that might make your life even easier.

First, consider creating a Python module called settings.py, which contains the definition of your parameters dictionary:

----- settings.py ---------

para = { 'source_server':'cm05', 'target_server':'tm02', ... }

Now, in your other files, you can simply

import settings

src = settings.para['source_server'] ....

Second, if your settings are very dynamic and change often, it might be useful to investigate the shelve module. This is a library module that will essentially allow you to store the current state of a dictionary as a disk file, and to later reload that dictionary from the file. This would let you have a persistent record of the most recent state of your parameters.

Jeff Shannon Technician/Programmer Credit International


Confidentiality Notice: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain information that is confidential privileged and/or exempt from disclosure under applicable law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message. Thank you.



More information about the Tutor mailing list