Differences between revisions 4 and 5
Revision 4 as of 2005-04-16 08:13:24
Size: 2369
Editor: aaron
Comment: searching for .py & .pyc files
Revision 5 as of 2005-04-16 08:17:06
Size: 2022
Editor: aaron
Comment:
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:

== Finding Functions Within a Module ==

So, putting them together,...

{{{
#!python
def functions_in_module(module)
    functions = []
    for obj in module.__dict__.values():
        if isinstance(obj, types.FunctionType):
            functions.append(obj)
    return functions
}}}
Line 53: Line 39:
With the [http://www.python.org/doc/current/lib/module-imp.html the imp module!] It dynamically loads named modules. With the ImpModule! It dynamically loads named modules.
Line 76: Line 62:
== Identifying Functions == == Finding Functions Within a Module ==
Line 78: Line 64:
If you want to identify, say, functions within the module, you can write code similar to so: We just look for dictionary values that are of type types.FunctionType.
Line 82: Line 68:
import types

def is_it_a_function(obj):
    return isinstance(obj, types.FunctionType)

def functions_in_module(module):
def functions_in_module(module)
Line 90: Line 71:
        if is_it_a_function(obj):         if isinstance(obj, types.FunctionType):
Line 95: Line 76:
== See Also ==

The DocXmlRpcServer page includes code demonstrating the use of these techniques.

Here's how to find all the modules in some directory, and import them.

TableOfContents()

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"..?

But perhaps there isn't.

   1 import os
   2 import sets
   3 
   4 def find_modules(path="."):
   5     """Return names of modules in a directory.
   6 
   7     Returns module names in a list. Filenames that end in ".py" or
   8     ".pyc" are considered to be modules. The extension is not included
   9     in the returned list.
  10     """
  11     modules = sets.Set()
  12     for filename in os.listdir(path):
  13         module = None
  14         if filename.endswith(".py"):
  15             module = filename[:-3]
  16         elif filename.endswith(".pyc"):
  17             module = filename[:-4]
  18         if module is not None:
  19             s.add(module)
  20     return list(modules)

Importing the Modules

How do you import a module, once you have it's name?

With the ImpModule! It dynamically loads named modules.

   1 import imp
   2 
   3 def load_module(name, path=["."]):
   4     """Return a named module found in a given path."""
   5     (file, pathname, description) = imp.find_module(name, path)
   6     return imp.load_module(name, file, pathname, description)
   7 
   8 modules = [load_module(name) for name in find_modules()]

Finding the Things Inside a Module

Once you have your module, you can look inside it, with .__dict__.

   1 module.__dict__

Finding Functions Within a Module

We just look for dictionary values that are of type types.FunctionType.

   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

See Also

The DocXmlRpcServer page includes code demonstrating the use of these techniques.

Discussion

  • (none yet!)

ModulesAsPlugins (last edited 2010-06-14 22:06:18 by c-98-207-20-119)

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