Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Configuration Files

Reply
Thread Tools

Configuration Files

 
 
Aaron
Guest
Posts: n/a
 
      12-26-2004
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
 
Reply With Quote
 
 
 
 
M.E.Farmer
Guest
Posts: n/a
 
      12-26-2004
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

 
Reply With Quote
 
 
 
 
Mike Meyer
Guest
Posts: n/a
 
      12-26-2004
Aaron <(E-Mail Removed)> 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 <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
M.E.Farmer
Guest
Posts: n/a
 
      12-26-2004
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

 
Reply With Quote
 
flamesrock
Guest
Posts: n/a
 
      12-26-2004
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 )

 
Reply With Quote
 
M.E.Farmer
Guest
Posts: n/a
 
      12-26-2004
>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicK Mike ASP .Net 5 08-15-2007 08:57 AM
Enterprise Library exception: The configuration section securityCryptographyConfiguration is not defined in the current configuration for the AppDomain. Jess Chadwick ASP .Net 1 09-21-2006 09:18 AM
Configuration reverted to previous configuration after power loss ward@sciinc.com Cisco 0 03-03-2006 04:14 PM
Microsoft Configuration Block and Enterprise library configuration tool Mark ASP .Net 0 02-15-2006 11:27 PM
PEAP Configuration Woes - PEAP configuration help jester Cisco 1 12-20-2005 02:04 PM



Advertisments