[Tutor] I Give Up. - Follow up post

Brian Gustin brian at daviesinc.com
Wed Jul 5 01:15:22 CEST 2006


OK .. so far so good.. :)
> 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:

> ####################################
> ### 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.
Umm well it would work nicely . *if* the configuration never changed- on 
the other hand, if it is to do as intended, the user of the application 
needs to be able to add and edit the configuration.

Unless they are well versed with python and/or programming and syntax, 
(in which case they probably arent going to use this script anyway)
it is easier for a non programmer to edit a configuration that makes 
some sort of logical sense and is easy to edit, without screwing up 
(need to make it more "forgiving" of things like line endings, spacing 
between name , = and value elements, etc)

So the ideal format is a configuration file where they can set up 
(within documented settings parameters) additional machines or protocols 
or ports to suit their own network or layout..

Easiest = a simple file where I set name = value and import it.
HOWEVER, if it is to be imported, it must follow proper python syntax, 
correct? so if it doesnt, they break it

an INI file on the other hand is a bit more forgiving, thus the choice 
to use it.

> 
> 
> 
>> 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.
> 
perhaps, when I get back to the project I plan to do a code review of 
code to date..
I have no problem with refactoring if I can find a better, momre 
effective way, but it *must* also be user friendly (ease of use and 
configuration for a non-programmer)

Another option I had was to do a web based interface for setting up 
additional machines, etc, but web based is non-ideal for this application.


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

if cfg.has_section("Hosts"):
         hostnames = cfg.items("Hosts")
         for hostname in hostnames:
             watch_hosts[hostname[0]] = hostname[1]

would result in like
watch_hosts = {"http" : "80","https" : "443"}

Sure, I could also do this by reading a config file and parsing it , but ..

Say you split at the = , and the config file is like

http = 80 #standard http port
  https=443#https port

Are you going to be 100% certain that when you call watch_hosts[http], 
that you are gonna GET string "80"?
  what about the space between http and =  ?

if you wanted to check if watch_hosts[keyname] == "http" , it would 
never work, right?(because if you split at =, you have string "http " <- 
see the space char?)

if I, however use configparser, I will always get "http" and "80" where 
I expect them to be , I dont have to worry (or send values through a 
cleanup or trim() function) if it has a comment for the line or not, I 
dont need to worry if there is whitespace. right?

This is intended for an end user to be able to edit a config file with a 
minimum of trouble, and be able to leave comments so they remember what 
each is for.. :)
(and if they do mess up somehow, I can catch that kind of thing in the 
code)

I just didnt see any more effective way to get this than to use 
ConfigParser as opposed to attempting to parse a file using regular 
expressions , trim, split, etc

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

I understand the concept/idea behind it, I think above is about as good 
an explanation as it gets - I tried to express how Im 
thinking/approaching this issue. Keeping in mind this is going to be 
edited by someone who may or may not know how to write a single line of 
program code.. :)
> 

> Take a look at:
> 
> http://www.python.org/doc/faq/general.html#why-isn-t-there-a-switch-or-case-statement-in-python 
> 
Thanks. reading up on it now.

> 
> If you need more examples, ask, and someone here on the list will be 
> happy to help.
> 
> !DSPAM:44aae7ac185203757830825!
> 
> 


More information about the Tutor mailing list