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 6 1394
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 rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
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 rk***@ucsd.edu
"In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter
rh0dium wrote: for*mod*in*modules: a*=*mod.mod() a.run()
Puzzle: If mod.mod did what you expect, what would a.run have to do to
maintain consistency?
There would be no way to determine the name of the module bound to the mod
variable, but fortunately the Python developers foresaw your problem and
stashed it (the name) in the __name__ attribute.
Use getattr(obj, attrname) to look up an attribute whose name is not known
at compile time:
for mod in modules:
a = getattr(mod, mod.__name__)()
a.run()
Peter
Peter Otten wrote: rh0dium wrote:
for mod in modules: a = mod.mod() a.run() Puzzle: If mod.mod did what you expect, what would a.run have to do to maintain consistency?
I thought that once a = example.example the class is then loaded.
Since my framework defines a python file with a class named the same
within it. So
example1.py contains a class example1 and a module run
example2.py contains a class example2 and a module run
example3.py contains a class example3 and a module run
Additional methods that must be present within the class include a run
method. Since this was by definition I knew I needed to do a
substitution - namely mod.mod ( which was intended to be translated to
example1.example1 ).
What you provided was very slick indeed. I no longer am dependant upon
the naming to get the job done.
Very nice - How does this work if some fool calls one class the same as
the other. I'm assuming the last class will be loaded - not the first.
There would be no way to determine the name of the module bound to the mod variable, but fortunately the Python developers foresaw your problem and stashed it (the name) in the __name__ attribute. Use getattr(obj, attrname) to look up an attribute whose name is not known at compile time:
OUTSTANDING!! Thanks so much! for mod in modules: a = getattr(mod, mod.__name__)() a.run()
Peter
On 4 Aug 2005 07:25:51 -0700, "rh0dium" <sk****@pointcircle.com>
declaimed the following in comp.lang.python: So how is the substitution not working??
What substitution?
"mod", as used in the loop, is just a name that is attached to a
module object... It is /not/ "example1", "example2", etc.
mod.mod()
The first mod is the name used inside the loop, and is associated with
the code of the current module. The .mod is an attribute that is
expected to be found inside the module object, and bears no relationship
to the mod used in the loop statement.
I'm having trouble coming up with a good simile... but...
Imagine your modules are envelopes in an inbox (on a desk).
for mod in modules: #modules is the inbox
is taking a post-it sticky note with "mod" written on it, and putting it
on the outside of an envelope.
a = mod.mod()
says, "take the envelope with 'mod' stuck to it, open it up, and look
for a sheet of paper with a SECOND sticky note 'mod' stuck to it, and
read the sheet of paper". When done, the "for" loop moves the outside
sticky note to the next envelope.
Your error message is telling you that none of the envelopes has
a sticky note "mod" INSIDE.
Try a dictionary instead of a list and see if the following
changes do anything for you.
modules = {} 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 )
modules[module_name] = module
except ImportError , e: print "Failed import of %s - %s" % ( module_name, e) pass
for mod in modules:
for (name, mod) in modules.items():
a = mod.mod()
a = getattr(mod, name)
a.run()
-- ================================================== ============ < wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < ================================================== ============ < Home Page: <http://www.dm.net/~wulfraed/> < Overflow Page: <http://wlfraed.home.netcom.com/> <
On 4 Aug 2005 08:50:55 -0700, "rh0dium" <sk****@pointcircle.com>
declaimed the following in comp.lang.python: Additional methods that must be present within the class include a run method. Since this was by definition I knew I needed to do a substitution - namely mod.mod ( which was intended to be translated to example1.example1 ).
But Python does NOT do "substitution"... Variables (like "mod")
are just names attached to things, and have no bearing on any other
names that might be attached to the thing. Very nice - How does this work if some fool calls one class the same as the other. I'm assuming the last class will be loaded - not the first.
Uh... try it in the interactive prompt and see what happens?
-=-=-=- file x.py
def something():
print "I'm the something in X"
-=-=-=- file y.py
def something():
print "I'm the something in Y"
-=-=-=-= file main.py
import x
import y
y.something()
x.something()
z = y
z.something()
print id(x), id(y), id(z)
z.something = x.something
z.something()
y.something()
-=-=-=-=-=
I'm the something in Y
I'm the something in X
I'm the something in Y
16028080 16028528 16028528
I'm the something in X
I'm the something in X
-- ================================================== ============ < wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < ================================================== ============ < Home Page: <http://www.dm.net/~wulfraed/> < Overflow Page: <http://wlfraed.home.netcom.com/> < This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: qwweeeit |
last post by:
The pythonic way of programming requires, as far as I know, to spread a
big application in plenty of more manageable scripts, using import or
from ... import to connect the various modules.
In...
|
by: Chris F Clark |
last post by:
In our C++ project we have some internal bug reporting macros that we
use to get useful information when the program does something
unexpected. Essentially at the point of the error, we invoke an...
|
by: dixie |
last post by:
If I sent a user an empty database container - dB with no tables and I
needed them to import their tables into it and one of their tables was a
hidden table with the prefix Usys, is there any way...
|
by: Stuart McGraw |
last post by:
I am looking for a VBA "format" or "template" function, that is,
a function that takes a format string and a varying number of
arguments, and substitutes the argument values into the format
string...
|
by: Natan |
last post by:
When you create and aspx page, this is generated by default:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using...
|
by: I am Sam |
last post by:
Ok I don't know what is the problem with my code But I am trying to build a
newsletter that gathers parameters from 3 textbox controls and a Listbox
control. The form then queries the event table...
|
by: Mike Collins |
last post by:
I am trying to export data from multiple tables in SQL Server to an XML file
so I can then import it to another database. It seems to be working fine for
exporting, but I am having trouble...
|
by: JMO |
last post by:
I can import a csv file with no problem. I can also add columns to the
datagrid upon import. I want to be able to start importing at the 3rd
row. This will pick up the headers necessary for the...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |