473,587 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ 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 1538

andrew.fab...@g mail.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***********@g mail.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
3886
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: win32gui.SendMessage(hwnd, win32con.BM_GETCHECK, 0 ,0) win32gui.SendMessage(hwnd, win32con.CB_GETCOUNT, 0 ,0)
11
5035
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 do this? Is there a better solution than putting the sequence at module scope?
3
3129
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 eval "\$$1=\$2" while @ARGV && $ARGV=~ /^(\w+)=(.*)/ && shift;
4
1790
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 software in a more "Pythonic" way. My question is, should we as python developers be trying to write code that follows more of a python standard...
9
1655
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 and keep running the original program. The obvious way I can think of it is using the exec* or spawn* function families. However, these are generic...
33
2273
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 World!</title> <image text="nice picture here" pos=... src=... /> <text opts=...> (some text here)
0
1849
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 want to put this comment and a alternative most simple default framework for discussion. Maybe there are more Python users which like to see that...
25
4677
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 pythonic way of dealing with missing keys and default values. According to my readings one can take the following approaches:
3
6409
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 logging.config.fileConfig() function. Let say my logging class is called "MyLogHandler" and it's in a module called "mylogmodule", I want to be able to...
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
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 we have to send another system
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.