Kent Johnson said unto the world upon 2004-12-04 06:21:
You are on the right track. Put your common definitions in a configuration module like this:

# Config.py
arch_data_dir='/home/dave/mygg/gg1.3/arch_data'
data_dir='/home/dave/mygg/gg1.3/data'

Then in client code, import Config. When you use the names defined in Config you have to prefix them with the module name like this:

<SNIP>

Alternately you can use either of these forms:

<SNIP>

or this:

# Get *all* names defined in Config into our global namespace
from Config import *

to make the bare names available in the client.

Kent

Dave S wrote:

Hi there,

I have some common data directories, like

/home/dave/mygg/gg1.3/logs
/home/dave/mygg/gg1.3/data
/home/dave/mygg/gg1.3/datacore
/home/dave/mygg/gg1.3/arch_data

which increasing numbers of scripts are accessing. At the begining of each script
I end up putting in declarations like


arch_data_dir='/home/dave/mygg/gg1.3/arch_data'
data_dir='/home/dave/mygg/gg1.3/data'
....

over & over. This is OK until I want to move a directory

Somewhere I read about importing a script to define common globals for all the scripts that import it.

I tried this, and failed - the variable was only valid for the module, to be expected really :)

Can anyone make a suggestion howto set up common global presets.

Cheers
Dave




Hi Dave, Kent, and all,

I have a caution about the
 from Config import *
idiom that Kent didn't mention.

It can lead to namespace pollution, in that if you have a module 'foo' with a name 'bar' and you are witting a script which says
from foo import *
you have to be very careful that your script doesn't also assign to the name 'bar', else you may end up thinking you have two different things available when you don't. ('bar' will either point to your script's bar or to Config.bar, depending on whether you imported Config before or after your scripts assignment to bar.)


The first time this bites you, it can eat up hours of your life. (But I'm not bitter;-)

I avoid this by using the
 import examplemodule as em

That imports everything so that you accesses it by
 em.some_name
rather than
 examplemodule.some_name

I find that really handy for the handful of utility modules I import into most of my scripts. Then, I just have to be sure to avoid a small set of names -- 'em' in this case. And my python files have nice descriptive names, but I only have to type then once.

Best,

Brian vdB

_______________________________________________
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to