Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 18th, 2005, 10:56 PM
Tom Willis
Guest
 
Posts: n/a
Default Dealing with config files what's the options

How are the expert pythoneers dealing with config files?

Is there anything similair to .net's config files or java's .properties?

A quick search on google didn't return anything that looked useful,
and I almost would expect to see some module that would be for dealing
with config information.

I can think of at least one way to do it, but I'm sure there are
shortcomings I can't see yet, and I'd rather use something someone
smarter than me has written.

I see in the logging module that there's stuff to handle configs but
seems kind of odd to have to import logging to get your config
information

Any ideas?

have I used the word config enough in this message? :)

--
Thomas G. Willis
http://paperbackmusic.net
  #2  
Old July 18th, 2005, 10:56 PM
Fuzzyman
Guest
 
Posts: n/a
Default Re: Dealing with config files what's the options

Hello Tom,


Tom Willis wrote:[color=blue]
> How are the expert pythoneers dealing with config files?
>
> Is there anything similair to .net's config files or java's[/color]
..properties?[color=blue]
>[/color]

I'm not familiar with those config file formats - but ConfigObj
certainly makes handling config files easy. It uses the ini type layout
- which you're not so fond of, although it aloows lists for values as
well.

from configobj import ConfigObj
config = ConfigObj(filename)
value1 = config['section 1']['value 1']

See http://www.voidspace.org.uk/python/configobj.html

I'm interested in suggestions as to ways to take it forward. I've just
added unicode support (still experimental - wait for the next release)
and an interface for validation. Adding nested sections using
indentation will probably be the next major feature..... (as well as
preserving user formatting when writing back files)

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml
[color=blue]
> A quick search on google didn't return anything that looked useful,
> and I almost would expect to see some module that would be for[/color]
dealing[color=blue]
> with config information.
>
> I can think of at least one way to do it, but I'm sure there are
> shortcomings I can't see yet, and I'd rather use something someone
> smarter than me has written.
>
> I see in the logging module that there's stuff to handle configs but
> seems kind of odd to have to import logging to get your config
> information
>
> Any ideas?
>
> have I used the word config enough in this message? :)
>
> --
> Thomas G. Willis
> http://paperbackmusic.net[/color]

  #3  
Old July 18th, 2005, 11:01 PM
Jorgen Grahn
Guest
 
Posts: n/a
Default Re: Dealing with config files what's the options

On Tue, 22 Feb 2005 20:38:28 -0500, Tom Willis <tom.willis@gmail.com> wrote:[color=blue]
> How are the expert pythoneers dealing with config files?[/color]
....[color=blue]
> Any ideas?[/color]

How about writing them in Python?

I have no URL handy, but it would surprise me if there wasn't a lot written
about different techniques for doing this.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
  #4  
Old July 18th, 2005, 11:01 PM
Dave Brueck
Guest
 
Posts: n/a
Default Re: Dealing with config files what's the options

Jorgen Grahn wrote:[color=blue]
> On Tue, 22 Feb 2005 20:38:28 -0500, Tom Willis <tom.willis@gmail.com> wrote:
>[color=green]
>>How are the expert pythoneers dealing with config files?[/color]
>
> ...
>[color=green]
>>Any ideas?[/color]
>
>
> How about writing them in Python?[/color]

Depending on who will be editing the config files, this can be a great approach.

At the simplest level, a config.py file like this is so easy to use:

# Net settings
timeoutSec = 5.3
maxConnections = 3

# Other stuff
foo = 'bar'

This type of a format is easy to use for just about anybody who has ever had to
use config files before. What's nice is that the code to use it is
straightforward too:

import config
conn = config.maxConnections
....

A few times I've tried to use accessor functions to ensure that the values are
present or valid or whatever, but I stopped doing that because in practice it's
just not needed (again, for users who are familiar with the concept of config
files).

A slightly more elaborate approach gives you full structure:

class Net:
maxConnections = 12

class System:
class Logging:
root = '/var/logs'

This prevents individual setting names from getting unwieldy, and the code that
uses it can be pretty readable too:

logRoot = config.System.Logging.root

or, if there are lots of retrievals to do:

