473,804 Members | 3,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ConfigParser and multiple option names

Hello,
since ConfigParser does not seem to support multiple times the same option
name, like:

dir="/home/florian"
dir="/home/john"
dir="/home/whoever"

(only the last one is read in)

I wonder what the best way to work around this.

I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"

What character would be best to work on various operating systems? (of what
names may a path consist is the question)

What do you think? Any better ideas?

Thanks,

Florian
May 2 '06 #1
12 5363
Florian Lindner escribió:
I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"
RCS uses , in filenames
What do you think? Any better ideas?


A bit ugly, but probably safer and simpler than adding arbitrary separators:

[section]
dir_1=/home/florian
dir_2=/home/john
dir_3=/home/whoever
a s(a|i)mple implementation to give you the idea, it has some bugs:

import ConfigParser
import re

class ConfigParserWit hLists(ConfigPa rser.ConfigPars er):
def setlist(self, section, option, value) :
for i, v in enumerate(value ) :
self.set(sectio n, '%s_%i' % (option, i + 1), v)

def getlist(self, section, option) :
res = []
m = re.compile('^' + option + '_\d+$').match
for oo in self.options(se ction) :
if m(oo) :
res.append(self .get(section, oo))
return res


HTH
May 2 '06 #2
Alexis Roda wrote:
Florian Lindner escribió:
I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"
RCS uses , in filenames


A kommata (,) is a valid character in path names. Ok, you can use quotes.
What do you think? Any better ideas?


A bit ugly, but probably safer and simpler than adding arbitrary
separators:

[section]
dir_1=/home/florian
dir_2=/home/john
dir_3=/home/whoever


I tend to use seperators, because I think it's more common to users. (the
PATH environment variable e.g.)
a s(a|i)mple implementation to give you the idea, it has some bugs:

[...]

Thanks for your input!

Florian
May 2 '06 #3
Florian Lindner wrote:
Hello,
since ConfigParser does not seem to support multiple times the same option
name, like:

dir="/home/florian"
dir="/home/john"
dir="/home/whoever"

(only the last one is read in)

I wonder what the best way to work around this.

I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"

What character would be best to work on various operating systems? (of what
names may a path consist is the question)

What do you think? Any better ideas?

Thanks,

Florian


I would either use (not tested):

[directories]
dir_01="/home/florian"
dir_02="/home/john"
dir_03="/home/whoever"

and in my program do:

INI=ConfigParse r.ConfigParser( )
INI.read(inifil epath)

section="direct ories"
dirs=[x for x in INI.options(sec tion) if x.startswith('d ir_')]

or more easily:

[init]
dirs=/home/florian,/home/john,home/whoever

and in program do:

section="init"
dirs=INI.get(se ction, 'dirs',).split( ',')

Which one to use depends on how many dirs you would be supporting.
If it is a lot, I like the first version. If a few, the second
seems better.

-Larry Bates
May 2 '06 #4
Florian Lindner wrote:
I think the best solution would be to use a seperation character:

dir="/home/florian, /home/john, home/whoever"

What character would be best to work on various operating systems? (of
what names may a path consist is the question)


I don't think there are any universally-disallowed chars for path names.
But I also haven't seen anyone mention os.pathsep yet. Seems like the best
choice for a separator char. If you need to run on multiple platforms,
your choices seem to be:

1. pick a legal-on-some-systems-but-uncommon-on-all char for dir names as a
separator (iirc osx uses : in paths so ; might have to do)
2. use os.pathsep to write platform-specific config files
3. do it another way

May 2 '06 #5
Florian Lindner wrote:
Hello,
since ConfigParser does not seem to support multiple times the same option
name, like:

dir="/home/florian"
dir="/home/john"
dir="/home/whoever"

<snip>

Another option would be to switch to the XMLParser library and use an
XML file for the configuration.

That way, you can build using the same name:

#==== config_file.xml ====
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE options>
<options>
<directories>
<path>"/home/florian"</path>
<path>"/home/john"</path>
<path>"/home/whoever"</path>
</directories>
{other options}
</options>
#==== config_file.xml ====

