Importing two module variables and having one clobber the other?

Fredrik Lundh fredrik at pythonware.com
Tue Mar 21 16:33:16 EST 2006


Joseph Turian wrote

> Here's the functionality I need:
>
> We allow the user to define parameter values, which are imported and
> can be accessed directly as variables within Python. These are defined
> in "parameters.py".
>
> More specifically, let's say the user creates "dir/parameters.py" and
> "dir/subdir/parameters.py" and then invokes the program from within
> dir/subdir. We first import the values from dir/parameters.py and then
> import the values from dirs/subdirs/parameters.py, potentially
> clobbering any values from the first import.
>
> How can I achieve this functionality?
> Here's what I have in mind:
> * Find every directory from os.environ["HOME"] through os.getcwd()

I assume "from" means "beneath" and "getcwd" means "walk" ?

> * Find all such directories in which parameters.py exists.
> * "from parameters import *", starting from the highest-level directory
> through the current directory.

the problem with this approach is that the user may, accidentally or on
purpose, clobber portions of your program as well.

here's a more robust approach:

    parameters = {}

    for file in list_of_parameter_files:
        try:
            execfile(file, parameters)
        except:
            print "error in", file
            traceback.print_exc()

    # when you get here, parameters contains all configuration
    # params (e.g. parameters["foo"], parameters["bar"]).

if you prefer to use a "parameters.value" syntax, you can wrap the resulting
dictionary in a class.  if you insist on using a "value" syntax, you can add the
values to the module namespace:

    mod = sys.modules[__name__]
    for key, value in parameters.items():
        if key in list_of_allowed_parameter_names:
            setattr(mod, key, value)

to make the values available as globals in all modules, you can do

    import __builtin__
    for key, value in parameters.items():
        if key in list_of_allowed_parameter_names:
            setattr(__builtin__, key, value)

but that's a bit ugly.

</F>






More information about the Python-list mailing list