Skip to content

Instantly share code, notes, and snippets.

@charlee
Created January 16, 2018 19:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlee/af54632272e08e42257ee047064c2d3b to your computer and use it in GitHub Desktop.
Save charlee/af54632272e08e42257ee047064c2d3b to your computer and use it in GitHub Desktop.
traverse modules and objects in python
import sys
import pkgutil
import importlib
from .base import BaseCommand, CommandExecutor
def discover_commands():
executor = CommandExecutor()
current_module = sys.modules[__name__]
__path__ = pkgutil.extend_path(current_module.__path__, __name__)
for importer, modname, ispkg in pkgutil.walk_packages(path=__path__, prefix=__name__+'.'):
mod = importlib.import_module(modname)
for name, clazz in mod.__dict__.items():
if type(clazz) == type and issubclass(clazz, BaseCommand) and clazz != BaseCommand:
executor.add_command(clazz)
print(executor.command_map)
return executor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment