Parametrized module import
Oliver Fromme
olli at haluter.fromme.com
Thu Jul 8 05:27:18 EDT 2004
Jacek Generowicz <jacek.generowicz at cern.ch> wrote:
> I have a module whose behaviour needs to be configurable. The module
> needs to decide, the first time it is imported, beteween alternative
> interfaces it presents.
>
> Currently, I set some environment variables which select the desired
> behaviour, and the module inspects those variables to determine the
> mode in which it should set itself up. I would prefer a pure Python
> solution, rather than one which depends on external state.
>
> Can you recommend any approaches, or warn against the pitfalls of some
> approaches?
You could create another module called "config" or "cfg"
which contains some global variables. You import it into
your configurable module as well as into your main program.
Then you can configure the module's behaviour via those
global variables before importing the module.
Something like this:
#--- File config.py: ---
foo_interface_mode = 0 # default
#--- File your_module.py: ---
import config
if config.foo_interface_mode == 0:
... this interface
else:
... that interface
#--- File main_program.py: ---
import config
config.foo_interface_mode = 1
import your_module
Best regards
Oliver
--
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany
``All that we see or seem is just a dream within a dream.''
(E. A. Poe)
More information about the Python-list
mailing list