Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Parametrized module import

Reply
Thread Tools

Parametrized module import

 
 
Jacek Generowicz
Guest
Posts: n/a
 
      07-08-2004
anton muhin <(E-Mail Removed)> writes:

> Of course, this solution might not fit your needs.


In this case, I am afraid that it does not.
 
Reply With Quote
 
 
 
 
Heather Coppersmith
Guest
Posts: n/a
 
      07-08-2004
On 08 Jul 2004 14:22:25 +0200,
Jacek Generowicz <(E-Mail Removed)> wrote:

> Heather Coppersmith <(E-Mail Removed)> writes:
>> Upon import, a module's "top level" code is executed,


> Yes, but only on the _first_ import. On subsequent imports the system
> notices that the module has already imported, and doesn't execute
> anything in the module, it just binds a name to the already imported
> module ...


>> so try a variation on this theme at the top level of your module:
>>
>> i_am_configured = False
>>
>> if i_am_configured:
>> print 'i am already configured'
>> else:
>> import my_configuration_module
>> configure_myself( )
>> i_am_configured = True


> ... so the first branch will _never_ get executed.


> My question is exactly about this problem: nothing gets executed on
> the second import, so there's nowhere for me to put the "already
> configured" message.


Oops--yes, you are correct. I apologize. My quickie tests were
apparently less than sufficient. [slaps self on wrist, and then
on forehead]

Regards,
Heather

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
 
Reply With Quote
 
 
 
 
John Lenton
Guest
Posts: n/a
 
      07-08-2004
On 08 Jul 2004 16:28:21 +0200, Jacek Generowicz
<(E-Mail Removed)> wrote:
>
> Yup ... but now I'm being put under pressure to make the API thus:
>
> import foo
> foo.config....
>
> which doesn't thrill me at all, for a plethora of implementation
> detail related reasons which are not interesting here.
>
> Thanks for the poniter, anyway.


How ugly is this:

import sys

def __config():
return sys._getframe(2).f_globals.get('__magic_config', None)

def config(name):
name = str(name)
if name not in __mapping:
raise RuntimeException, "unknown flavor " + name
sys._getframe(1).f_globals['__magic_config'] = name

def __default_bar(*a, **kw):
print "I am the default"

def __white_bar(*a, **kw):
print "I am white"

def __black_bar(*a, **kw):
print "I am black"

__mapping = {'bar': {None: __default_bar,
'white': __white_bar,
'black': __black_bar},
}

for i in __mapping:
globals()[i] = lambda *a, **kw: __mapping[i][__config()](*a, **kw)

I'd say about 6 in a fuglyness scale, but it might do what you want.
I'm sure there's an easyer way of putting things into the current
namespace, but this works.

--
John Lenton ((E-Mail Removed)) -- Random fortune:
bash: fortune: command not found
 
Reply With Quote
 
John Lenton
Guest
Posts: n/a
 
      07-08-2004
On 08 Jul 2004 16:28:21 +0200, Jacek Generowicz
<(E-Mail Removed)> wrote:
>
> Yup ... but now I'm being put under pressure to make the API thus:
>
> import foo
> foo.config....
>
> which doesn't thrill me at all, for a plethora of implementation
> detail related reasons which are not interesting here.
>
> Thanks for the poniter, anyway.


How ugly is this:

import sys

def __config():
return sys._getframe(2).f_globals.get('__magic_config', None)

def config(name):
name = str(name)
if name not in __mapping:
raise RuntimeException, "unknown flavor " + name
sys._getframe(1).f_globals['__magic_config'] = name

def __default_bar(*a, **kw):
print "I am the default"

def __white_bar(*a, **kw):
print "I am white"

def __black_bar(*a, **kw):
print "I am black"

__mapping = {'bar': {None: __default_bar,
'white': __white_bar,
'black': __black_bar},
}

for i in __mapping:
globals()[i] = lambda *a, **kw: __mapping[i][__config()](*a, **kw)

