Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

section with in a section config file and reading that config file

2,511 views
Skip to first unread message

kampy

unread,
Oct 19, 2012, 5:51:13 AM10/19/12
to
hi all,
my requirement is to have section with in a section in config parameters
ex:
[AAA]
[BBB]
a=1
b=1
[CCC]
a=1
b=2
Any one help me in understanding how to make sure that config file to have a structure like this and reading with the config parser

Tarek Ziadé

unread,
Oct 19, 2012, 6:09:53 AM10/19/12
to pytho...@python.org
a configuration file is a flat sequences of sections, you cannot do this

what you could do is have 2 files, and add a link from one to the other:


file1.ini:

[AAA]
extended = file2.ini

file2.ini:

[BBB]
a=1
b=1

[CCC]
a=1
b=2


then create a bit of logic on the top of ConfigParser to read back those
values

HTH
Tarek

narasi...@gmail.com

unread,
Oct 19, 2012, 6:22:37 AM10/19/12
to pytho...@python.org
yes but it is not only for one structure like above there will be many sections like that

narasi...@gmail.com

unread,
Oct 19, 2012, 6:22:37 AM10/19/12
to comp.lan...@googlegroups.com, pytho...@python.org
On Friday, 19 October 2012 15:39:57 UTC+5:30, Tarek Ziadé wrote:

Tarek Ziadé

unread,
Oct 19, 2012, 9:57:50 AM10/19/12
to pytho...@python.org
On 10/19/12 12:22 PM, narasi...@gmail.com wrote:
>
> yes but it is not only for one structure like above there will be many sections like that
I'd use yaml or json then...


rusi

unread,
Oct 19, 2012, 10:26:47 AM10/19/12
to
On Oct 19, 6:58 pm, Tarek Ziadé <ta...@ziade.org> wrote:
> On 10/19/12 12:22 PM, narasimha1...@gmail.com wrote:
>
> > yes but it is not only for one structure like above there will be many sections like that
>
> I'd use yaml or json then...

Maybe http://www.voidspace.org.uk/python/configobj.html ??

Steven D'Aprano

unread,
Oct 19, 2012, 5:29:04 PM10/19/12
to
On Fri, 19 Oct 2012 12:09:53 +0200, Tarek Ziadé wrote:

> On 10/19/12 11:51 AM, kampy wrote:
>> hi all,
>> my requirement is to have section with in a section in config
>> parameters ex:
>> [AAA]
>> [BBB]
>> a=1
>> b=1
>> [CCC]
>> a=1
>> b=2
>> Any one help me in understanding how to make sure that config file to
>> have a structure like this and reading with the config parser
>
> a configuration file is a flat sequences of sections, you cannot do this

That is incorrect.

A configuration file is a file containing configuration data. That is all.

"Configuration file" says nothing about the format of the file. It could
be a Unix .rc file, a Windows .ini file with no section header, a
Windows .ini file with section headers, a Python source code file, YAML,
JSON, XML, a PLIST file, or any other format you decide to use.

If the Original Poster wants an ini file with nested sections, he can
have an ini file with nested sections.

There is no support for nested sections in the ConfigParser module, but
the ConfigObj third-party module supports it. Otherwise the OP could
write his own code, possibly by subclassing from ConfigParser.

A simple google for "python ini file nested sections" finds this:

http://wiki.python.org/moin/ConfigParserShootout



--
Steven
Message has been deleted

Tarek Ziadé

unread,
Oct 19, 2012, 5:59:03 PM10/19/12
to pytho...@python.org
On 10/19/12 11:29 PM, Steven D'Aprano wrote:
> On Fri, 19 Oct 2012 12:09:53 +0200, Tarek Ziadé wrote:
>
>> On 10/19/12 11:51 AM, kampy wrote:
>>> hi all,
>>> my requirement is to have section with in a section in config
>>> parameters ex:
>>> [AAA]
>>> [BBB]
>>> a=1
>>> b=1
>>> [CCC]
>>> a=1
>>> b=2
>>> Any one help me in understanding how to make sure that config file to
>>> have a structure like this and reading with the config parser
>> a configuration file is a flat sequences of sections, you cannot do this
> That is incorrect.
uh ?
>
> A configuration file is a file containing configuration data. That is all.
yeah,
organized in [sections]. a flat list of sections.

>
> "Configuration file" says nothing about the format of the file. It could
> be a Unix .rc file, a Windows .ini file with no section header, a
> Windows .ini file with section headers, a Python source code file, YAML,
> JSON, XML, a PLIST file, or any other format you decide to use.
>
> If the Original Poster wants an ini file with nested sections, he can
> have an ini file with nested sections.

That's not an ini file anymore. That's a Foord-file :)

There's no notion of nested sections in ini configuration files, since
there's no syntax marker to do the nesting

see https://en.wikipedia.org/wiki/INI_file#Sections

>
> There is no support for nested sections in the ConfigParser module, but
> the ConfigObj third-party module supports it. Otherwise the OP could
> write his own code, possibly by subclassing from ConfigParser.
This is not a ini configuration file anymore, since it introduces ad-hoc
markers added that
are not recognized by other parsers.

Which is fine.

But instead of using an exotic, ad-hoc, look-alike ini file,
I strongly recommend using a standard that has native nested elements
(json or yaml)

Cheers
Tarek

Steven D'Aprano

unread,
Oct 19, 2012, 6:59:15 PM10/19/12
to
On Fri, 19 Oct 2012 23:59:03 +0200, Tarek Ziadé wrote:

> On 10/19/12 11:29 PM, Steven D'Aprano wrote:
>> On Fri, 19 Oct 2012 12:09:53 +0200, Tarek Ziadé wrote:
>>
>>> On 10/19/12 11:51 AM, kampy wrote:
>>>> hi all,
>>>> my requirement is to have section with in a section in config
>>>> parameters ex:
>>>> [AAA]
>>>> [BBB]
>>>> a=1
>>>> b=1
>>>> [CCC]
>>>> a=1
>>>> b=2
>>>> Any one help me in understanding how to make sure that config file
>>>> to have a structure like this and reading with the config parser
>>> a configuration file is a flat sequences of sections, you cannot do
>>> this
>> That is incorrect.

> uh ?

"That is incorrect" means that your statement was wrong. Configuration
files are NOT flat sequences of sections. *Some* configuration files are
flat sequences of sections, some are nested, hierarchical sections, and
some do not have sections at all.


>> A configuration file is a file containing configuration data. That is
>> all.
> yeah,
> organized in [sections]. a flat list of sections.

No, you are making the same wrong statement.

I have hundreds of configuration files on my computer, and very few of
them are a flat list of sections.


>> "Configuration file" says nothing about the format of the file. It
>> could be a Unix .rc file, a Windows .ini file with no section header, a
>> Windows .ini file with section headers, a Python source code file,
>> YAML, JSON, XML, a PLIST file, or any other format you decide to use.
>>
>> If the Original Poster wants an ini file with nested sections, he can
>> have an ini file with nested sections.
>
> That's not an ini file anymore. That's a Foord-file :)

Who cares? Did the poster say that he needed an INI file? No he did not.
He says he needs a configuration file, and that is *much* more general
than just INI files. Not all configuration files are INI files.

The poster says he needs a list of key:value pairs split into nested
sections. Who cares if that is an INI file or not? It is still a
configuration file, and he can have such a file if he wants.

You might as well complain that his sample config was not valid Python
code. Who cares if it is not valid Python code, he didn't ask for valid
Python code, and he didn't ask for a valid INI file.


--
Steven
0 new messages