From: Don C. <dco...@ch...> - 2007-08-23 18:16:47
|
I think this is "normal" inspect.getmembers is calling getattr on attributes that are not readable from javax.swing import JFrame from java.awt import IllegalComponentStateException frame = JFrame() for key in dir(frame): try: getattr(frame, key) print key except (TypeError, AttributeError, IllegalComponentStateException), e: print "%s [NOT READABLE %s]" % (key, e.__class__) On 8/23/07, Jake B <ota...@gm...> wrote: > You are correct, I am able to import inspect with Jython 2.2. It doesn't > seem to work on all classes though. For example, I can do this, and > everything is happy: > > Jython Completion Shell > JythJython Completion Shell > Jython 2.2 on java1.5.0_10 > >>> import inspect > >>> class Test: > ... def testMethod(): > ... print 'foo' > ... > >>> t = Test() > >>> t > <<unknown>.Test instance 1> > >>> inspect.getmembers(t,inspect.ismethod) > [('testMethod', <method Test.testMethod of Test instance 1>)] > > But if I try the same thing with a JFrame, it doesn't work: > > >>> from javax.swing import JFrame > >>> f = JFrame() > >>> f > javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane= > javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true] > >>> inspect.getmembers (f,inspect.ismethod) > Traceback (most recent call last): > File "<input>", line 1, in ? > File > "/mathworks/home/jbeard/jythonconsole-0.0.4/inspect.py", > line 160, in getmembers > value = getattr(object, key) > TypeError: write only attribute > > Is this normal? Could anyone explain why this is happening? > Please let me know. Thanks. > > Jake > > > On 8/23/07, Charlie Groves <cha...@gm...> wrote: > > inspect has been added to Jython 2.2, so you should be able to import > > it with that version. > > > > Charlie > > > > On 8/23/07, Don Coleman <dco...@ch... > wrote: > > > Your import failed because inspect.py is not included with Jython 2.1. > > > For jythonconsole I used inspect.py from CPython 2.2.2. > > > > > > On 8/22/07, Jake B < ota...@gm...> wrote: > > > > Just a quick note: > > > > I tried import inspect under jython 2.2rc3, and it works fine, but it > fails > > > > under jython 2.1. > > > > Is this normal? > > > > Please let me know. Thanks. > > > > > > > > Jake > > > > > > > > > > > > On 8/22/07, Jake B <ota...@gm...> wrote: > > > > > Thank you for the quick replies! Jythonconsole is very interesting. > Also, > > > > I found that the commercial version of Pydev supports some > autocompletion > > > > and autoimports libraries. Unfortunately, Pydev autocompletion doesn't > work > > > > perfectly, and jythonconsole does not yet adequately fulfill my needs > for an > > > > editing environment. > > > > > For now, I'd just be happy to have the inspect module working. The > > > > developer of jythonconsole says that he uses inspect.py in his > > > > implementation, but whenever i'm using jython and I try to import it, > I > > > > always get an error message saying that the module cannot be found. > When I'm > > > > in python, however, I'm able to import it just fine. > > > > > If anyone has any advice as to how I could troubleshoot this, I > would > > > > greatly appreciate it if you would let me know. Thanks. > > > > > > > > > > Jake > > > > > > > > > > > > > > > > > > > > On 8/21/07, Pekka Laukkanen < pe...@ik...> wrote: > > > > > > 2007/8/21, Jake B < ota...@gm...>: > > > > > > > I was wondering if there > > > > > > > were any other function that could retrieve an object's public > methods > > > > and > > > > > > > other constructs. > > > > > > > > > > > > You probably already know about it but simply using dir(object) > gives > > > > > > you list of all the object's attributes. If you are only > interested > > > > > > about methods, relatively simple code like below can help. > > > > > > > > > > > > >>> from java.lang import String > > > > > > >>> from types import MethodType > > > > > > >>> s = String('') > > > > > > >>> for a in [a for a in dir(s) if type(getattr(s, a)) is > MethodType ]: > > > > > > ... print a > > > > > > > > > > > > > > > > > > Cheers, > > > > > > .peke |