Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Conditional based on whether or not a module is being used

Reply
Thread Tools

Conditional based on whether or not a module is being used

 
 
Pete Emerson
Guest
Posts: n/a
 
      03-05-2010
In a module, how do I create a conditional that will do something
based on whether or not another module has been loaded?

Suppose I have the following:

import foo
import foobar

print foo()
print foobar()

########### foo.py
def foo:
return 'foo'

########### foobar.py
def foobar:
if foo.has_been_loaded(): # This is not right!
return foo() + 'bar' # This might need to be foo.foo() ?
else:
return 'bar'

If someone is using foo module, I want to take advantage of its
features and use it in foobar, otherwise, I want to do something else.
In other words, I don't want to create a dependency of foobar on foo.

My failed search for solving this makes me wonder if I'm approaching
this all wrong.

Thanks in advance,
Pete
 
Reply With Quote
 
 
 
 
Pete Emerson
Guest
Posts: n/a
 
      03-05-2010
On Mar 5, 11:24*am, Pete Emerson <(E-Mail Removed)> wrote:
> In a module, how do I create a conditional that will do something
> based on whether or not another module has been loaded?
>
> Suppose I have the following:
>
> import foo
> import foobar
>
> print foo()
> print foobar()
>
> ########### foo.py
> def foo:
> * * return 'foo'
>
> ########### foobar.py
> def foobar:
> * * if foo.has_been_loaded(): # This is not right!
> * * * * return foo() + 'bar' * * *# This might need to be foo.foo() ?
> * * else:
> * * * * return 'bar'
>
> If someone is using foo module, I want to take advantage of its
> features and use it in foobar, otherwise, I want to do something else.
> In other words, I don't want to create a dependency of foobar on foo.
>
> My failed search for solving this makes me wonder if I'm approaching
> this all wrong.
>
> Thanks in advance,
> Pete


Aha, progress. Comments appreciated. Perhaps there's a different and
more conventional way of doing it than this?

def foobar():
import sys
if 'foomodule' in sys.modules.keys():
import foomodule
return foomodule.foo() + 'bar'
else:
return 'bar'
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      03-05-2010
Pete Emerson wrote:
> In a module, how do I create a conditional that will do something
> based on whether or not another module has been loaded?
>
> Suppose I have the following:
>
> import foo
> import foobar
>
> print foo()
> print foobar()
>
> ########### foo.py
> def foo:
> return 'foo'
>
> ########### foobar.py
> def foobar:
> if foo.has_been_loaded(): # This is not right!
> return foo() + 'bar' # This might need to be foo.foo() ?
> else:
> return 'bar'
>
> If someone is using foo module, I want to take advantage of its
> features and use it in foobar, otherwise, I want to do something else.
> In other words, I don't want to create a dependency of foobar on foo.
>
> My failed search for solving this makes me wonder if I'm approaching
> this all wrong.
>
> Thanks in advance,
> Pete


One way would be

if "foo" in sys.modules:
# foo was imported

However that won't get you all the way, since sys.modules["foo"] will be
set even if the importing statement was

from foo import this, that, the_other

So you might want to add

foo = sys.modules["foo"]

inside the function.

regards
Steve

--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      03-05-2010
Pete Emerson wrote:
> In a module, how do I create a conditional that will do something
> based on whether or not another module has been loaded?
>
> Suppose I have the following:
>
> import foo
> import foobar
>
> print foo()
> print foobar()
>

By the way, the above statements are never going to work, because
modules aren't callable. Maybe you want

print foo.foo()
print foobar.foobar()

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-05-2010
On Fri, 05 Mar 2010 11:24:44 -0800, Pete Emerson wrote:

> In a module, how do I create a conditional that will do something based
> on whether or not another module has been loaded?


try:
import foo
except ImportError:
foo = None

def function():
if foo:
return foo.func()
else:
do_something_else()



Or, alternatively:

try:
import foo
except ImportError:
import alternative_foo as foo # This better succeed!

def function():
return foo.func()


--
Steven
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      03-05-2010
Pete Emerson wrote:
> In a module, how do I create a conditional that will do something
> based on whether or not another module has been loaded?
>
> Suppose I have the following:
>
> import foo
> import foobar
>
> print foo()
> print foobar()
>
> ########### foo.py
> def foo:
> return 'foo'
>
> ########### foobar.py
> def foobar:
> if foo.has_been_loaded(): # This is not right!
> return foo() + 'bar' # This might need to be foo.foo() ?
> else:
> return 'bar'
>
> If someone is using foo module, I want to take advantage of its
> features and use it in foobar, otherwise, I want to do something else.
> In other words, I don't want to create a dependency of foobar on foo.
>
> My failed search for solving this makes me wonder if I'm approaching
> this all wrong.
>

Look for its name in sys.modules, for example:

'foo' in sys.modules
 
Reply With Quote
 
Martin P. Hellwig
Guest
Posts: n/a
 
      03-05-2010
On 03/05/10 19:24, Pete Emerson wrote:
> In a module, how do I create a conditional that will do something
> based on whether or not another module has been loaded?

