473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Jul 18 '05 #1
5 1613
Hello Tom,
Tom Willis wrote:
How are the expert pythoneers dealing with config files?

Is there anything similair to .net's config files or java's ..properties?
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
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


Jul 18 '05 #2
On Tue, 22 Feb 2005 20:38:28 -0500, Tom Willis <to********@gmail.com> wrote:
How are the expert pythoneers dealing with config files? .... Any ideas?


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!
Jul 18 '05 #3
Jorgen Grahn wrote:
On Tue, 22 Feb 2005 20:38:28 -0500, Tom Willis <to********@gmail.com> wrote:
How are the expert pythoneers dealing with config files?


...
Any ideas?

How about writing them in Python?


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
Jul 18 '05 #4
On Fri, 25 Feb 2005 15:02:04 -0700, Dave Brueck
<da**@pythonapocrypha.com> wrote:
Jorgen Grahn wrote:
On Tue, 22 Feb 2005 20:38:28 -0500, Tom Willis <to********@gmail.com> wrote:
How are the expert pythoneers dealing with config files?


...
Any ideas?

How about writing them in Python?


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

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
Jul 18 '05 #5
Tom Willis wrote:
On Fri, 25 Feb 2005 15:02:04 -0700, Dave Brueck
How about writing them in Python?
Depending on who will be editing the config files, this can be a great approach.

[snip] 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.


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
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply*...
12
by: Rob Cranfill | last post by:
Hello, I've successfully coded Python to do what I want with a RotatingFileHandler, but am having trouble getting the same behavior via a config file. I wanted to create one log file each...
4
by: Jon Maz | last post by:
Hi All, I'm getting to grips with PHP and Apache (on Windows for current development, production site will be on Linux), and have a simple (I hope!) request. When you change the httpd.conf...
22
by: Daniel Billingsley | last post by:
Ok, I wanted to ask this separate from nospam's ridiculous thread in hopes it could get some honest attention. VB6 had a some simple and fast mechanisms for retrieving values from basic text...
25
by: n3crius | last post by:
hi, i just got a web host with asp.net , seemed really cool. aspx with the c# or vb IN the actual main page run fine, but when i use codebehind and make another source file ( a .cs) to go with...
19
by: Linda | last post by:
In classic ASP I used to have a file called settings.asp included on every page of my web, it consisted of a number of different settings unique to this application, among them the database path...
3
by: =?Utf-8?B?Tm9ybUQ=?= | last post by:
It isn't clear this is the right place to post this, but it IS related to ..NET CONFIG files... Windows Explorer (at least on W2003Server) doesn't seem to know that .config files exist. ...
21
by: Nick Craig-Wood | last post by:
Lance Gamet <lance@gamet.comwrote: I've found http://docs.python.org/lib/module-ConfigParser.html To be easy to use and built in. It makes human readable / editable ..ini - like files. ...
10
by: =?Utf-8?B?TUNN?= | last post by:
When creating a new VB Web Application Project with VS2008, there are several settings (compiler settings, option strict, etc) that appear both in the web.config file and "My Project". I'm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.