[Tutor] Global presets ?

Alan Gauld alan.gauld at freenet.co.uk
Sat Dec 4 12:55:52 CET 2004


> 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.

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...

> 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 :)

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


#############
# File: prog1.py

import myvars

localvar = myvars.value1
myvars.value2 = 'Alan'


##############
#  File prog2.py

import myvars

newvar = myvars.value2

print myvars.value1 - 27
##############

Does that help?

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



More information about the Tutor mailing list