Revision 1 as of 2005-04-14 22:54:05

Clear message

I frequently want to find all the modules in some directory, with some property, and do something with them.

If you know how, please inform me. Here are some things I'm researching.

Finding the Things Inside a Module

   1 module.__dict__

Identifying Functions

   1 import types
   2 
   3 def is_it_a_function(obj):
   4     return isinstance(obj, types.FunctionType)

Finding Functions Within a Module

So, putting them together,...

   1 def functions_in_module(module)
   2     functions = []
   3     for obj in module.__dict__.values():
   4         if isinstance(obj, types.FunctionType):
   5             functions.append(obj)
   6     return functions

Finding Modules in a Directory

Is there a better way than just listing the contents of the directory, and taking those tiles that end with ".pyc" or ".py"..?

Importing the Modules

How do you do it dynamically, just given a filename?

Once you've imported it, how do you get a handle on it? (That is, how do you get it's dict?)

Unable to edit the page? See the FrontPage for instructions.