Bound Methods

Hi,

I am playing with «introspection» and I would like to know
if it is possible to know which method is bound to a particular object.

Or putting the problem in another form: It’s possible
to know which object is bound to a particular method?

Suppose a window with 2 buttons (bt1 + bt2)

And:

bt1.Bind(wx.EVT_BUTTON, self.onButton1)

bt2.Bind(wx.EVT_BUTTON, self.onButton2)

I can easily identify the two objects, the two methods of their
parent and the source line where they are defined.

But:

How can I know that onButton1 is bound to bt1 and onButton2
is bound to bt2?

Or:

How can I know that bt1 is bound to onButton1 and bt2 is
bound to onButton2?

Any help would be appreciated.

Antonio Barbosa

I think you already know that by saying:

bt1.Bind(wx.EVT_BUTTON, self.onButton1)
bt2.Bind(wx.EVT_BUTTON, self.onButton2)

What are you trying to do ? What is your purpose ? Can you be more specific ?

···

2007/2/26, António Barbosa (GN/dpi) <ab@jn.pt>:

Hi,

I am playing with «introspection» and I would like to know if it is possible
to know which method is bound to a particular object.

Or putting the problem in another form: It's possible to know which object
is bound to a particular method?

Suppose a window with 2 buttons (bt1 + bt2)

And:

bt1.Bind(wx.EVT_BUTTON, self.onButton1)

bt2.Bind(wx.EVT_BUTTON, self.onButton2)

I can easily identify the two objects, the two methods of their parent and
the source line where they are defined.

But:

How can I know that onButton1 is bound to bt1 and onButton2 is bound to bt2?

Or:

How can I know that bt1 is bound to onButton1 and bt2 is bound to
onButton2?

Any help would be appreciated.

Antonio Barbosa

Hi Murat,

Excuse me but I was too synthetic :wink:
I will try to explain it better:

Sometimes I have to modify or debug a program made by me (or by someone else) and I would like, for instance, to change the behavior of pressing a particular button.
In a small program it is very easy to do it: I only have to read a hand-full of code lines.
But in large programs with several script files, with several classes an several dialogs, that task is not so easy!
Particularly if I am not the author or if my memory not helps me as it should :wink:

Suppose that at near the end of main script, I put something like this:

···

#--------------------------------------------------
class MyApp(wx.App):
  def OnInit(self):
           (...)
    self.Bind(wx.EVT_RIGHT_UP , self.OnRightClick) #only for debug!

  def OnRightClick(self, event):
    import inspect
    obj = wx.FindWindowAtPointer()
    name=obj.GetName()+' '+obj.GetLabel()
    A=[name]
    parent=obj.GetParent()
    bases= parent.__class__.__bases__[0]
    methodList = [ method for method in dir (parent) if method not in dir (bases) and callable(getattr(parent, method)) ]
    for method in methodList:
      A.append('Method: '+method)
    method=myPopUp(self.dialog_1,A) #myPopup gives a popup menu, and returns the choice
    myattr=method.split()
    if len(myattr)>1:
      s=getattr(parent,myattr[1])
      script=inspect.getsourcefile(s)
      line=inspect.getsourcelines(s)[1]
      print 'TRACE ---->[%s] %s Script: "%s" line %d' %(name,method,script, line)
    else:
      print 'TRACE ----> [%s]' %(name)
#--------------------------------------------------

With this, I get the name of object that I am inspecting, and a list of methods of parent's class.

What I would like is to show only the relevant methods: - the ones that are BOUND to it!

It is possible?
I know that I can parse the class code and search for lines that have «.bind», but it is not a good solution, don't you agree?

I hope to have been more clear.
Thanks for your attention.

Antonio Barbosa

-----Mensagem original-----
De: Murat Erten [mailto:murerten@gmail.com]
Enviada: seg 26-02-2007 19:01
Para: wxPython-users@lists.wxwidgets.org
Assunto: Re: [wxPython-users] Bound Methods

2007/2/26, António Barbosa (GN/dpi) <ab@jn.pt>:

Hi,

I am playing with «introspection» and I would like to know if it is possible
to know which method is bound to a particular object.

Or putting the problem in another form: It's possible to know which object
is bound to a particular method?

Suppose a window with 2 buttons (bt1 + bt2)

And:

bt1.Bind(wx.EVT_BUTTON, self.onButton1)

bt2.Bind(wx.EVT_BUTTON, self.onButton2)

I can easily identify the two objects, the two methods of their parent and
the source line where they are defined.

But:

How can I know that onButton1 is bound to bt1 and onButton2 is bound to bt2?

Or:

How can I know that bt1 is bound to onButton1 and bt2 is bound to
onButton2?

Any help would be appreciated.

Antonio Barbosa

I think you already know that by saying:

bt1.Bind(wx.EVT_BUTTON, self.onButton1)
bt2.Bind(wx.EVT_BUTTON, self.onButton2)

What are you trying to do ? What is your purpose ? Can you be more specific ?

I looked at the wx.EvtHandler class' functions but I couldn't find
something useful to you :frowning:

