Re: [Tutor] Global presets ?

2004-12-04 Thread Kent Johnson
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:

import Config
print Config.data_dir
Alternately you can use either of these forms:
# Get a couple of names from Config into our global namespace
from Config import arch_data_dir, data_dir
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

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


Re: [Tutor] Global presets ?

2004-12-04 Thread Dave S
Thanks Guys,
They are both good ways of getting round my problem, I appreciate your 
input  will have a play.

Cheers
Dave
:-) :-) :-) :-)
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Global presets ?

2004-12-04 Thread Dave S
Alan Gauld wrote:
have you considered making the root directory an environment variable?
That way you can read the value (os.getenv) at the start of the
script.
And if you ever need to move the structure you can simply change the
environment value. It also means different users can use their own
structures by defining their own environment value...
 


# File myvars.py
value1 = 42
value2 = 'spam'
 

Got ya so far ..
#
# File: prog1.py
import myvars
localvar = myvars.value1
myvars.value2 = 'Alan'
 

Never thought of setting 'myvars.value2 = 'Alan''  I guess this would 
just set the variable in the myvars namespace since it could not change 
myvars.py itself.

##
#  File prog2.py
import myvars
newvar = myvars.value2
 

With you ...
print myvars.value1 - 27
 

Have I misunderstood, should this not be 42 ? Typo or me not understanding ?

##
Does that help?
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

 

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