Can you refactor functions and bindings?

Steve Freedenburg wrote:

Refectoring my configuration frame shrunk the code many, many lines.
The dictionaries that were created to the list worked like a champ...
Is there a way to refactor event binding and event generation? I asked this before
but I think the way I was asking the question was wrong, or the answers I was
given were beyond me, or lost in the thread somewhere.
I have 12 lines that look like this:
self.Bind(wx.EVT_BUTTON, self.C1DefSnd, self.Containers[0]['SndBtn'])
...
self.Bind(wx.EVT_BUTTON, self.C6DefSnd, self.Containers[5]['SndBtn'])
and
self.Bind(wx.EVT_BUTTON, self.C1DefFnt, self.Containers[0]['FntBtn'])
...
self.Bind(wx.EVT_BUTTON, self.C6DefFnt, self.Containers[5]['FntBtn'])
and 6 functions for the event DefSnd, and 6 functions for the event DefFnt
... I can't write:
def self.Functions[0]['SndDef'](self, event): Or can I?

No. The key is realizing that the "event" parameter that is given to your event function contains the button that was hit. You can use one handler for all 6 buttons. Inside the handler, if you say this:
    def handler( self, event ):
        btn = event.GetEventObject()

then "btn" will be the wx.Button that was originally bound to the event. So, if you store an extra piece of information in the button object, you can get it back. For example:
    self.Containers[0]['SndBtn'].Container = self.Containers[0]
    self.Bind( wx.EVT_BUTTON, self.handler, self.Containers[0]['SndBtn'] )
    ...
    def handler( self, event ):
        btn = event.GetEventObject()
        cntr = btn.Container

Now "cntr" is the dictionary corresponding to this button.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Oh, man, that was so easy… I’m ashamed… I was trying something

similar, but it didn’t work, and each attempt after was more and more

wrong. I still feel like I’m learning, which is a good thing.

···

----- Original Message -----

From:
Nathaniel Echols

To: wxpython-users@lists.wxwidgets.org

Sent: Tuesday, 22 July, 2008 20:21

Subject: Re: [wxpython-users] Can you refactor functions and bindings?

On Tue, Jul 22, 2008 at 5:16 PM, Steve Freedenburg stevefreedenburg@charter.net wrote:

I wrote this just to see if it worked... and it does of course... but I still
have 6 lines binding the 6 buttons to this handler.  Is there a way to say

this:

      self.Bind(wx.EVT_BUTTON, self.CDefSnd, self.Containers[0]['SndBtn'])
      [ etc. ]

with something like this:

      self.Bind(wx.EVT_BUTTON, self.CDefDnd, self.Containers[ALL]['SndBtn'])

just iterate through the list:

for Container in self.Containers:

self.Bind(wx.EVT_BUTTON, self.CDefDnd, Container[‘SndBtn’])

-Nat



wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Are my notes getting to the list???

Anyway, I still don't know your use-case so it's hard to comment.

Steve Freedenburg wrote:

Awesome!!!
   def CDefSnd(self, event):
       btn = event.GetEventObject()
       if btn == self.Containers[0]['SndBtn']:
           print "This came from Container 1."
       if btn == self.Containers[1]['SndBtn']:
           print "This came from Container 2."
       event.Skip()

I don't like this for multiple reasons:

1) You generally don't spell "switch" with ifs in python -- a dict is probably better. Google for discussion of that.

2) using an index to identify which container is a bit ugly, and searching for it is also.

3) you should have the Container or the button have some mets-data with it identifying it, a name, Id, something, rather than identifying it by where it happens to be in the list. Again, what makes sense depends on your use case.

If you read my emails, and used the lambda trick, you'd see that you could spell this something like:

     def CDefSnd(self, event, index):
          print "This came from Container %i."%index
          event.Skip()

Isn't that better?

have 6 lines binding the 6 buttons to this handler. Is there a way to say
this:

I think you got that answered.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Steve Freedenburg wrote:

Oh, man, that was so easy.... I'm ashamed... I was trying something
similar, but it didn't work, and each attempt after was more and more
wrong. <sigh> I still feel like I'm learning, which is a good thing.

It's kind of fun to watch you reach all of these "AHA!" moments. That's one of the things I love about Python. Once you get the philosophy, it becomes pretty easy to write powerful programs.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.