···

2007/2/26, António Barbosa (GN/dpi) <ab@jn.pt>:

Hi Murat,

Excuse me but I was too synthetic :wink:
I will try to explain it better:

Sometimes I have to modify or debug a program made by me (or by someone else) and I would like, for instance, to change the behavior of pressing a particular button.
In a small program it is very easy to do it: I only have to read a hand-full of code lines.
But in large programs with several script files, with several classes an several dialogs, that task is not so easy!
Particularly if I am not the author or if my memory not helps me as it should :wink:

Suppose that at near the end of main script, I put something like this:

#--------------------------------------------------
class MyApp(wx.App):
        def OnInit(self):
           (...)
                self.Bind(wx.EVT_RIGHT_UP , self.OnRightClick) #only for debug!

        def OnRightClick(self, event):
                import inspect
                obj = wx.FindWindowAtPointer()
                name=obj.GetName()+' '+obj.GetLabel()
                A=[name]
                parent=obj.GetParent()
                bases= parent.__class__.__bases__[0]
                methodList = [ method for method in dir (parent) if method not in dir (bases) and callable(getattr(parent, method)) ]
                for method in methodList:
                        A.append('Method: '+method)
                method=myPopUp(self.dialog_1,A) #myPopup gives a popup menu, and returns the choice
                myattr=method.split()
                if len(myattr)>1:
                        s=getattr(parent,myattr[1])
                        script=inspect.getsourcefile(s)
                        line=inspect.getsourcelines(s)[1]
                        print 'TRACE ---->[%s] %s Script: "%s" line %d' %(name,method,script, line)
                else:
                        print 'TRACE ----> [%s]' %(name)
#--------------------------------------------------

With this, I get the name of object that I am inspecting, and a list of methods of parent's class.

What I would like is to show only the relevant methods: - the ones that are BOUND to it!

It is possible?
I know that I can parse the class code and search for lines that have «.bind», but it is not a good solution, don't you agree?

I hope to have been more clear.
Thanks for your attention.

Antonio Barbosa

-----Mensagem original-----
De: Murat Erten [mailto:murerten@gmail.com]
Enviada: seg 26-02-2007 19:01
Para: wxPython-users@lists.wxwidgets.org
Assunto: Re: [wxPython-users] Bound Methods

2007/2/26, António Barbosa (GN/dpi) <ab@jn.pt>:
>
> Hi,
>
> I am playing with «introspection» and I would like to know if it is possible
> to know which method is bound to a particular object.
>
> Or putting the problem in another form: It's possible to know which object
> is bound to a particular method?
>
> Suppose a window with 2 buttons (bt1 + bt2)
>
> And:
>
> bt1.Bind(wx.EVT_BUTTON, self.onButton1)
>
> bt2.Bind(wx.EVT_BUTTON, self.onButton2)
>
> I can easily identify the two objects, the two methods of their parent and
> the source line where they are defined.
>
> But:
>
> How can I know that onButton1 is bound to bt1 and onButton2 is bound to bt2?
>
> Or:
>
> How can I know that bt1 is bound to onButton1 and bt2 is bound to
> onButton2?
>
> Any help would be appreciated.
>
> Antonio Barbosa
>

I think you already know that by saying:

bt1.Bind(wx.EVT_BUTTON, self.onButton1)
bt2.Bind(wx.EVT_BUTTON, self.onButton2)

What are you trying to do ? What is your purpose ? Can you be more specific ?

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

But maybe while you bind an event to a widget you can do this:

button = wx.Button(frame)
button.bound_methods = list()
...
button.Bind(wx.EVT_BUTTON, func1)
button.bound_methods.append(func1)

If you can get the button object you can get the methods you bind to
it like that.

···

2007/2/27, Murat Erten <murerten@gmail.com>:

