473,549 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_foob ar() works because I manually enter the classes' name)
>>a=MyApp()
sensorsList = []
for snames in a.pluginsDict["sensorsplugins "]:
sensorsList.app end(__import__( snames))
>>sensorsList[0].example.do_foo bar()
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 1062
On 8/19/07, James Stroud <js*****@mbi.uc la.eduwrote:
Quick and dirty (you could also use a try: except:):

f = __import__(modu le_name)
for anobj in f.__dict__.valu es():
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__(modn ame)
for anobj in m.__dict__.valu es():
if hasattr(anobj, 'do_foobar'):
anobj.do_foobar ()
Traceback (most recent call last):
File "<pyshell#7 6>", 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.uc la.eduwrote:
>Quick and dirty (you could also use a try: except:):

f = __import__(modu le_name)
for anobj in f.__dict__.valu es():
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__(modn ame)
for anobj in m.__dict__.valu es():
if hasattr(anobj, 'do_foobar'):
anobj.do_foobar ()
Traceback (most recent call last):
File "<pyshell#7 6>", 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
4813
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 their class files are stored in my current directory. Using "javac abc.java" works just fine. But some of my classes *do* have "package" statements....
8
6098
by: Corey Lubin | last post by:
someGlobal=1 class Original: def foo(self): # Make use of someGlobal from original import * someGlobal=2
8
1635
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. Ultimately, I would need to loop through all of these, instantiating each of the classes in turn and calling a pre-known method of each. Finding...
0
1115
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 needing to print something out on a gui control or something similar. It is then needed to send the called class the control object and the class...
0
1164
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. (We already use various solutions, but the maintenance burden is high.) We have a common module containing many basic classes, but for the sake of...
26
4115
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 be written by me. In ruby (or C#), I divide the code into two source files. Like this: GeneratedPerson.rb Class Person
3
1813
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 imports ) Take Example :- in A.py :- import B print dir() # no problems we can see B which contain re module and C module
0
1017
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 imports ) Take Example :- in A.py :- import B print dir() # no problems we can see B which contain re module and C module
0
1351
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. (BTW, this is why using "from module import *" is not a good idea) Having a single global namespace is a lot worse. Yes. How often do you...
0
7532
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7462
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7975
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7492
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3510
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1957
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1069
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.