Logging = config.System.Logging
logRoot = Logging.root
.... etc ...

Using classes asks a little bit more of the users (they can break it a little
more easily), but again, in practice it really hasn't been a problem at all.

-Dave
  #5  
Old July 18th, 2005, 11:01 PM
Tom Willis
Guest
 
Posts: n/a
Default Re: Dealing with config files what's the options

On Fri, 25 Feb 2005 15:02:04 -0700, Dave Brueck
<dave@pythonapocrypha.com> wrote:[color=blue]
> Jorgen Grahn wrote:[color=green]
> > On Tue, 22 Feb 2005 20:38:28 -0500, Tom Willis <tom.willis@gmail.com> wrote:
> >[color=darkred]
> >>How are the expert pythoneers dealing with config files?[/color]
> >
> > ...
> >[color=darkred]
> >>Any ideas?[/color]
> >
> >
> > How about writing them in Python?[/color]
>
> Depending on who will be editing the config files, this can be a great approach.
>
> At the simplest level, a config.py file like this is so easy to use:
>
> # Net settings
> timeoutSec = 5.3
> maxConnections = 3
>
> # Other stuff
> foo = 'bar'
>
> This type of a format is easy to use for just about anybody who has ever had to
> use config files before. What's nice is that the code to use it is
> straightforward too:
>
> import config
> conn = config.maxConnections
> ...
>
> A few times I've tried to use accessor functions to ensure that the values are
> present or valid or whatever, but I stopped doing that because in practice it's
> just not needed (again, for users who are familiar with the concept of config
> files).
>
> A slightly more elaborate approach gives you full structure:
>
> class Net:
> maxConnections = 12
>
> class System:
> class Logging:
> root = '/var/logs'
>
> This prevents individual setting names from getting unwieldy, and the code that
> uses it can be pretty readable too:
>
> logRoot = config.System.Logging.root
>
> or, if there are lots of retrievals to do:
>
> Logging = config.System.Logging
> logRoot = Logging.root
> ... etc ...
>
> Using classes asks a little bit more of the users (they can break it a little
> more easily), but again, in practice it really hasn't been a problem at all.
>
> -Dave
> --
> http://mail.python.org/mailman/listinfo/python-list
>[/color]
I actually thought of this, and I was kind of on the fence due to the
intended audience.

I don't think it's too much to ask that they are comfy with the
concept of variables. I mean, if it was a shell script they'd be at
the top of the file anyway.

Then again I'm some what hesitant to help them make the connection
that I'm giving them the ability to indirectly edit the code. Kind of
like opening pandoras box. Once the figure out they can open any file
(*.py) with notepad, there will be utter anarchy and I'll get the call
at 4am that somethings wrong with the production data.



--
Thomas G. Willis
http://paperbackmusic.net
  #6  
Old July 18th, 2005, 11:01 PM
Dave Brueck
Guest
 
Posts: n/a
Default Re: Dealing with config files what's the options

Tom Willis wrote:[color=blue]
> On Fri, 25 Feb 2005 15:02:04 -0700, Dave Brueck[color=green][color=darkred]
>>>How about writing them in Python?[/color]
>>
>>Depending on who will be editing the config files, this can be a great approach.[/color][/color]
[snip][color=blue]
> I actually thought of this, and I was kind of on the fence due to the
> intended audience.
>
> I don't think it's too much to ask that they are comfy with the
> concept of variables. I mean, if it was a shell script they'd be at
> the top of the file anyway.
>
> Then again I'm some what hesitant to help them make the connection
> that I'm giving them the ability to indirectly edit the code. Kind of
> like opening pandoras box. Once the figure out they can open any file
> (*.py) with notepad, there will be utter anarchy and I'll get the call
> at 4am that somethings wrong with the production data.[/color]

If you're giving them an executable (i.e. py2exe'd), then you can just exclude
config.py from the exe.

Either way, if you're not so worried about malicious breakage but ignorant
breakage, then you could always name your config file something like
'options.cfg' and then:

import new, sys

# Do this at app startup
sys.modules['config'] = new.module('config')
exec file('options.cfg').read() in config.__dict__

# All other modules do this
import config
conn = config.maxConnections
.... etc ...

-Dave
 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles