views:

125

answers:

3

I just started experimenting with a new technique I name (for the moment at least) "module duck typing".

Example:

Main Module

import somepackage.req  ## module required by all others
import abc
import Xyz

Module abc

__all__=[]

def getBus():
    """ Locates the `req` for this application """
    for mod_name in sys.modules:
        if mod_name.find("req") > 0:
            return sys.modules[mod_name].__dict__["Bus"]
    raise RuntimeError("cannot find `req` module")

Bus=getBus()

In module abc I do not need to explicitly import req: it could be anywhere in the package hierarchy. Of course this requires some discipline...

With this technique, it is easy to relocate packages within the hierarchy.

Are there pitfalls awaiting me? e.g. moving to Python 3K

Updated: after some more testing, I decided to go back to inserting package dependencies directly in sys.path.

+4  A: 

There might be all kinds of modules imported that contain "req" and you don't know if it's the module you are actually looking for:

>>> import urllib.request
>>> import tst
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tst.py", line 12, in <module>
    Bus=getBus()
  File "tst.py", line 9, in getBus
    return sys.modules[mod_name].__dict__["Bus"]
KeyError: 'Bus'

The whole point of packages is that there are namespaces for module hierarchies. Looking up module names "from any package" just causes your code to break randomly if the user happens to import some library that happens to contain a module with a conflicting name.

sth
+1: good point: I need to be more precise with my "pattern matching". Consider this "work in progress" ;-)
jldupont
+1  A: 

This technique is dangerous and error prone. It could work with your tests until the day that someone imports a new something.req and gets a confusing, far-off error. (This is in the best case scenario; the current implementation would jump on many other modules.) If you restructure packages, it's easy enough to at that time modify your code in an automated fashion without any use of magic. Python makes it possible to do all sorts of magical, dynamic things, but that doesn't mean we should.

Mike Graham
A: 

I think this is more like duck typing. I would also recommend using a more unique identifier than "Bus"

def getBus():
    """ Locates the Bus for this application """
    for mod in sys.modules.values():
        if hasattr(mod, 'Bus') and type(mod.Bus) is...: # check other stuff about mod.Bus
            return mod.Bus
    raise RuntimeError("cannot find Bus")
gnibbler