[PEAK] plugins, dynamic module loading

Bob Ippolito bob at redivi.com
Mon Nov 24 11:52:24 EST 2003


On Nov 24, 2003, at 11:17 AM, darryl wrote:

> hey all,
>
> I'm writing a whole series of stupid jabber/irc bots. Right now i'm
> working on dynamically loading modules/plugins. Here's what i'm doing
> now:
>
>    def loadModules(self, path):
>        for m in glob.glob("%s/*.py" % path):
>            m = re.sub(path, "", m)
>            m = re.sub(r'[/\\]', "", m)
>            m = re.sub("\.py$", "", m)
>            exec "import %s\nself.modules.append(%s.%s(self))\n" 
> %(m,m,m)
>            print "registered %s" %m
>
> It's very ugly :)

Looks like Perl to me, what the heck are you thinking?  :)

def loadModules(self, path):
     self.modules.extend([
         __import__(os.path.splitext(m)[0])
         for m in os.path.listdir(path)
         if os.path.splitext(m)[1] == '.py' or 
os.path.exists(os.path.join(path, m, '__init__.py'))
     ])

This also has the advantage that it lets you use a package.  As with 
yours, path must be in sys.path.

-bob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2357 bytes
Desc: not available
Url : http://www.eby-sarna.com/pipermail/peak/attachments/20031124/47097ca5/smime.bin


More information about the PEAK mailing list