<cut>>
> If someone is using foo module, I want to take advantage of its
> features and use it in foobar, otherwise, I want to do something else.
> In other words, I don't want to create a dependency of foobar on foo.
>
> My failed search for solving this makes me wonder if I'm approaching
> this all wrong.
>
> Thanks in advance,
> Pete


Hmm how about the module is available, just not imported yet, I would
assume that you still would like to use the module then.
Perhaps playing around with the imp module might get you what you mean
instead of what you say?

--
mph

 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      03-05-2010
On 3/5/10, Pete Emerson <(E-Mail Removed)> wrote:
> In a module, how do I create a conditional that will do something
> based on whether or not another module has been loaded?
>
> Suppose I have the following:
>
> import foo
> import foobar
>
> print foo()
> print foobar()
>
> ########### foo.py
> def foo:
> return 'foo'
>
> ########### foobar.py
> def foobar:
> if foo.has_been_loaded(): # This is not right!
> return foo() + 'bar' # This might need to be foo.foo() ?
> else:
> return 'bar'
>
> If someone is using foo module, I want to take advantage of its
> features and use it in foobar, otherwise, I want to do something else.
> In other words, I don't want to create a dependency of foobar on foo.
>
> My failed search for solving this makes me wonder if I'm approaching
> this all wrong.


Just try importing foo, and then catch the exception if it's not installed.

#foobar.py
try:
import foo
except ImportError:
FOO_PRESENT = False
else:
FOO_PRESENT = True

if FOO_PRESENT:
def foobar():
return foo.foo() + 'bar'
else:
def foobar():
return 'bar'


You could alternately do the `if FOO_PRESENT` check inside the
function body rather than defining separate versions of the function.

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
Pete Emerson
Guest
Posts: n/a
 
      03-05-2010
On Fri, Mar 5, 2010 at 12:17 PM, Chris Rebert <(E-Mail Removed)> wrote:
> On 3/5/10, Pete Emerson <(E-Mail Removed)> wrote:
>> In a module, how do I create a conditional that will do something
>> based on whether or not another module has been loaded?
>>
>> Suppose I have the following:
>>
>> import foo
>> import foobar
>>
>> print foo()
>> print foobar()
>>
>> ########### foo.py
>> def foo:
>> * *return 'foo'
>>
>> ########### foobar.py
>> def foobar:
>> * *if foo.has_been_loaded(): # This is not right!
>> * * * *return foo() + 'bar' * * *# This might need to be foo.foo() ?
>> * *else:
>> * * * *return 'bar'
>>
>> If someone is using foo module, I want to take advantage of its
>> features and use it in foobar, otherwise, I want to do something else.
>> In other words, I don't want to create a dependency of foobar on foo.
>>
>> My failed search for solving this makes me wonder if I'm approaching
>> this all wrong.

>
> Just try importing foo, and then catch the exception if it's not installed.
>
> #foobar.py
> try:
> * *import foo
> except ImportError:
> * *FOO_PRESENT = False
> else:
> * *FOO_PRESENT = True
>
> if FOO_PRESENT:
> * *def foobar():
> * * * *return foo.foo() + 'bar'
> else:
> * *def foobar():
> * * * *return 'bar'
>
>
> You could alternately do the `if FOO_PRESENT` check inside the
> function body rather than defining separate versions of the function.
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>


Except I want to use the module only if the main program is using it
too, not just if it's available for use. I think that I found a way in
my follow-up post to my own message, but not sure it's the best way or
conventional.

Pete
 
Reply With Quote
 
Pete Emerson
Guest
Posts: n/a
 
      03-05-2010
On Mar 5, 12:06*pm, "Martin P. Hellwig" <(E-Mail Removed)>
wrote:
> On 03/05/10 19:24, Pete Emerson wrote:
>
> > In a module, how do I create a conditional that will do something
> > based on whether or not another module has been loaded?

> <cut>>
> > If someone is using foo module, I want to take advantage of its
> > features and use it in foobar, otherwise, I want to do something else.
> > In other words, I don't want to create a dependency of foobar on foo.

>
> > My failed search for solving this makes me wonder if I'm approaching
> > this all wrong.

>
> > Thanks in advance,
> > Pete

>
> Hmm how about the module is available, just not imported yet, I would
> assume that you still would like to use the module then.
> Perhaps playing around with the imp module might get you what you mean
> instead of what you say?
>
> --
> mph


I can certainly see why one might want to use it if it's available but
not yet imported. In that case I could do a try / exception block. But
in this case, I actually don't want to use the module unless the main
program is doing it too. But you've got me thinking, I need to make
sure that's really the desired behavior.

Pete
 
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
way for a function to understand whether it's being run through aOnCreate callback or not slamdunk Python 3 07-10-2009 07:30 AM
constructor that depends whether the object being constructed is const or not Hicham Mouline C++ 12 07-08-2009 09:16 AM
How to find a whether a method is used by particular thread or not? sivasu.india@gmail.com Java 2 04-10-2007 12:57 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Preprocessor conditional compilation variable not being saved Chris P ASP .Net 0 10-28-2003 08:48 PM



Advertisments