2369
Comment: searching for .py & .pyc files
|
← Revision 12 as of 2010-06-14 22:06:18 ⇥
3164
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
[[TableOfContents()]] == 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 }}} |
<<TableOfContents>> |
Line 28: | Line 14: |
import sets | |
Line 37: | Line 22: |
modules = sets.Set() | modules = set() |
Line 53: | Line 38: |
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 61: |
== Identifying Functions == | == Finding Functions Within a Module == |
Line 78: | Line 63: |
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 67: |
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 70: |
if is_it_a_function(obj): | if isinstance(obj, types.FunctionType): |
Line 95: | Line 75: |
== See Also == The DocXmlRpcServer page includes code demonstrating the use of these techniques. |
|
Line 96: | Line 80: |
I got this error when executing find_modules() in a package directory. That is the directory contained an {{{ __init.py__}}} file: {{{ File "C:\Python254\lib\site-packages\joedorocak\find_modules.py", line 27, in find_modules s.add(module) NameError: global name 's' is not defined }}} It looks to me like s needs to be initialized (some place near "modules = set()"). I'm not sure what the protocol is here, so I'm just going to leave this comment in the discussion. |
|
Line 97: | Line 88: |
(none yet!) | Here's what seems to work for me. I got rid of 's' altogether. {{{ 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 = 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: modules.add(module) return list(modules) }}} All the best, JoeDorocak |
Here's how to find all the modules in some directory, and import them.
Contents
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
3 def find_modules(path="."):
4 """Return names of modules in a directory.
5
6 Returns module names in a list. Filenames that end in ".py" or
7 ".pyc" are considered to be modules. The extension is not included
8 in the returned list.
9 """
10 modules = set()
11 for filename in os.listdir(path):
12 module = None
13 if filename.endswith(".py"):
14 module = filename[:-3]
15 elif filename.endswith(".pyc"):
16 module = filename[:-4]
17 if module is not None:
18 s.add(module)
19 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.
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.
See Also
The DocXmlRpcServer page includes code demonstrating the use of these techniques.
Discussion
I got this error when executing find_modules() in a package directory. That is the directory contained an __init.py__ file:
File "C:\Python254\lib\site-packages\joedorocak\find_modules.py", line 27, in find_modules s.add(module) NameError: global name 's' is not defined
It looks to me like s needs to be initialized (some place near "modules = set()"). I'm not sure what the protocol is here, so I'm just going to leave this comment in the discussion.
Here's what seems to work for me. I got rid of 's' altogether.
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 = 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: modules.add(module) return list(modules)
All the best,