473,382 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How do I call anonymous classes from imported modules?

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.

--
Later, Joe
Aug 20 '07 #1
2 1056
On 8/19/07, James Stroud <js*****@mbi.ucla.eduwrote:
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)
Aug 20 '07 #2
JoeSox wrote:
On 8/19/07, James Stroud <js*****@mbi.ucla.eduwrote:
>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
Aug 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ed Severn | last post by:
I'm sorry of this question has been posed and answered many times before. But I have avoided using the "package" statement because of this. Most of my classes have no "package" statement, and...
8
by: Corey Lubin | last post by:
someGlobal=1 class Original: def foo(self): # Make use of someGlobal from original import * someGlobal=2
8
by: Rob Snyder | last post by:
Greetings - I have a situation where I need to be able to have a Python function that will take all the modules in a given directory and find all the classes defined in these modules by name....
0
by: dberlin | last post by:
hey, My situation is like this- whenever I create an application with a gui I put the gui source in one file that calls all the other classes (from imported modules). This creates a problem when...
0
by: François Pinard | last post by:
Hi, everybody. I wish someone could advise me. I'm running in circles, trying to find an elegant way to devise run-time pluggable classes. It all goes around method resolution order, I guess....
26
by: Sanjay | last post by:
Hi All, Not being able to figure out how are partial classes coded in Python. Example: Suppose I have a code generator which generates part of a business class, where as the custome part is to...
3
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
by: Gabriel Genellina | last post by:
En Sun, 24 Aug 2008 07:34:41 -0300, Mohamed Yousef <harrrrpo@gmail.comescribió: Yes. That way, when you see a name "foo" used in a module, you can look at the imports to see where it comes from....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.