pythonic way of making a config module

A

andrew.fabbro

I'm working on an app that will be deployed on several different
servers. In each case, I'll want to change some config info (database
name, paths, etc.)

In perl, I would have done something like this:

Package Config;
<some exporting mechanics>
$dbname = "somename";
etc.

And then use'd the module and referenced the vars as $Config::dbname.

What is a pythonic equivalent? These vars are only changed usually at
install time.

I could set up a class and then instantiate it, but that seems a
waste...I guess what I'm looking for is either (a) a way to suck
variables into a namespace keeping a prefix (without having to prefix
all the vars with something like config_) - e.g., reference them as
config.dbname or something - is this execfile? or (b) class variables,
which is the route I usually take in Java if not using preferences,
etc.

I'm aware of configParser and such, and could certainly write a module
that parses a configuration file, but using a native python module
seems the easiest. I want to do something reasonably pythonic so the
python Gods do not smite me for some stupid perlism ;)

Thanks.
 
F

Fuzzyman

I'm working on an app that will be deployed on several different
servers. In each case, I'll want to change some config info (database
name, paths, etc.)

In perl, I would have done something like this:

Package Config;
<some exporting mechanics>
$dbname = "somename";
etc.

Create a python module that defines the variables you want, and then
import it (or from it).

You can then edit that module as a config file.

That's one suggestion - with the security risks associated with an
import statement...

Alternatively, my usual reccomendation for a config file reader is
ConfigObj... ;-)

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
F

Fredrik Lundh

I'm working on an app that will be deployed on several different
servers. In each case, I'll want to change some config info (database
name, paths, etc.)

In perl, I would have done something like this:

Package Config;
<some exporting mechanics>
$dbname = "somename";
etc.

And then use'd the module and referenced the vars as $Config::dbname.

What is a pythonic equivalent? These vars are only changed usually at
install time.

I could set up a class and then instantiate it, but that seems a
waste...I guess what I'm looking for is either (a) a way to suck
variables into a namespace keeping a prefix (without having to prefix
all the vars with something like config_) - e.g., reference them as
config.dbname or something - is this execfile?

how about a plain

import config

?

where config.py contains

# configuration file
dbname = "somename"

and is used like

import config
db = db_open(config.dbname)

etc.

</F>
 
S

skip

andrew> I'm working on an app that will be deployed on several different
andrew> servers. In each case, I'll want to change some config info (database
andrew> name, paths, etc.)

andrew> In perl, I would have done something like this:
...
andrew> What is a pythonic equivalent?

Take a look at the ConfigParser module. It reads and writes Windows-style
INI files. Also, you can do something similar to what you do in Perl. It's
just that you import the config data from the places that need it instead of
exporting it from the place it's defined.

Skip
 

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
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top