Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Importing two module variables and having one clobber the other?

32 views
Skip to first unread message

Joseph Turian

unread,
Mar 21, 2006, 3:11:36 PM3/21/06
to
Can I simulate the behavior of "from foo import *" using
imp.load_module()?

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()
* Find all such directories in which parameters.py exists.
* "from parameters import *", starting from the highest-level directory
through the current directory.

Thanks!
Joseph

Fredrik Lundh

unread,
Mar 21, 2006, 4:33:16 PM3/21/06
to pytho...@python.org
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>

Joseph Turian

unread,
Mar 21, 2006, 5:05:49 PM3/21/06
to

Fredrik Lundh wrote:

> if you prefer to use a "parameters.value" syntax, you can wrap the resulting
> dictionary in a class.

That sounds good. How do I do that?

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

Actually:
def superdirs(d):
lst = [d]
while d != os.environ["HOME"]:
(d, tl) = os.path.split(d)
lst += [d]
lst.reverse()
return lst


Joseph

0 new messages