Not an "Aha!" but a "DOH!" momment.

So I’ve been messing with the refactoring of, and abstracting (as I think

it was called) of the program I’ve been working on.

And had a “DOH!” momment more so than a “Aha!” momment.

For the past few hours I have been renaming things to try and

make them make more sense.

So every time an “add container” button is clicked this happens:

def ContAddEvent(self, event):
    ContButton = {'ButtonNumber':''}
    ContButtonLen = len(self.ContButtonsList)
    i = ContButtonLen + 1
    ContButton['ButtonNumber'] = wx.Button(self.ContButtonPanel, -1, 'Container %i'%i)
    ContButton['ButtonNumber'].SetToolTipString('Click here to configure this container.')
    self.ContButtonPanelSBS.Add(ContButton['ButtonNumber'], 1, wx.EXPAND | wx.ALL, 5)
    self.Fit()
    self.ContButtonsList.append(ContButton)

And everything worked great, even the sequencing, for removing buttons was just about the

opposite of the above function.

def ContRemoveEvent(self, event):
    try:
        ContButtonsLen = len(self.ContButtonsDict)
        x = ContButtonsLen - 1
        self.ContButtonPanelSBS.Remove(self.ContButtonsList[x]['ButtonNumber'])
        self.Fit()
        del self.ContButtonsList[x]
    except:
        dlg = wx.MessageDialog(self, 'There are not any buttons to remove.', 'Container Button Error', wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

I had to through in an exception in case the user clicks “remove button” and there

are not buttons to remove. As you can see.

And elsewhere in the code was:

for buttons in self.ContButtonList:

    self.Bind(wx.EVT_BUTTON, self.ContConfig, buttons['ButtonNumber'])

to reference the list of dictionaries.

Can anyone see my problem? I didn’t for about half an eternity…

that Bind needs to be in the ConAddEvent function with the for loop taken out.

My buttons weren’t doing anything… so I was scratching my head, because the

code had worked for other things before… Problem solved, but it goes to

illustrate how things propogate in Python. I’m still trying to wrap my mind around

it.

Steve

Steve Freedenburg wrote:

Well Paul,

The reason I used a dictionary and
not a list is because I ‘may’ be inclined to add come code to the

button creation function that would
be something to the effect of make buttons 1 through 10 in this

BoxSizer, and 11-20 in this
BoxSizer, and 21-30 in this BoxSizer. I don’t see a need to do this yet

but I have built myself a way to do
it with less restructuring of the code, by simply having to add

more keys.

Next, I won’t debate about the
clutter of one function from the other, they do pretty much the same

thing by looking at it. I am slowly
adapting to putting more ‘instruction’ in a line though. So in

time I hope to think like you do
with regard to code generation. I’ll stick with what I know how to

do here an now, and maybe in 3
hours, I’ll buckle and adopt your fuction…

I can only speak of what I know, and
it does make sense that any language requires

an item to exist for you start
assigning it to something. Which I was doing the inverse. I was

assigning it to something and
building it later.

I had no plans to master Python, but
it does seem to be creeping up on me slowly. I can write

out code now without having to look
at examples for everything. That is refreshing, it’s beginning

to flow.

Paul, the upshot here is ever time I
get a reply to one of my e-mails I get so much out of it.

And I’ve learned a smidge off this
e-mail. Enough smidges and I’ll be as good as you all one day.

I hope…

Do you think I’m right about keeping
a dictionary now? For the “possibility” of adding a “feature”

maybe never, but as early as one
hour from now?

Good question. It’s a matter of style and both approaches work.
Personally, I would not replace a simple variable with a dictionary
unless that is really what I needed. In general, I prefer to solve the
problem in front of me as simply as possible. If I need a dictionary
later, so be it; I’ll change it if and when it becomes a requirement.
However if I never need one, I’ve saved myself some time and trouble.
Python design principles: “Simple is better than complex” and
“Readability matters.” Programming is a practical endeavor.

Another reason is that, six months from now, somebody will read your
code and wonder why you used a dictionary in that spot. He will search
through the code to find a reason, looking for a use of the dictionary
functionality. That somebody is very likely to be you. I try not to
seed my future work with unnecessary brain-teasers, no matter how small.

···

wxpython-users@lists.wxwidgets.orghttp://lists.wxwidgets.org/mailman/listinfo/wxpython-userswxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
wxpython-users@lists.wxwidgets.orghttp://lists.wxwidgets.org/mailman/listinfo/wxpython-users