There are also several tutorials
<a
href="http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/pyxml-akara">
Uche Ogbuji's Akara site
</a>

has some excellent XML tutorials which include python XML processing.

May 2 '06 #6
Florian Lindner wrote:
since ConfigParser does not seem to support multiple times the same option
name, like:

dir="/home/florian"
dir="/home/john"
dir="/home/whoever"


I generally do this:

dirs =
/home/florian
/home/john
/home/whoever

....and then use str.split() in my program.
--
Benji York
May 2 '06 #7

Benji York wrote:
<SNIP>

I generally do this:

dirs =
/home/florian
/home/john
/home/whoever

...and then use str.split() in my program.
--
Benji York


The only problem with this would be if you plan on updating the config
file later in the program - I don't think ConfigParser would write the
new config file with these options setup this way.

May 2 '06 #8
I have had this same problem before, and what I ended up doing was
writing my own far more limited config parser that would create lists
for repeated named assignments.

Who is the maintainer of ConfigParser? Perhaps a keyword option can
be added so that this kind of behaviour can be added at creation.

Caleb

May 3 '06 #9
Not tested by me, but according to docs it does support list values:

http://cheeseshop.python.org/pypi/ConfigObj

Regards
May 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6786
by: Matthew Barnes | last post by:
I'm considering submitting a patch for Python 2.4 to allow environment variable expansion in ConfigParser files. The use cases for this should be obvious. I'd like to be able to specify something like the following in a configuration file: data_file=${HOME}/mydata.dat ....(where HOME=/home/matt) and have ConfigParser automatically expand it to:
0
2022
by: asqui | last post by:
(Assume an instantiated ConfigParser, c, with a loaded file) >>> c.get("DEFAULT", "foo", False, {"foo": "Bar"}) 'Bar' >>> c.get("DEFAULT", "Foo", False, {"Foo": "Bar"}) Traceback (most recent call last): File "<pyshell#70>", line 1, in ? c.get("DEFAULT", "Foo", False, {"Foo": "Bar"}) File "C:\Python23\lib\ConfigParser.py", line 513, in get raise NoOptionError(option, section)
3
683
by: Sergey | last post by:
Is there an alternative to standard module ConfigParser, which can use delimitier symbol other than ":" and "=", preferaby just space? I need to parse such configs: 2:5020/758 xxxx 2:5020/794 yyyy ConfigParser is not able to work with option names which contain symbol ':' It is not difficult to modify ConfigParser to resolve it, but may be another
2
6961
by: Thomas Guettler | last post by:
Hi, I need a config like this: ignore=".*/foodir/.*\.pyc" ignore=".*/foodir/.*~" .... The ConfigParser of the standard library can't handle this,
1
1386
by: Alexandre CONRAD | last post by:
Hello list ! I'm using the ConfigParser module to use configuration files (what else would it be for ?). But I have a dilema: I'd like to setup multiple "update server" for my application with update "priority". At first, I thought about adding a new section in my actual existing config file such as:
3
4361
by: tony.ha | last post by:
Hello I use ConfigParser as show below to read a config.txt file; from ConfigParser import ConfigParser config = ConfigParser() config.read('config.txt') items = config.items('FV') for item in items: module_name = item print module_name
3
7193
by: derf912 | last post by:
I am working with ConfigParser in python and the ini file that I am reading in has a section titled and under that section I have multiple options. All the options have the same name "customer =" Eample: customer = widget1 customer = widget2 customer = widget3 customer = widget4 I am trying to get a list of all the options but have only been able to get the last one to print. Here is the script that I am working with right...
4
14285
by: Phoe6 | last post by:
Hi, I have a configfile, in fact, I am providing a configfile in the format: Name: Foo Author: Bar Testcases: tct123
3
2846
by: Phoe6 | last post by:
Hi, Am starting a new thread as I fear the old thread which more than a week old can go unnoticed. Sorry for the multiple mails. I took the approach of Subclassing ConfigParser to support multiline values without leading white-spaces, but am struct at which position in _read I should modify to accomodate the non-leading whitespace based multiline values.
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.