Configuration Files

Discussion in 'Python' started by Aaron, Dec 26, 2004.

  1. Aaron

    Aaron Guest

    Hi,

    I'm interested in creating a large number of configuration files which I
    have no experience doing in python. The fields will be static for the most
    part. But design changes and I might want to add new fields in the future..

    My question is - whats the best module for creating, reading, and editing
    these files(something like an INI)? I want it to be powerful,
    yet simple and minimalistic, not requiring a huge amount of overhead.

    I've heard of xml, and have seen it used in both html fashion, and
    likewise, without any tags at all- just spaces in between entries; why is
    this? CSV is another thing I've seen thrown around on online
    documentation. Whats the difference between the two, and which one should
    I use(if either)?

    -thanks in advance
     
    Aaron, Dec 26, 2004
    #1
    1. Advertising

  2. Aaron

    M.E.Farmer Guest

    Aaron wrote:
    > Hi,
    >
    > I'm interested in creating a large number of configuration files

    which I
    > have no experience doing in python. The fields will be static for the

    most
    > part. But design changes and I might want to add new fields in the

    future..
    >
    > My question is - whats the best module for creating, reading, and

    editing
    > these files(something like an INI)?


    Ummm did you look at the standard library before asking any on this?
    If you are a total newbie I'll give you a start it is in the
    Python/Lib folder.
    Look for a module called ConfigParser.py ;)
    I might post a code sample if I am up to it....so much turkey, and
    cookies.

    > I want it to be powerful,
    > yet simple and minimalistic, not requiring a huge amount of overhead.

    Don't we all!

    M.E.Farmer
     
    M.E.Farmer, Dec 26, 2004
    #2
    1. Advertising

  3. Aaron

    Mike Meyer Guest

    Aaron <> writes:

    > Hi,
    >
    > I'm interested in creating a large number of configuration files which I
    > have no experience doing in python. The fields will be static for the most
    > part. But design changes and I might want to add new fields in the future..
    >
    > My question is - whats the best module for creating, reading, and editing
    > these files(something like an INI)? I want it to be powerful,
    > yet simple and minimalistic, not requiring a huge amount of overhead.
    >
    > I've heard of xml, and have seen it used in both html fashion, and
    > likewise, without any tags at all- just spaces in between entries; why is
    > this? CSV is another thing I've seen thrown around on online
    > documentation. Whats the difference between the two, and which one should
    > I use(if either)?


    M.E. Farmer already suggested the ConfigParser module, and I have to
    concurr with him - it basically handles INI files.

    XML is verbose. It allows the construction of complex data structures
    (it's been called S-expressions with brokeits). It's primary advantage
    is that there are tools for dealing with XML sanely - XML editors,
    validators, parsers, and similar things. Without those tools, it can
    be a PITA to deal with. So if your configuration is simple enough to
    be captured by an INI file, avoid XML. If not, then googling for "xml
    editor windows free" turns up a fair number of hits. For Unix, I used
    to use an XML editor called Ted that was written in Python, but can't
    turn anything up on google. There are xml editors for both KDE and
    Gnome. Personally, I use psgml, which is an emacs package that handles
    both xml and it's parent sgml.

    CSV stands for Comma Separated Values. It's primarily an interchange
    format for spreadsheet programs. While there is a default csv module
    (that also handles tab separated values, and other arbitrary
    separators), it's not the right tool for use as a config file.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Dec 26, 2004
    #3
  4. Aaron

    M.E.Farmer Guest

    Here are two functions that show how to use ConfigParser.

    py>def ConfigWrite(key, value, section, cfgpath):
    .... import ConfigParser, pprint
    .... parser=ConfigParser.ConfigParser()
    .... cfg = open(cfgpath,'w')
    .... parser.add_section(section)
    .... Pretty = pprint.PrettyPrinter()#optional
    .... value = Pretty.pformat(value)#optional
    .... parser.set(section,key,value)
    .... parser.write(cfg)
    .... cfg.close()


    py>def ConfigReader(key, section, cfgpath):
    .... import ConfigParser
    .... parser=ConfigParser.ConfigParser()
    .... parser.read(cfgpath)
    .... return parser.get(section,key)
    Hope that helps...now *where* are those sugar cookies
    M.E.Farmer
     
    M.E.Farmer, Dec 26, 2004
    #4
  5. Aaron

    flamesrock Guest

    thanks for the replies, guys!

    MEFarmer, that example helps remove a lot of the confusion, thanks!
    I've looked through the SDL but there are so many different ways to do
    it, I wasn't which one was hands down best

    And Mike, I wish they would stick explanations like that in the SDL!
    Would help make breaking into the language a lot easier for newbs like
    me.

    (Spending time on these newsgroups is giving me more questions that
    answers heh )
     
    flamesrock, Dec 26, 2004
    #5
  6. Aaron

    M.E.Farmer Guest

    >thanks for the replies, guys!

    Your welcome, glad I could help.

    >(Spending time on these newsgroups is giving me more questions that
    >answers heh )

    Thats good as long as it motivates you to learn ;)
    M.E.Farmer
     
    M.E.Farmer, Dec 26, 2004
    #6
    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. Luis Esteban Valencia

    Change configuration files after installation.

    Luis Esteban Valencia, Jul 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    617
    Luis Esteban Valencia
    Jul 12, 2005
  2. Ashish

    ASP.Net 2.0 Configuration files.

    Ashish, Dec 12, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    3,167
    Scott Allen
    Dec 12, 2005
  3. =?Utf-8?B?U2F0aXNoIEJhYnUgRGFzYXJp?=

    The application's configuration files must contain 'trust' section

    =?Utf-8?B?U2F0aXNoIEJhYnUgRGFzYXJp?=, Jan 11, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,846
    =?Utf-8?B?U2F0aXNoIEJhYnUgRGFzYXJp?=
    Jan 11, 2006
  4. Mark
    Replies:
    0
    Views:
    863
  5. Stephan Koser
    Replies:
    3
    Views:
    694
    Juha Laiho
    Dec 7, 2004
Loading...

Share This Page