How do I call anonymous classes from imported modules?

J

JoeSox

I am importing 3rd party modules via a plugin script. I wish to
iterate thru the modules and call a common method like .do_foobar() or
something with all the imports.
But I can't figure out how to do something like below without knowing
the class name before hand. Is there a builtin that helps call
classes anonymously?


(Calling example.do_foobar() works because I manually enter the classes' name)
a=MyApp()
sensorsList = []
for snames in a.pluginsDict["sensorsplugins"]: sensorsList.append(__import__(snames))
sensorsList[0].example.do_foobar()

but I need something like
because I will not know the plugin class names.
 
J

James Stroud

JoeSox said:
I am importing 3rd party modules via a plugin script. I wish to
iterate thru the modules and call a common method like .do_foobar() or
something with all the imports.
But I can't figure out how to do something like below without knowing
the class name before hand. Is there a builtin that helps call
classes anonymously?


(Calling example.do_foobar() works because I manually enter the classes' name)
a=MyApp()
sensorsList = []
for snames in a.pluginsDict["sensorsplugins"]:
sensorsList.append(__import__(snames))
sensorsList[0].example.do_foobar()


but I need something like
sensorsList[0][0].do_foobar()

because I will not know the plugin class names.

Quick and dirty (you could also use a try: except:):

f = __import__(module_name)
for anobj in f.__dict__.values():
if hasattr(anobj, 'do_foobar'):
anobj.do_foobar()

Note that this does not test whether anobj is a class as this would
entail a type-check, which hints to poor design.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
J

JoeSox

Quick and dirty (you could also use a try: except:):

f = __import__(module_name)
for anobj in f.__dict__.values():
if hasattr(anobj, 'do_foobar'):
anobj.do_foobar()

Note that this does not test whether anobj is a class as this would
entail a type-check, which hints to poor design.

Thanks for the help. The __dict__.values() is something I have not
previously looked into but it does not work as desired because python
wants an instance of the class. How can I cast an object if I don't
know what type of object it is (example.ticker, in this debug)?
modname = sensorsList[0].__name__
m = __import__(modname)
for anobj in m.__dict__.values():
if hasattr(anobj, 'do_foobar'):
anobj.do_foobar()


Traceback (most recent call last):
File "<pyshell#76>", line 3, in ?
anobj.do_foobar()
TypeError: unbound method do_foobar() must be called with ticker
instance as first argument (got nothing instead)
 
J

James Stroud

JoeSox said:
Quick and dirty (you could also use a try: except:):

f = __import__(module_name)
for anobj in f.__dict__.values():
if hasattr(anobj, 'do_foobar'):
anobj.do_foobar()

Note that this does not test whether anobj is a class as this would
entail a type-check, which hints to poor design.

Thanks for the help. The __dict__.values() is something I have not
previously looked into but it does not work as desired because python
wants an instance of the class. How can I cast an object if I don't
know what type of object it is (example.ticker, in this debug)?
modname = sensorsList[0].__name__
m = __import__(modname)
for anobj in m.__dict__.values():
if hasattr(anobj, 'do_foobar'):
anobj.do_foobar()


Traceback (most recent call last):
File "<pyshell#76>", line 3, in ?
anobj.do_foobar()
TypeError: unbound method do_foobar() must be called with ticker
instance as first argument (got nothing instead)

This was the poor design I was hinting to. do_foobar is type-checking
for ticker. "Casting" as you might think of it does not exist in python.
Creating new objects based on the values of existing objects (lets call
it "conversion" for lack of a better name) does (e.g. int(2.0)). But
this is not casting. Unless you have the power to re-author all of the
do_foobar()s to not type-check, you should create an instance of ticker
and pass that:

t = ticker(param1, param2, parametc)
[...]
anobj.do_foobar(t)

The "got nothing instead" means that you should have passed an
argument--and unfortunately whoever authored do_foobar() type-checked
for a ticker, which is not a desirable way to design an API.

James
 
J

JoeSox

This was the poor design I was hinting to. do_foobar is type-checking
for ticker. "Casting" as you might think of it does not exist in python.
Creating new objects based on the values of existing objects (lets call
it "conversion" for lack of a better name) does (e.g. int(2.0)). But
this is not casting. Unless you have the power to re-author all of the
do_foobar()s to not type-check, you should create an instance of ticker
and pass that:

t = ticker(param1, param2, parametc)
[...]
anobj.do_foobar(t)

The "got nothing instead" means that you should have passed an
argument--and unfortunately whoever authored do_foobar() type-checked
for a ticker, which is not a desirable way to design an API.

I designed the system using this 'tutorial'
http://lucumr.pocoo.org/blogarchive/python-plugin-system

With your help and revisiting the article I figured out that this call works: plg=plugin()
print plg.do_foobar()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top