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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I call anonymous classes from imported modules?
On 8/19/07, James Stroud <[EMAIL PROTECTED]> wrote: > 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 "", line 3, in ? anobj.do_foobar() TypeError: unbound method do_foobar() must be called with ticker instance as first argument (got nothing instead) -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I call anonymous classes from imported modules?
On 8/19/07, James Stroud <[EMAIL PROTECTED]> wrote: > 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: >>> for plugin in get_plugins_by_capability('do_foobar'): plg=plugin() print plg.do_foobar() -- Thanks, Joe -- http://mail.python.org/mailman/listinfo/python-list
Re: 'REPL' style IDE
On 8/20/07, beginner <[EMAIL PROTECTED]> wrote: > Hi Everyone, > > I am using the Wing IDE. It works great when developing applications, > but the workflow is like Visual Studio -- after you execute it or > debug it, the python script ends. > > What I want is an interactive interpreting environment. I want the IDE > to execute a boot script to initialize my environment and create some > basic data objects. And then I want to be able to type in command on > the command line using these objects. The IDLE that comes with Python > does this, but compared with Wing, it does not have a lot of the > convenient features. > > I am wondering if there is anything more powerful than IDLE that can > do this. I use Wing IDE. Place something like this on the bottom of your module you are debuging. Place a stop point on the line you want then start your debug! Hope that helps. def test(): c=MyClass c.do_foobar() if __name__ == '__main__': test() -- Later, Joe -- http://mail.python.org/mailman/listinfo/python-list
Re: 'REPL' style IDE
On 8/20/07, JoeSox <[EMAIL PROTECTED]> wrote: > On 8/20/07, beginner <[EMAIL PROTECTED]> wrote: > > Hi Everyone, > > > > I am using the Wing IDE. It works great when developing applications, > > but the workflow is like Visual Studio -- after you execute it or > > debug it, the python script ends. > > > > What I want is an interactive interpreting environment. I want the IDE > > to execute a boot script to initialize my environment and create some > > basic data objects. And then I want to be able to type in command on > > the command line using these objects. The IDLE that comes with Python > > does this, but compared with Wing, it does not have a lot of the > > convenient features. > > > > I am wondering if there is anything more powerful than IDLE that can > > do this. > > I use Wing IDE. > Place something like this on the bottom of your module you are debuging. > Place a stop point on the line you want then start your debug! Hope that > helps. > > def test(): >c=MyClass >c.do_foobar() > > if __name__ == '__main__': > test() Just seeing if you all were paying attention :P s/b... c=MyClass() -- Later, Joe -- http://mail.python.org/mailman/listinfo/python-list
Python project solicitation
Hello All, I was wondering if anyone would be interested in improving a module I quickly hacked together this past Sunday: pswrdgen.py It is a semantic password generator that uses WordNet 2.1, random capitalization, and character swapping. http://code.google.com/p/pswrdgen/ I must say this thing is pretty cool. I had a coworker try it out and he ran into problems getting it to run on his Linux OS. So I am really looking for some non-Windows developers to take a look at it. All of the info is at the project site above. Thanks. And on a side note, I see a bunch of Python newbies here. I really recommend "Python Phrasebook" by Brad Dayley. This thing is small enough to fit in your back pocket and lots of basic code phrases. -- Later, Joe -- http://mail.python.org/mailman/listinfo/python-list
Re: Python project solicitation
On 8/21/07, Greg Copeland <[EMAIL PROTECTED]> wrote: > > On Aug 20, 9:35 pm, JoeSox <[EMAIL PROTECTED]> wrote: > > I must say this thing is pretty cool. I had a coworker try it out and > > he ran into problems getting it to run on his Linux OS. So I am > > really looking for some non-Windows developers to take a look at it. > > All of the info is at the project site above. > > Thanks. > > > > I looked at it real quick. You need to use os.path.join for your file > paths. You also need to use sys.platform for windows specific > processing. For example: > > if sys.platform == 'Win32': > FS_ROOT = 'C:' > else: > FS_ROOT = '/' > > WORDNETPATH=os.path.join( FS_ROOT, 'WordNet', '2.1', 'dict' ) > > So on and so on. You wrote it very MSWin centric so it is not a > surprise it has trouble of on other platforms. All of your file > references need to be adjusted as above using os.path.join. > > Keep in mind I only looked at it real quick. Those appear to be the > cross platform deal killers. Short of something I missed (could > have), it should work find on most any other platform once you take > out the Windows-isms. Excellent. I will look at this. Being a hobbyist programmer, I am use to writing code just for a Windows environment. I've only been using Python for a year or so and I never could get into Java. -- Later, Joe -- http://mail.python.org/mailman/listinfo/python-list
Event for multithread help advice
I have two threads going class guiThread(threading.Thread) class mainThread(threading.Thread) Within the guiThread, I have an instance of class GUIFramework(Frame) in this Tkinter instance I have a ListBox. The second thread, mainThread, has an instance of a custom class the will need to send out text or other objects to the gui. What is the best or easiest way to send, let's say text, from mainThread to the guiThread ListBox? Should I use a custom Event class, pubsub, or Queue? This is the way I have things right now (not in its entirety): - start.py import threading import thread import time import GUIFramework import myFoo class guiThread(threading.Thread): def __init__(self, threadID, name): self.threadID = threadID self.name = name self.guiFrame = GUIFramework(master=None) threading.Thread.__init__(self) def run(self): print "Starting " + self.name self.guiFrame.mainloop() print "Exiting " + self.name class mainThread(threading.Thread): def __init__(self, threadID, name): self.threadID = threadID self.name = name self.myFoo = myFoo() threading.Thread.__init__(self) def run(self): print "Starting " + self.name self.myFoo.alive() print "Exiting " + self.name def start(): """ Start """ #Create new threads threadgui = guiThread(1, "threadgui") threadmain= carlThread(2, "threadmain") #Start new Threads threadgui.start() threadmain.run() while threadmain.isAlive(): if not threadgui.isAlive(): doExit = 1 pass print "Exiting Main Thread" if __name__ == '__main__': start() - I've tried a fews things but I really haven't had any luck. -- Thanks, Joe -- http://mail.python.org/mailman/listinfo/python-list