Differences between revisions 3 and 4
Revision 3 as of 2005-04-15 04:37:07
Size: 2821
Editor: aaron
Comment: find_module, load_module
Revision 4 as of 2005-04-16 08:13:24
Size: 2369
Editor: aaron
Comment: searching for .py & .pyc files
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
I frequently want to find all the modules in some directory, with some property, and do something with them. Here's how to find all the modules in some directory, and import them.
Line 3: Line 3:
If you know how, please inform me. Here are some things I'm researching.

== Finding the Things Inside a Module ==

{{{
#!python
module.__dict__
}}}

== Identifying Functions ==

{{{
#!python
import types

def is_it_a_function(obj):
    return isinstance(obj, types.FunctionType)
}}}
[[TableOfContents()]]
Line 40: Line 23:
== 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__?)

There seems to be [http://www.python.org/doc/current/lib/module-imp.html a module imp] that can be used to dynamically load a named module.

== Playing with imp Module ==

Make a file "eraseme.py":
But perhaps there isn't.
Line 54: Line 27:
print "Successfully imported!" import os
import sets

def find_modules(path="."):
    """Return names of modules in a directory.

    Returns module names in a list. Filenames that end in ".py" or
    ".pyc" are considered to be modules. The extension is not included
    in the returned list.
    """
    modules = sets.Set()
    for filename in os.listdir(path):
        module = None
        if filename.endswith(".py"):
            module = filename[:-3]
        elif filename.endswith(".pyc"):
            module = filename[:-4]
        if module is not None:
            s.add(module)
    return list(modules)
Line 57: Line 49:
Then, at the python shell, == Importing the Modules ==

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

With the [http://www.python.org/doc/current/lib/module-imp.html the imp module!] It dynamically loads named modules.
Line 61: Line 57:
>>> import imp
>>> returned = imp.find_module("eraseme", ["."])
>>> returned
(<open file 'eraseme.py', mode 'U' at 0xb7f62220>, 'eraseme.py', ('.py', 'U', 1))
>>>
import imp

def load_module(name, path=["."]):
    """Return a named module found in a given path."""
    (file, pathname, description) = imp.find_module(name, path)
    return imp.load_module(name, file, pathname, description)

modules = [load_module(name) for name in find_modules()]
Line 68: Line 67:
The {{{find_module}}} line searches for "eraseme" in the current working directory ("."). == Finding the Things Inside a Module ==
Line 70: Line 69:
The first two returned items are self-explanatory, but what about that tuple- {{{('.py', 'U', 1)}}} ..?

The first (".py") is obviously the extension.

'U' means, "I opened the file in UniversalNewline mode." Basically, universal newline mode is like read ("r"), except that it interprets all newline forms the same way.

Finally, the 1 is a code that matches against imp.PY_SOURCE (1), imp.PY_COMPILED (2), or imp.C_EXTENSION (3). Basically, it's telling us that the .py file is a source file.

Once you have this stuff, it's easy to load:
Once you have your module, you can look inside it, with {{{.__dict__}}}.
Line 82: Line 73:
>>> eraseme = imp.load_module("eraseme", returned[0], returned[1], returned[2])
successfully imported!
>>> eraseme
<module 'eraseme' from './eraseme.py'
module.__dict__
Line 88: Line 76:
Note that, you don't have to play by the rules. If you wanted to, you could have said: == Identifying Functions ==

If you want to identify, say, functions within the module, you can write code similar to so:
Line 92: Line 82:
>>> module = imp.load_module("eggs", returned[0], "spam.py", returned[2])
successfully imported!
>>> module
<module 'eggs' from 'spam.py'>
>>> dir(module)
['__builtins__', '__doc__', '__file__', '__name__']
>>> module.__name__
'eggs'
>>> module.__file__
'spam.py'
>>>
import types

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

def functions_in_module(module):
    functions = []
    for obj in module.__dict__.values():
        if is_it_a_function(obj):
            functions.append(obj)
    return functions
Line 105: Line 95:
...even though the file's ''real'' name is "eggs.py". = Discussion =

  (none yet!)

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

TableOfContents()

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

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 [http://www.python.org/doc/current/lib/module-imp.html the imp module!] 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__

Identifying Functions

If you want to identify, say, functions within the module, you can write code similar to so:

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

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.