Mass importing of a template based system.. Trouble with name substitutions

rh0dium sklass at pointcircle.com
Wed Aug 3 22:38:30 EDT 2005


Hi all,

Basically I have a bunch of pluggins in a directory (METDIR).  For each
one of these templated pluggins I want to do a specific routine. Let's
start with a basic template

file example1.py
----------------
class example1:
   def __init__(self):
      print "Initialize"
   def run(self):
      print "Hi from example 1"
----------------

file example2.py
----------------
class example2:
   def __init__(self):
      print "Initalize"
   def run(self):
      print "example 2"
----------------

Now I want to go through each pluggin ( example1.py and example2.py )
and execute run.  So here is my code but it doesn't work..


if os.path.isdir(METDIR):
   modules = []

   # Add the metrics dir toyour path..
   sys.path.insert( 0, os.getcwd() + "/" +  METDIR )

   for metric in glob.glob(METDIR+"/*.py"):
      # Now lets start working on the individual metrics
      module_name, ext = os.path.splitext(os.path.basename(metric))
      try:
         module = __import__(module_name)
         modules.append( module )
      except ImportError , e:
         print "Failed import of  %s - %s" % ( module_name, e)
         pass

   for mod in modules:
      a = mod.mod()
      a.run()

But it doesn't work with the following..

Traceback (most recent call last):
  File "./metriX.py", line 109, in main
    a = mod.mod()
AttributeError: 'module' object has no attribute 'mod'

So it looks like it's not doing the substitution.  Does anyone know how
I can do this?

Thanks much




More information about the Python-list mailing list