473,581 Members | 2,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python noob's simple config problem

kj

I'm sure this is a simple, but recurrent, problem for which I can't
hit on a totally satisfactory solution.

As an example, suppose that I want write a module X that performs
some database access. I expect that 99.999% of the time, during
the foreseeable future, the database connection parameters will
remain unchanged. The only exception that I envision for this
would be during testing or debugging.

Given all this, I am tempted to turn these connection parameters
into hard-coded module attributes that I can always override (i.e.
overwrite) when necessary.

But for as long as I can remember the dogma has been that hard-coded
values are bad, and that one should use other techniques, such as
configuration files, or parameters to a suitable constructor, etc.

This is where I begin to get confused: whose responsibility is it
to know of and read the config file? I can think of two distinct
scenarios: 1) module X is being used by a large, full-fledged
application A that already uses a config file for its own configuration;
2) module X is being used by a simple script that has no need for
a config file. In case 1 I'd be glad to let application A set
module X's connection parameters using values read from its own
(i.e. A's) config file; this minimizes the number of config files
that need to be maintained. In case 2, however, it would be
preferable for module X to read its connection params from its own
(i.e. X's) config file. In this way the script won't have to bother
setting some parameters that are in fact practically constant...

After going round and round on this, my original idea of hard-coding
the values as module attributes begins to look pretty attractive
again.

How would you handle such situations?

Thanks!

kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #1
2 1107
On Thu, 12 Jun 2008 21:32:34 +0000, kj wrote:
I'm sure this is a simple, but recurrent, problem for which I can't hit
on a totally satisfactory solution.

As an example, suppose that I want write a module X that performs some
database access. I expect that 99.999% of the time, during the
foreseeable future, the database connection parameters will remain
unchanged. The only exception that I envision for this would be during
testing or debugging.

Given all this, I am tempted to turn these connection parameters into
hard-coded module attributes that I can always override (i.e. overwrite)
when necessary.

But for as long as I can remember the dogma has been that hard-coded
values are bad, and that one should use other techniques, such as
configuration files, or parameters to a suitable constructor, etc.

This is where I begin to get confused: whose responsibility is it to
know of and read the config file? I can think of two distinct
scenarios: 1) module X is being used by a large, full-fledged
application A that already uses a config file for its own configuration;
2) module X is being used by a simple script that has no need for a
config file. In case 1 I'd be glad to let application A set module X's
connection parameters using values read from its own (i.e. A's) config
file; this minimizes the number of config files that need to be
maintained. In case 2, however, it would be preferable for module X to
read its connection params from its own (i.e. X's) config file. In this
way the script won't have to bother setting some parameters that are in
fact practically constant...

After going round and round on this, my original idea of hard-coding the
values as module attributes begins to look pretty attractive again.

How would you handle such situations?

Thanks!

kynn
I think I would just abstract it away with a "getter" for the connection,
a function that takes some optional parameters, if not supplied, it
simply fetches them from a default configuration. Ie:

def connect(params= None):
if params is None:
return dblayer.connect (conf["default"])
else:
return dblayer.connect (params)

Unless I have misunderstood you completely? Now people can change your
scripts config file, and if someone wants to use your code, they can use
the getter directly.

I hope this is of some help.
Jun 27 '08 #2
kj
In <g2*********@ku ling.itea.ntnu. no=?iso-8859-1?q?Robin_K=E5v eland?= Hansen <ka*******@gmai l.comwrites:
>On Thu, 12 Jun 2008 21:32:34 +0000, kj wrote:
>I'm sure this is a simple, but recurrent, problem for which I can't hit
on a totally satisfactory solution.

As an example, suppose that I want write a module X that performs some
database access. I expect that 99.999% of the time, during the
foreseeable future, the database connection parameters will remain
unchanged. The only exception that I envision for this would be during
testing or debugging.

Given all this, I am tempted to turn these connection parameters into
hard-coded module attributes that I can always override (i.e. overwrite)
when necessary.

But for as long as I can remember the dogma has been that hard-coded
values are bad, and that one should use other techniques, such as
configuratio n files, or parameters to a suitable constructor, etc.

This is where I begin to get confused: whose responsibility is it to
know of and read the config file? I can think of two distinct
scenarios: 1) module X is being used by a large, full-fledged
application A that already uses a config file for its own configuration;
2) module X is being used by a simple script that has no need for a
config file. In case 1 I'd be glad to let application A set module X's
connection parameters using values read from its own (i.e. A's) config
file; this minimizes the number of config files that need to be
maintained. In case 2, however, it would be preferable for module X to
read its connection params from its own (i.e. X's) config file. In this
way the script won't have to bother setting some parameters that are in
fact practically constant...

After going round and round on this, my original idea of hard-coding the
values as module attributes begins to look pretty attractive again.

How would you handle such situations?

Thanks!

kynn
>I think I would just abstract it away with a "getter" for the connection,
a function that takes some optional parameters, if not supplied, it
simply fetches them from a default configuration. Ie:
>def connect(params= None):
if params is None:
return dblayer.connect (conf["default"])
else:
return dblayer.connect (params)
>Unless I have misunderstood you completely? Now people can change your
scripts config file, and if someone wants to use your code, they can use
the getter directly.
>I hope this is of some help.
Thanks!

Kynn

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jun 27 '08 #3

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

Similar topics

6
2756
by: Ara.T.Howard | last post by:
hi- i know nada about python so please forgive me if this is way off base. i'm trying to fix a bug in MoinMoin whereby WordsWithTwoCapsInARowLike ^^ ^^ ^^
9
4508
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our purposes. I have to admit that, although I'm very familiar with Python, I'm complete Perl noob (and I hope to stay one) which is reflected in my questions....
25
2131
by: Brian | last post by:
Had a unsettling conversation with a CS instructor that teaches at local high schools and the community college. This person is a long-term Linux/C/Python programmer, but he claims that the install, config, and library models for C# have proved to be less problematic than Python. So both his courses (intro, data structs, algorithms) are...
10
3242
by: sandipm | last post by:
Hi, In my application, I have some configurable information which is used by different processes. currently I have stored configration in a conf.py file as name=value pairs, and I am importing conf.py file to use this variable. it works well import conf print conf.SomeVariable but if I need to change some configuration parameteres, it...
8
1584
by: Derek Martin | last post by:
I'd like to know if it's possible to code something in Python which would be equivalent to the following C: ---- debug.c ---- #include <stdio.h> bool DEBUG;
0
7883
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
7808
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8312
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7917
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5685
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
5366
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
3809
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1413
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
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.