[Tutor] I Give Up. - Follow up post
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Wed Jul 5 00:11:54 CEST 2006
> Now, if I want to iterate over a list of machines , and check each
> machine for whatever it was set for (in the config file)
> So the config file requires http to be the first part of the string ,
> followed by something to make it unique (I.E. http1, http2, http3)
Hi Brian,
Have you considered using something other than the INI-style file format?
ConfigParser works best when there is a fixed, known set of keys: it
doesn't work so well when you want to use an arbitrary collection where
you do not know the keys in advance.
Bluntly put: it does sound like the INI file format and what you
ultimately want is not meshing well together.
Let's clarify the requirement: you want to have a mapping from services to
their configurations. Ignoring the format of the configuration file for
the moment, it sounds like you ultimately want to parse the configruation
and get:
{ 'apache' : ('http', 80),
... }
where a 'machine' is the key into the corresponding (protocol, port)
value.
If you are just prototyping this, a 'module' file like:
####################################
### machine_config.py
machines = { 'apache': ('http', 80),
## fill me in with the config of other machines
}
####################################
could be used as your configuration file format. The value portion of a
dictionary can be an arbitrary value. Here, we map a machine's name to a
tuple containing the protocol and port. No string hackery is involved
here.
> believe me I have tried dictionaries, Ive tried parsing the file by
> other means, but the only way I could get results I needed was through
> configparser
I think you're making this problem too hard for yourself.
> Yeah, basically you carry values in a dictionary named by keyname ,
> but.. there have been situations where I need the key name as the
> variable name , I.E. config_options[name] = value could become
>
> name = value as if it was explicitly defined that way
Can you show us the situation you're talking about that requires this?
> It's difficult to grasp or even effectively explain the concept or idea,
Try to do so. I think this is a real point that needs to be cleared up.
> Agreed. however I just set this as a preliminary - in no way is this
> code finished. :) I actually intended to build a function to do this.
> (In perl, I use Switch/Case statements, does Python have anything
> similar? - with switch/case, you can set a final default "catch-all" at
> which point I log a "unknown event" message )
Take a look at:
http://www.python.org/doc/faq/general.html#why-isn-t-there-a-switch-or-case-statement-in-python
If you need more examples, ask, and someone here on the list will be happy
to help.
More information about the Tutor
mailing list