From: Pete Emerson on
On Mar 5, 11:57 am, MRAB <pyt...(a)mrabarnett.plus.com> wrote:
> 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

Excellent, this is what I finally discovered, although I was looking
for 'foo' in sys.modules.keys(), which apparently isn't necessary.
From: Chris Rebert on
On Fri, Mar 5, 2010 at 12:25 PM, Pete Emerson <pemerson(a)gmail.com> wrote:
> On Fri, Mar 5, 2010 at 12:17 PM, Chris Rebert <clp2(a)rebertia.com> wrote:
>> On 3/5/10, Pete Emerson <pemerson(a)gmail.com> wrote:
>>> In a module, how do I create a conditional that will do something
>>> based on whether or not another module has been loaded?
<snip>
>>> 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.
<snip>
> 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.

What is your use case for this behavior exactly? You've piqued my curiosity.

Cheers,
Chris
--
http://blog.rebertia.com
From: Pete Emerson on
On Mar 5, 1:14 pm, Chris Rebert <c...(a)rebertia.com> wrote:
> On Fri, Mar 5, 2010 at 12:25 PM, Pete Emerson <pemer...(a)gmail.com> wrote:
> > On Fri, Mar 5, 2010 at 12:17 PM, Chris Rebert <c...(a)rebertia.com> wrote:
> >> On 3/5/10, Pete Emerson <pemer...(a)gmail.com> wrote:
> >>> In a module, how do I create a conditional that will do something
> >>> based on whether or not another module has been loaded?
> <snip>
> >>> 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.
> <snip>
> > 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.
>
> What is your use case for this behavior exactly? You've piqued my curiosity.
>
> Cheers,
> Chris
> --http://blog.rebertia.com

I have written my first module called "logger" that logs to syslog via
the syslog module but also allows for logging to STDOUT in debug mode
at multiple levels (to increase verbosity depending on one's need), or
both. I've looked at the logging module and while it might suit my
needs, it's overkill for me right now (I'm still *very* much a python
newbie).

I want to write other modules, and my thinking is that it makes sense
for those modules to use the "logger" module to do the logging, if and
only if the parent using the other modules is also using the logger
module.

In other words, I don't want to force someone to use the "logger"
module just so they can use my other modules, even if the "logger"
module is installed ... but I also want to take advantage of it if I'm
using it.

Now that I've written that, I'm not sure that makes a whole lot of
sense. It seems like I could say, "hey, this person has the 'logger'
module available, let's use it!".

Thoughts?
From: Terry Reedy on
On 3/5/2010 4:29 PM, Pete Emerson wrote:
> On Mar 5, 1:14 pm, Chris Rebert<c...(a)rebertia.com> wrote:

> I want to write other modules, and my thinking is that it makes sense
> for those modules to use the "logger" module to do the logging, if and
> only if the parent using the other modules is also using the logger
> module.
>
> In other words, I don't want to force someone to use the "logger"
> module just so they can use my other modules, even if the "logger"
> module is installed ... but I also want to take advantage of it if I'm
> using it.
>
> Now that I've written that, I'm not sure that makes a whole lot of
> sense. It seems like I could say, "hey, this person has the 'logger'
> module available, let's use it!".

Except in unusual cases, where merely importing a modules uses
substantial resources, I would say that if it is available, use it.

From: Vinay Sajip on
On Mar 5, 9:29 pm, Pete Emerson <pemer...(a)gmail.com> wrote:
>
> I have written my first module called "logger" that logs to syslog via
> the syslog module but also allows forloggingto STDOUT in debug mode
> at multiple levels (to increase verbosity depending on one's need), or
> both. I've looked at theloggingmodule and while it might suit my
> needs, it's overkill for me right now (I'm still *very* much a python
> newbie).
>

Overkill in what sense? You just need to write a few lines of code to
be able to use the logging package which comes with Python:

import logging, logging.handlers, sys
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
logging.getLogger().addHandler(logging.handlers.SysLogHandler())
# default logs to syslog at (localhost, 514) with facility LOG_USER
# you can change the default to use e.g. Unix domain sockets and a
different facility

So you're experienced enough and have time enough to write your own
logger module, but too much of a newbie to use a module which is part
of Python's included batteries? If you're writing something like
logging to learn about it and what the issues are, that's fair enough.
But I can't see what you mean by overkill, exactly. The three lines
above (or thereabouts) will, I believe, let you log to syslog and to
stdout...which is what you say you want to do.

> I want to write other modules, and my thinking is that it makes sense
> for those modules to use the "logger" module to do thelogging, if and
> only if the parent using the other modules is also using the logger
> module.
>
> In other words, I don't want to force someone to use the "logger"
> module just so they can use my other modules, even if the "logger"
> module is installed ... but I also want to take advantage of it if I'm
> using it.
>
> Now that I've written that, I'm not sure that makes a whole lot of
> sense. It seems like I could say, "hey, this person has the 'logger'
> module available, let's use it!".
>
> Thoughts?

Well, the logging package is available in Python and ready for use and
pretty much battle tested, so why not use that? Are you planning to
use third-party libraries in your Python work, or write everything
yourself? If you are planning to use third party libraries, how would
their logging be hooked into your logger module? And if not, is it
good to have two logging systems in parallel?

Of course as the maintainer of Python's logging package, you'd expect
me to be biased in favour of it. You maybe shouldn't let that sway
you ;-)

Regards,


Vinay Sajip
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Slicing [N::-1]
Next: ANN: Wing IDE 3.2.5 Released