2007/2/26, António Barbosa (GN/dpi) <ab@jn.pt>:
>
> Hi Murat,
>
> Excuse me but I was too synthetic :wink:
> I will try to explain it better:
>
> Sometimes I have to modify or debug a program made by me (or by someone else) and I would like, for instance, to change the behavior of pressing a particular button.
> In a small program it is very easy to do it: I only have to read a hand-full of code lines.
> But in large programs with several script files, with several classes an several dialogs, that task is not so easy!
> Particularly if I am not the author or if my memory not helps me as it should :wink:
>
> Suppose that at near the end of main script, I put something like this:
>
> #--------------------------------------------------
> class MyApp(wx.App):
> def OnInit(self):
> (...)
> self.Bind(wx.EVT_RIGHT_UP , self.OnRightClick) #only for debug!
>
> def OnRightClick(self, event):
> import inspect
> obj = wx.FindWindowAtPointer()
> name=obj.GetName()+' '+obj.GetLabel()
> A=[name]
> parent=obj.GetParent()
> bases= parent.__class__.__bases__[0]
> methodList = [ method for method in dir (parent) if method not in dir (bases) and callable(getattr(parent, method)) ]
> for method in methodList:
> A.append('Method: '+method)
> method=myPopUp(self.dialog_1,A) #myPopup gives a popup menu, and returns the choice
> myattr=method.split()
> if len(myattr)>1:
> s=getattr(parent,myattr[1])
> script=inspect.getsourcefile(s)
> line=inspect.getsourcelines(s)[1]
> print 'TRACE ---->[%s] %s Script: "%s" line %d' %(name,method,script, line)
> else:
> print 'TRACE ----> [%s]' %(name)
> #--------------------------------------------------
>
> With this, I get the name of object that I am inspecting, and a list of methods of parent's class.
>
> What I would like is to show only the relevant methods: - the ones that are BOUND to it!
>
> It is possible?
> I know that I can parse the class code and search for lines that have «.bind», but it is not a good solution, don't you agree?
>
> I hope to have been more clear.
> Thanks for your attention.
>
> Antonio Barbosa
>
> -----Mensagem original-----
> De: Murat Erten [mailto:murerten@gmail.com]
> Enviada: seg 26-02-2007 19:01
> Para: wxPython-users@lists.wxwidgets.org
> Assunto: Re: [wxPython-users] Bound Methods
>
> 2007/2/26, António Barbosa (GN/dpi) <ab@jn.pt>:
> >
> > Hi,
> >
> > I am playing with «introspection» and I would like to know if it is possible
> > to know which method is bound to a particular object.
> >
> > Or putting the problem in another form: It's possible to know which object
> > is bound to a particular method?
> >
> > Suppose a window with 2 buttons (bt1 + bt2)
> >
> > And:
> >
> > bt1.Bind(wx.EVT_BUTTON, self.onButton1)
> >
> > bt2.Bind(wx.EVT_BUTTON, self.onButton2)
> >
> > I can easily identify the two objects, the two methods of their parent and
> > the source line where they are defined.
> >
> > But:
> >
> > How can I know that onButton1 is bound to bt1 and onButton2 is bound to bt2?
> >
> > Or:
> >
> > How can I know that bt1 is bound to onButton1 and bt2 is bound to
> > onButton2?
> >
> > Any help would be appreciated.
> >
> > Antonio Barbosa
> >
>
> I think you already know that by saying:
>
> bt1.Bind(wx.EVT_BUTTON, self.onButton1)
> bt2.Bind(wx.EVT_BUTTON, self.onButton2)
>
> What are you trying to do ? What is your purpose ? Can you be more specific ?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org
>

I looked at the wx.EvtHandler class' functions but I couldn't find
something useful to you :frowning:

Hi Murat

Yes you are right.
I even had that idea, but it doesn't work in a script from some else :wink:
And it will pollute the locals() with a lot of stuff that it will be necessary ONLY in DEBUG MODE.

Thanks, anyway.

It is a pity not having that facility: it would be a great tool, because of the lack of a complete environment IDE for python.
I'm using WxGlade to do the UI, and DrPython to edit the scripts, and I'm quite happy. With that «introspection-tool» I would be happier :wink:

Cheers
Antonio Barbosa

···

-----Original Message-----
From: Murat Erten [mailto:murerten@gmail.com]
Sent: terça-feira, 27 de Fevereiro de 2007 18:07
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] Bound Methods

.......

But maybe while you bind an event to a widget you can do this:

button = wx.Button(frame)
button.bound_methods = list()
...
button.Bind(wx.EVT_BUTTON, func1)
button.bound_methods.append(func1)

If you can get the button object you can get the methods you bind to
it like that.

António Barbosa (GN/dpi) wrote:

With this, I get the name of object that I am inspecting, and a list
of methods of parent's class.

What I would like is to show only the relevant methods: - the ones
that are BOUND to it!

It is possible?

No, the event table populated by the Bind calls is not exposed to wxPython, and is not very accessible from outside the class in C++ either.

One way you could do this is to (optionally) replace the Bind method with your own code that keeps track of the methods that are bound as event handlers, and then calls the original method. Python is flexible enough that you could do this simply by reassigning wx.EvtHandler.Bind.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Hi Robin,

Nice tip!

I don't know how can you pay enough attention to ALL of us, pointing always good directions:)

Thanks, I will work on it.
Antonio Barbosa

-----Mensagem original-----

···

De: Robin Dunn [mailto:robin@alldunn.com]
Enviada: ter 27-02-2007 21:15
Para: wxPython-users@lists.wxwidgets.org
Assunto: Re: [wxPython-users] Bound Methods

António Barbosa (GN/dpi) wrote:

With this, I get the name of object that I am inspecting, and a list
of methods of parent's class.

What I would like is to show only the relevant methods: - the ones
that are BOUND to it!

It is possible?

No, the event table populated by the Bind calls is not exposed to
wxPython, and is not very accessible from outside the class in C++ either.

One way you could do this is to (optionally) replace the Bind method
with your own code that keeps track of the methods that are bound as
event handlers, and then calls the original method. Python is flexible
enough that you could do this simply by reassigning wx.EvtHandler.Bind.

-- javascript:SetCmd(cmdSend);
Enviar
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org