469,275 Members | 1,531 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Importing two module variables and having one clobber the other?

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

Mar 21 '06 #1
2 1350
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>

Mar 21 '06 #2

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

Mar 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by J. Kenney | last post: by
12 posts views Thread by qwweeeit | last post: by
4 posts views Thread by jean-marc | last post: by
4 posts views Thread by Rahul Chatterjee | last post: by
9 posts views Thread by rbygscrsepda | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.