Initializing defaults to module variables

B

Burton Samograd

Hi,

I'm writing an app that stores some user configuration variables in a
file ~/.program/config, which it then imports like so:

import sys
from posix import environ
sys.path.append(environ["HOME"]+"/.program")
import config

I can then access the configuration through code like:

login(config.username)

My question is, how can I setup my program defaults so that they can
be overwritten by the configuration variables in the user file (and so
I don't have to scatter default values all over my code in try/catch
blocks)? Basically, I would like to be able to do something like this:

config.login = "noone"
config.password = "secret"
.
.
.
import sys
from posix import environ
sys.path.append(environ["HOME"]+"/.program")
import config

So I guess the real question is:

Is there a way to create a module namespace and populate it
before sourcing the file?
 
K

Kent Johnson

Burton said:
Hi,

I'm writing an app that stores some user configuration variables in a
file ~/.program/config, which it then imports like so:

import sys
from posix import environ
sys.path.append(environ["HOME"]+"/.program")
import config

I can then access the configuration through code like:

login(config.username)

My question is, how can I setup my program defaults so that they can
be overwritten by the configuration variables in the user file (and so

You could use the ConfigParser module instead of putting your config in
a Python module. ConfigParser allows specifying defaults.

Kent
 
E

Eric Deveaud

Burton said:
Hi,

I'm writing an app that stores some user configuration variables in a
file ~/.program/config, which it then imports like so:

import sys
from posix import environ
sys.path.append(environ["HOME"]+"/.program")
import config

I can then access the configuration through code like:

login(config.username)

better use some plain text configuration file
check ConfigParser documentation.
So I guess the real question is:

Is there a way to create a module namespace and populate it
before sourcing the file?

if you take a look at how naming space is affected by the variants
"from module import *" and his conterpart "import module", you will have your
answer.

shortly

if you use "import foo" all the variables/class/functions comming from the
module foo are located in a globlal space dictionary named foo

if you use "form foo import *" all variables/class/functions comming from the
foo module are located in gloabal space naming dictionary.


try this

-=-=-=-=-=-=- consider module foo.py -=-=-=-=-=-=-

my_var = "my var from foo"

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


hebus:~/tmp > python
Python 2.4.2 (#1, Mar 22 2006, 12:59:23)
[GCC 3.3.5 (Debian 1:3.3.5-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'my_var' is not defined

and now

hebus:~/tmp > python
Python 2.4.2 (#1, Mar 22 2006, 12:59:23)
[GCC 3.3.5 (Debian 1:3.3.5-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.my var from foo


this will lead to a solution regarding your problem

hebus:~/tmp > python
Python 2.4.2 (#1, Mar 22 2006, 12:59:23)
[GCC 3.3.5 (Debian 1:3.3.5-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.my var from foo


once again, I bet your best choice will be to have some plain text config file

Eric
 
L

Luke Plant

Burton said:
My question is, how can I setup my program defaults so that they can
be overwritten by the configuration variables in the user file (and so
I don't have to scatter default values all over my code in try/catch
blocks)?

The Django web framework happens to do something very like this.
Instead of 'import config' you have to do 'from django.conf import
settings', and you have an environment variable that allows you to
specify the user settings file, but otherwise I think it is pretty much
the same.

The guts of the mechanism is here:

http://code.djangoproject.com/browser/django/branches/magic-removal/django/conf/__init__.py

It has quite a bit of custom Django stuff in there that you can ignore,
but I think the principles should apply.

Luke
 
B

Burton Samograd

Burton Samograd said:
Is there a way to create a module namespace and populate it
before sourcing the file?

A reply to my own question, but I thought I would share the answer. I
got it to work with the following code:

import config
import sys
from posix import environ
sys.path.insert(0, environ["HOME"]+"/.program")
reload(config)

I have a file in my working directory that contains all the program
variables with default values called config.py, which is loaded at the
start, and then the user configuration directory is inserted into the
load path and then their config.py is loaded, which will then
overwrite the default values.
 
F

Fredrik Lundh

Burton said:
A reply to my own question, but I thought I would share the answer. I
got it to work with the following code:

import config
import sys
from posix import environ
sys.path.insert(0, environ["HOME"]+"/.program")
reload(config)

I have a file in my working directory that contains all the program
variables with default values called config.py, which is loaded at the
start, and then the user configuration directory is inserted into the
load path and then their config.py is loaded, which will then
overwrite the default values.

since you know the name of the config file you're looking for, you can
simplify (and unweirdify) your code a bit by changing your config file to
look like this:

# File: config.py

#
# configuration defaults

some_param = "value"
some_other_param = 1

#
# get user overrides

import os
try:
execfile(os.path.expanduser("~/.program/config.py"))
except IOError:
pass # ignore missing config file, but not syntax errors

# end of file

with this in place, you just have to do

import config

in any application module than needs to access the configuration data.

</F>
 
B

Burton Samograd

Fredrik Lundh said:
since you know the name of the config file you're looking for, you can
simplify (and unweirdify) your code a bit by changing your config file to
look like this:

# File: config.py

#
# configuration defaults

some_param = "value"
some_other_param = 1

#
# get user overrides

import os
try:
execfile(os.path.expanduser("~/.program/config.py"))
except IOError:
pass # ignore missing config file, but not syntax errors

# end of file

with this in place, you just have to do

import config

in any application module than needs to access the configuration
data.

nice, that looks to be just what I was looking for. thanks.
 

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
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top