I'd say about 6 in a fuglyness scale, but it might do what you want.
I'm sure there's an easyer way of putting things into the current
namespace, but this works.

--
John Lenton ((E-Mail Removed)) -- Random fortune:
bash: fortune: command not found
 
Reply With Quote
 
John Lenton
Guest
Posts: n/a
 
      07-08-2004
On Thu, 8 Jul 2004 13:25:25 -0300, John Lenton <(E-Mail Removed)> wrote:
> raise RuntimeException, "unknown flavor " + name


that should've been RuntimeError, and flavour.

--
John Lenton ((E-Mail Removed)) -- Random fortune:
bash: fortune: command not found
 
Reply With Quote
 
Oliver Fromme
Guest
Posts: n/a
 
      07-08-2004
Jacek Generowicz <(E-Mail Removed)> wrote:
> But this doesn't quite solve the problem I'm trying to solve, which is
> to warn, at the time the import statement is executed, that the
> configuration which is in place is not being respected.


I see.

> There's nothing wrong with the user changing the mode twenty times
> before the first import. I guess the imported module could block
> further configuration changes, and the warning can come when you try
> to change the configuration _after_ the first import.


In that case, why don't you let the imported module change
the definition of the function in the "config" module?
That's exactly the place after which modifications should
be disallowed, if I understand you correctly.

Third try.

#--- File config.py: ---
foo_interface_mode = 0 # default
def set_foo_interface_mode (mode):
foo_interface_mode = mode

#--- File your_module.py: ---
import config

def config_warn():
print "foo_interface_mode may only be set once!"

config.set_foo_interface_mode = config_warn

if config.foo_interface_mode == 0:
... this interface
else:
... that interface

#--- File main_program.py: ---
import config
config.set_foo_interface_mode (1)
import your_module

Best regards
Oliver

--
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''
(E. A. Poe)
 
Reply With Quote
 
Jacek Generowicz
Guest
Posts: n/a
 
      07-09-2004
Oliver Fromme <(E-Mail Removed)> writes:

> > I guess the imported module could block further configuration
> > changes, and the warning can come when you try to change the
> > configuration _after_ the first import.


> #--- File config.py: ---
> foo_interface_mode = 0 # default
> def set_foo_interface_mode (mode):
> foo_interface_mode = mode
>
> #--- File your_module.py: ---
> import config
>
> def config_warn():
> print "foo_interface_mode may only be set once!"
>
> config.set_foo_interface_mode = config_warn


Yup, that's exactly the sort of thing I was suggesting.
 
Reply With Quote
 
David Fraser
Guest
Posts: n/a
 
      07-09-2004
Jacek Generowicz wrote:
> I have a module whose behaviour needs to be configurable. The module
> needs to decide, the first time it is imported, beteween alternative
> interfaces it presents.
>
> Currently, I set some environment variables which select the desired
> behaviour, and the module inspects those variables to determine the
> mode in which it should set itself up. I would prefer a pure Python
> solution, rather than one which depends on external state.
>
> Can you recommend any approaches, or warn against the pitfalls of some
> approaches?
>

What I would recommend is class-ifying the code inside the module. Then
you can create a fresh object each time you want a different
configuration, and you don't have to worry about the relationship
between import and configure.
This may make your module more flexible as well

David
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
imp.find_module don't found my module but standard import statement can import this module… why ? Stéphane Klein Python 0 07-05-2011 07:36 AM
Why 'import module' will not import module.py but the directorymodule? Peng Yu Python 0 10-31-2009 11:51 PM
Handling import conflicts when module has the same name as a library module that it needs to import? plb Python 0 02-08-2005 01:08 PM
Handling import conflicts when module has the same name as a library module that it needs to import? plb Python 0 02-08-2005 01:01 PM
opening parametrized window in asp.net without post back-Help required! Keshav Gadia ASP .Net 2 09-13-2004 10:05 AM



Advertisments