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

rh0dium sklass at pointcircle.com
Thu Aug 4 10:25:51 EDT 2005


Hi again,

No you're right there isn't a mod.mod I want this to be evaluated from
this

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

to this.

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

then

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

etc.

So how is the substitution not working??


Robert Kern wrote:
> rh0dium wrote:
> > 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.
>
> Doing what substitution? Neither of the modules that you showed define a
> mod() callable and nothing else seems to add one.
>
> --
> Robert Kern
> rkern at ucsd.edu
>
> "In the fields of hell where the grass grows high
>   Are the graves of dreams allowed to die."
>    -- Richard Harter




More information about the Python-list mailing list