Importing two module variables and having one clobber the other?

J

Joseph Turian

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
 
F

Fredrik Lundh

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

Joseph Turian

Fredrik said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top