[Tutor] Re: creating variables from the 'key' of a dictionary. Fu rther explainations

Andrei project5 at redrival.net
Sun Jul 4 08:49:59 EDT 2004


Larry Blair wrote on Fri, 2 Jul 2004 13:59:50 -0700:

> 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

Well, you can have multiple configuration modules just as well as you can
have multiple parameter files.

> 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

You can use __import__ to load a module the name of which you'll get as a
command line argument or something.

> 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?

What is the difference between specifying things in some kind of
configuration file, reading them and making them into variables and
specifying things in a configuration file, reading them in a dictionary and
then getting the values from that dictionary? The two methods have no
effect whasoever on your configuration file. The difference is in how you
handle the data. 
Creating variables based on a configuration file is not something that I
think is very wise. You might at some point use a variable for some other
purpose and then without noticing it, overwrite it with some variable you
read from the configuration - this danger does not exist if you keep stuff
in a module or dictionary..

> My intent is 
> 1. to have a function to read the parameters file - create a list or
> dictionary of each parameter and value 

Good.

> 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.

Not good for the reason outlined above. Why not just keep and access the
dictionary instead of creating a variable at runtime? If you feel you
really, really must, use the method I mentioned in my previous message. But
accessing the params dictionary really is a better idea.


-- 
Yours,

Andrei

=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.




More information about the Tutor mailing list