473,468 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pythonic way of making a config module

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.

Jan 18 '06 #1
3 1531

andrew.fab...@gmail.com wrote:
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

Jan 18 '06 #2

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
Jan 18 '06 #3
an***********@gmail.com wrote:
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>

Jan 18 '06 #4

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

Similar topics

2
by: zapazap | last post by:
-- Pythonic control of Windows GUI application: tabs and listviews Dear Snake-charming Gurus, I have learned to manipulate some controls ("Button", "ComboBox", etc) through such means as: ...
11
by: Charles Krug | last post by:
I've a function that needs to maintain an ordered sequence between calls. In C or C++, I'd declare the pointer (or collection object) static at the function scope. What's the Pythonic way to...
3
by: David MacKay | last post by:
Dear Greater Py, <motivation note="reading this bit is optional"> I am writing a command-line reader for python. I'm trying to write something with the same brevity as perl's one-liner ...
4
by: Carl J. Van Arsdall | last post by:
It seems the more I come to learn about Python as a langauge and the way its used I've come across several discussions where people discuss how to do things using an OO model and then how to design...
9
by: Daniel Nogradi | last post by:
Is it possible to pass a python object to a python program as argument? In my program I would like to start executing an other python program and don't wait until that finishes, only launch it...
33
by: Gregory Petrosyan | last post by:
Buenos dias, amigos! I have to write _simple_ gui library, for embedding into game. My first attempt was to use XML: isn't it cute to describe ui in such a way: <window> <title>Hello...
0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
25
by: Alex Popescu | last post by:
Hi all! I am pretty sure this has been asked a couple of times, but I don't seem to find it on the archives (Google seems to have a couple of problems lately). I am wondering what is the most...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.