473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1452
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: J. Kenney | last post by:
Good Morning, Is there a way for a function called within an _imported_ library to call back to _calling_ python program to run a function? (Shown below) Also can you overload the print...
12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
4
by: jean-marc | last post by:
As an application programmer, I'm not well versed in the material aspects of computing (memory, cpu, bus and all). My understanding of imports in Python is such: the __main__ program is the center...
4
by: Rahul Chatterjee | last post by:
Hello All I have 2 websites both using different style sheets (.css). The stylesheets are stored in a session variable and get set at the time the site gets invoked. What is happening is...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
1
by: D Mat | last post by:
Hi, I'm trying to get MS Access 2000 to automatically import a series of (~200) flat text, tab delimited, data files into a single Access table, with consistent fields and rows. The files have...
9
by: rbygscrsepda | last post by:
Hi, I'm a newbie at Python. :) Right now it's not letting me import * from any relative package name--i.e., a name that starts with a dot. For instance, none of the following work: from . import...
4
by: rshepard | last post by:
I'm stymied by what should be a simple Python task: accessing the value of a variable assigned in one module from within a second module. I wonder if someone here can help clarify my thinking. I've...
3
by: rs387 | last post by:
Hi, I've found the following behaviour on importing a variable from a module somewhat odd. The behaviour is identical in Python 2.5 and 3.0b2. In summary, here's what happens. I have a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.