473,326 Members | 2,090 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,326 software developers and data experts.

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 1095
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*********@kuling.itea.ntnu.no=?iso-8859-1?q?Robin_K=E5veland?= Hansen <ka*******@gmail.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
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.
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
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
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...
25
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...
10
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...
8
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.