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

Discussion in 'Python' started by kampy, Oct 19, 2012.

  1. kampy

    kampy Guest

    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
     
    kampy, Oct 19, 2012
    #1
    1. Advertising

  2. kampy

    Tarek Ziadé Guest

    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

    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
     
    Tarek Ziadé, Oct 19, 2012
    #2
    1. Advertising

  3. kampy

    Guest

    On Friday, 19 October 2012 15:39:57 UTC+5:30, 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
    >
    >
    >
    > 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
     
    , Oct 19, 2012
    #3
  4. kampy

    Guest

    On Friday, 19 October 2012 15:39:57 UTC+5:30, 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
    >
    >
    >
    > 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
     
    , Oct 19, 2012
    #4
  5. kampy

    Tarek Ziadé Guest

    On 10/19/12 12:22 PM, 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...
     
    Tarek Ziadé, Oct 19, 2012
    #5
  6. kampy

    rusi Guest

    On Oct 19, 6:58 pm, Tarek Ziadé <> wrote:
    > On 10/19/12 12:22 PM, 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 ??
     
    rusi, Oct 19, 2012
    #6
  7. Re: section with in a section config file and reading that configfile

    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
     
    Steven D'Aprano, Oct 19, 2012
    #7
  8. On Fri, 19 Oct 2012 02:51:13 -0700 (PDT), kampy
    <> declaimed the following in
    gmane.comp.python.general:

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

    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Oct 19, 2012
    #8
  9. 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
     
    Tarek Ziadé, Oct 19, 2012
    #9
  10. Re: section with in a section config file and reading that configfile

    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
     
    Steven D'Aprano, Oct 19, 2012
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian McCullough
    Replies:
    0
    Views:
    500
    Brian McCullough
    Feb 16, 2007
  2. alf

    reading web.config section

    alf, Feb 27, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    676
    Juan T. Llibre
    Mar 1, 2007
  3. Sergei Shelukhin
    Replies:
    1
    Views:
    1,152
    David
    Mar 29, 2007
  4. CSharpner
    Replies:
    0
    Views:
    1,048
    CSharpner
    Apr 9, 2007
  5. M Irfan
    Replies:
    2
    Views:
    3,926
    M Irfan
    Apr 18, 2007
Loading...

Share This Page