ConfigParser.items sorting

Dean McClure bratprince at gmail.com
Wed Oct 28 17:55:19 EDT 2009


On Oct 28, 4:50 pm, Jon Clements <jon... at googlemail.com> wrote:
> On 28 Oct, 06:21, Dean McClure <bratpri... at gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > Just wondering how I can get theitems() command fromConfigParserto
> > not resort all the item pairs that it presents.
>
> > I am trying to get it to read some data in order:
>
> > [Relay Info]
> > relay_name: IPC
> > relay_current_range: [60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100,
> > 104, 108, 112, 116]
> > relay_current_mutliplier: [1/8, 1/4, 1/2, 1, 2, 4]
> > relay_i: arcfc/(relay_current_range*relay_current_mutliplier)
>
> > so I can input the data and then eval() the equation at the end but
> > when I go
> > config.items('Relay Info')
> > It mixes everything up, is there a way to stop this?
>
> > Here is my selection code
>
> > variables = sorted(config.items('Relay Info'))
> > #Get inputs from user for each variable
> > for variable in variables:
> >         if variable[0] == 'relay_name':
> >                 vars()[variable[0]] = variable[1]
> >         else:
> >                 vars()[variable[0]] = 'not a real thing this is just a fake that
> > will never turn up to establish the variable'
> >                 if variable[1][0] == '[' and variable[1][-1] == ']':
> >                         if variable[0] != 'Any':
> >                                 while (variable[1].count(vars()[variable[0]]) < 1):
> >                                         vars()[variable[0]] = raw_input(str(variable)[1:-1] + "\n")
> >                                         if variable[1].count(vars()[variable[0]]) < 1:
> >                                                 print 'Setting unavailable'
> >                         else:
> >                                 vars()[variable[0]] = raw_input(str(variable)[1:-1] + "\n")
> >                 else:
> >                         vars()[variable[0]] = variable[1]
> >                 vars()[variable[0]] = float(eval(vars()[variable[0]]))
>
> > Thanks for the help!
>
> I'm not 100% sure what you're asking, as why should the order be
> important?
>
> It's probably worth mentioning that the builtin dictionary type is
> 'unordered' as it uses hashing to store keys. However, theConfigParsermodule does allow you to supply a dict_type parameter in
> version 2.6+ [http://docs.python.org/library/configparser.html], so
> you could provide an 'ordered dictionary' which returns itsitemsin
> insertion order. There's lots of recipes out there for those, but I
> believe Raymond Hettinger has a fairly good one on the ActiveState(?)
> cookbook site. (Google for python cookbook).
>
> Since however, the idea of processing INI files is that you *know*
> what you're looking for and how to interpret it, I'm not sure why
> you're not using something similar to this (v2.6.2):
>
> relay_name = config.get('Relay Info', 'relay_name')
> relay_current_range = config.get('Relay Info', 'relay_current_range')
> relay_current_range_list = eval(relay_current_range)
>
> ...etc...
>
> hth,
>
> Jon.

Sorry, basically I was just using configparser to pull sets of
variables and equations to calculate the clearing time of a circuit
breaker. As each device is slightly different and I like the structure
of the configparser I decided it'd be an acceptable way to do this.
The thing is that I wanted to have the relay settings in the first
section, then have sections for different protection settings, all of
these have a few input parameters and an output for clearing time. The
user will select the config file for the relay they want and therefore
not all the settings will be the same, I know this isn't the intended
use of the configparser but I thought it'd be much easier for people
to run from their hdd, just a folder of conf files that can be edited
or expanded on with relative ease.

In order to accommodate for all the different configurations I thought
I would have (as my code was showing but poorly explained) the
variable name in the config file (these will be standardised to a
degree) set to be either a set value or a selectable range in which
the program will ask the user to select a value from the range. One
relay may have a value for overload that is a percentage of the CT
value while others may have a selected full load current which is used
for everything so having all the input variables and then the equation
to output the clearing time means that I can ask for the input values
and evaluate the equation. Or at least I would be able to if I could
output them in order?

Thanks



More information about the Python-list mailing list