Configuration Files

A

Aaron

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
 
M

M.E.Farmer

Aaron said:
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

Mike Meyer

Aaron said:
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
 
M

M.E.Farmer

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
 
F

flamesrock

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 )
 
M

M.E.Farmer

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top