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

K

kampy

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
 
T

Tarek Ziadé

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

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
 
N

narasimha18sv

my requirement is to have section with in a section in config parameters

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



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

yes but it is not only for one structure like above there will be many sections like that
 
N

narasimha18sv

my requirement is to have section with in a section in config parameters

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



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

yes but it is not only for one structure like above there will be many sections like that
 
S

Steven D'Aprano

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
 
D

Dennis Lee Bieber

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

INI file structure (which I believe the config file follows) does not
support hierarchical sections. What you CAN do is something like:

[TopLevels]
topsections=AAA, DDD

[AAA]
sections=BBB, CCC

[BBB]
a=1
b=1

[CCC]
a=1
b=2

[DDD]
sections=EEE

[EEE]
a=3
b=4

In other words, YOU have to define the nesting hierarchy by
specifying keys that identify the sections in the next level down.
 
T

Tarek Ziadé

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
 
S

Steven D'Aprano

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.

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top