Sugestions

Greetings,
  I was wondering if anyone can suggest a more efficient way writing the following similar methods. I have about 12 of these and would like to reduce their number and optimize my code by removing such redundant methods.

:: guiComT1 & guiComT2 are imported classes from different py files ::
:: speed is not an issue as the largest of the ListBoxes will only have
  about 30 or so rows.

...

def onUpdateListBox_T1 (self, name, box):
   ct = getattr(self.guiComT1, box).GetItemCount()
   ct=+1
   ndata = { ct : (name)}
   items = ndata.items()
   for key, data in items:
       index = getattr(self.guiComT1, box).InsertStringItem(ct, data)
       getattr(self.guiComT1, box).SetStringItem(index, 0, data)
       getattr(self.guiComT1, box).SetItemData(index, key)

def onUpdateListBox_T2 (self, name, box):
    ct = getattr(self.guiComT2, box).GetItemCount()
    ct=+1
    ndata = { ct : (name)}
    items = ndata.items()
    for key, data in items:
        index = getattr(self.guiComT2, box).InsertStringItem(ct, data)
        getattr(self.guiComT2, box).SetStringItem(index, 0, data)
        getattr(self.guiComT2, box).SetItemData(index, key)

...

Thanks,
Scott

···

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.6.6 - Release Date: 6/8/2005

scott a écrit :

Greetings,
    I was wondering if anyone can suggest a more efficient way writing the following similar methods. I have about 12 of these and would like to reduce their number and optimize my code by removing such redundant methods.

:: guiComT1 & guiComT2 are imported classes from different py files ::
:: speed is not an issue as the largest of the ListBoxes will only have
about 30 or so rows.

...

def onUpdateListBox_T1 (self, name, box):
  ct = getattr(self.guiComT1, box).GetItemCount()
  ct=+1
  ndata = { ct : (name)}
  items = ndata.items()
  for key, data in items:
      index = getattr(self.guiComT1, box).InsertStringItem(ct, data)
      getattr(self.guiComT1, box).SetStringItem(index, 0, data)
      getattr(self.guiComT1, box).SetItemData(index, key)

def onUpdateListBox_T2 (self, name, box):
   ct = getattr(self.guiComT2, box).GetItemCount()
   ct=+1
   ndata = { ct : (name)}
   items = ndata.items()
   for key, data in items:
       index = getattr(self.guiComT2, box).InsertStringItem(ct, data)
       getattr(self.guiComT2, box).SetStringItem(index, 0, data)
       getattr(self.guiComT2, box).SetItemData(index, key)

A good way is to create a nested function:

def updateListFunction(guiCom):
     def onUpdateListBox(self, name, box):
         ct = getattr(guiCom, box).GetItemCount()
         ct=+1
         ndata = { ct : (name)}
         items = ndata.items()
         for key, data in items:
             index = getattr(guiCom, box).InsertStringItem(ct, data)
             getattr(guiCom, box).SetStringItem(index, 0, data)
             getattr(guiCom, box).SetItemData(index, key)
     return onUpdateListBox
...
onUpdateListBox_T1 = updateListFunction(self.guiComT1)
onUpdateListBox_T2 = updateListFunction(self.guiComT2)
...

···

--
Amaury

sorry Scott but the code you provided is sily... I mean.... why would you create a dictionary with one key, item pair to iterate over it... :slight_smile:
also.... why use items instead of directly accessing ndata via ndata.iteritems().... next in like is that getattr stuff.... :slight_smile: why not use the box argument directly and call that method with getattr(self.guiComT1, box).... even if you don't want to do that... wouldn't it be easier to do a

box = getattr(self.guiComT1, box) #at the begining of the method and use that instance from then on...
ct = box.GetItemCount()
....
index = box.InsertStringItem(ct, data)..... etc

if this is code used in event handler.... and it smells like it is... you could use only one method onUpdateListBox and call it with the instance you need... the methods you provided do the same thing... add a string to a listbox... the only difference is the target listbox.... and that could be provided as a argument.

:slight_smile:

Peter.

···

On Fri, 10 Jun 2005 19:34:00 +0300, scott <scott@ebbyfish.com> wrote:

Greetings,
  I was wondering if anyone can suggest a more efficient way writing the following similar methods. I have about 12 of these and would like to reduce their number and optimize my code by removing such redundant methods.

:: guiComT1 & guiComT2 are imported classes from different py files ::
:: speed is not an issue as the largest of the ListBoxes will only have
  about 30 or so rows.

...

def onUpdateListBox_T1 (self, name, box):
   ct = getattr(self.guiComT1, box).GetItemCount()
   ct=+1
   ndata = { ct : (name)}
   items = ndata.items()
   for key, data in items:
       index = getattr(self.guiComT1, box).InsertStringItem(ct, data)
       getattr(self.guiComT1, box).SetStringItem(index, 0, data)
       getattr(self.guiComT1, box).SetItemData(index, key)

def onUpdateListBox_T2 (self, name, box):
    ct = getattr(self.guiComT2, box).GetItemCount()
    ct=+1
    ndata = { ct : (name)}
    items = ndata.items()
    for key, data in items:
        index = getattr(self.guiComT2, box).InsertStringItem(ct, data)
        getattr(self.guiComT2, box).SetStringItem(index, 0, data)
        getattr(self.guiComT2, box).SetItemData(index, key)

...

Thanks,
Scott

Hi scott,

scott wrote:

Greetings,
    I was wondering if anyone can suggest a more efficient way writing the following similar methods. I have about 12 of these and would like to reduce their number and optimize my code by removing such redundant methods.

:: guiComT1 & guiComT2 are imported classes from different py files ::
:: speed is not an issue as the largest of the ListBoxes will only have
about 30 or so rows.

wouldn't this work?

def updateListBox (self, listbox, name, box):
   ct = getattr(listbox, box).GetItemCount()
   ct=+1
   ndata = { ct : (name)}
   items = ndata.items()
   for key, data in items:
       index = getattr(listbox, box).InsertStringItem(ct, data)
       getattr(listbox, box).SetStringItem(index, 0, data)
       getattr(listbox, box).SetItemData(index, key)

def onUpdateListBox_T1 (self, name, box):
   self.updateListBox(self.guiComT1,name,box)

def onUpdateListBox_T2 (self, name, box):
   self.updateListBox(self.guiComT2,name,box)

Adi

sorry Scott but the code you provided is sily...

:slight_smile: Silly! Indeeed Peter, have you no mercy for newbies? :slight_smile:

I have been working with Python for only 6 months, so forgive me if I make 'silly' mistakes (LOL). (Oh and by the way I cut-n-pasted that little method for the wx demo :wink: )

But thank you for your advice, I will read up on iteritems()

Scott

Peter Damoc wrote:

···

On Fri, 10 Jun 2005 19:34:00 +0300, scott <scott@ebbyfish.com> wrote:

Greetings,
    I was wondering if anyone can suggest a more efficient way writing the following similar methods. I have about 12 of these and would like to reduce their number and optimize my code by removing such redundant methods.

:: guiComT1 & guiComT2 are imported classes from different py files ::
:: speed is not an issue as the largest of the ListBoxes will only have
  about 30 or so rows.

...

def onUpdateListBox_T1 (self, name, box):
   ct = getattr(self.guiComT1, box).GetItemCount()
   ct=+1
   ndata = { ct : (name)}
   items = ndata.items()
   for key, data in items:
       index = getattr(self.guiComT1, box).InsertStringItem(ct, data)
       getattr(self.guiComT1, box).SetStringItem(index, 0, data)
       getattr(self.guiComT1, box).SetItemData(index, key)

def onUpdateListBox_T2 (self, name, box):
    ct = getattr(self.guiComT2, box).GetItemCount()
    ct=+1
    ndata = { ct : (name)}
    items = ndata.items()
    for key, data in items:
        index = getattr(self.guiComT2, box).InsertStringItem(ct, data)
        getattr(self.guiComT2, box).SetStringItem(index, 0, data)
        getattr(self.guiComT2, box).SetItemData(index, key)

...

Thanks,
Scott

sorry Scott but the code you provided is sily... I mean.... why would you create a dictionary with one key, item pair to iterate over it... :slight_smile:
also.... why use items instead of directly accessing ndata via ndata.iteritems().... next in like is that getattr stuff.... :slight_smile: why not use the box argument directly and call that method with getattr(self.guiComT1, box).... even if you don't want to do that... wouldn't it be easier to do a

box = getattr(self.guiComT1, box) #at the begining of the method and use that instance from then on...
ct = box.GetItemCount()
....
index = box.InsertStringItem(ct, data)..... etc

if this is code used in event handler.... and it smells like it is... you could use only one method onUpdateListBox and call it with the instance you need... the methods you provided do the same thing... add a string to a listbox... the only difference is the target listbox.... and that could be provided as a argument.

:slight_smile:

Peter.

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

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.6.6 - Release Date: 6/8/2005

Thanks everybody for the suggestions, Now I can see better ways to improve my code. :slight_smile:

Scott

scott wrote:

···

Greetings,
    I was wondering if anyone can suggest a more efficient way writing the following similar methods. I have about 12 of these and would like to reduce their number and optimize my code by removing such redundant methods.

:: guiComT1 & guiComT2 are imported classes from different py files ::
:: speed is not an issue as the largest of the ListBoxes will only have
about 30 or so rows.

...

def onUpdateListBox_T1 (self, name, box):
  ct = getattr(self.guiComT1, box).GetItemCount()
  ct=+1
  ndata = { ct : (name)}
  items = ndata.items()
  for key, data in items:
      index = getattr(self.guiComT1, box).InsertStringItem(ct, data)
      getattr(self.guiComT1, box).SetStringItem(index, 0, data)
      getattr(self.guiComT1, box).SetItemData(index, key)

def onUpdateListBox_T2 (self, name, box):
   ct = getattr(self.guiComT2, box).GetItemCount()
   ct=+1
   ndata = { ct : (name)}
   items = ndata.items()
   for key, data in items:
       index = getattr(self.guiComT2, box).InsertStringItem(ct, data)
       getattr(self.guiComT2, box).SetStringItem(index, 0, data)
       getattr(self.guiComT2, box).SetItemData(index, key)

...

Thanks,
Scott

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.6.6 - Release Date: 6/8/2005

> sorry Scott but the code you provided is sily...

:slight_smile: Silly! Indeeed Peter, have you no mercy for newbies? :slight_smile:

It takes one to know one :wink: I still try to resist the urge to cut'n'paste but I seldom succeed :slight_smile:
a lot of my code is spagetti, a lot of noise.... and this kind of errors only accumulates till is too difficult to manage and I have to reimplement stuff.

Einstein said "Make things as simple as you can, but no simpler."
Every time I listen to it I had something to gain, every time I cut the number of lines of code I gained something...
benefits ranging from a big smile when I sumbled uppon a line of code from when I was learning functional programming, something like
map(pacs[pac][op].append, filter(isdir, map(lambda dir:opj(datesh, dir), os.listdir(datesh))))
to benefits like trivial code alteration for a project I've done recently.

I have been working with Python for only 6 months, so forgive me if I make 'silly' mistakes (LOL). (Oh and by the way I cut-n-pasted that little method for the wx demo :wink: )

I hope you don't think I've worked with Guido on Python's design..... :slight_smile:
I have maybe 18 months with python so I'm not all that far ahead of you :slight_smile:
I still aspire to the rank of newbie :wink:
http://www.catb.org/~esr/writings/unix-koans/

But thank you for your advice, I will read up on iteritems()

well... if you're in a mood to read here are 2 links I liked a lot :wink:
http://www.ferg.org/projects/ferg-event_driven_programming.html
http://www.artima.com/intv/simplest3.html
they will provide for a much pleasanter read than chapter 2.3.8 of python's documentation :wink:

Scott

Peter.

···

On Fri, 10 Jun 2005 21:15:35 +0300, Scott Mallory <sm@ebbyfish.com> wrote:

scott wrote:

  ct = getattr(self.guiComT1, box).GetItemCount()
  ct=+1

Assuming this as as typo for:

  ct = getattr(self.guiComT1, box).GetItemCount()
  ct += 1

You could simplify as either:

     def updater(entry):
         def onUpdate(self, name, boxname):
             box = getattr(getattr(self, entry), boxname)
             ct = box.GetItemCount() + 1
             index = box.InsertStringItem(ct, name)
             box.SetStringItem(index, 0, name)
             box.SetItemData(index, ct)
         return onUpdate
     ...
         class Whatever ...
             onUpdateListBox_T1 = updater('guiComT1')
             onUpdateListBox_T2 = updater('guiComT2')

_or_:

     class Whatever ...
         @staticmethod
         def doUpdate(entry, name, boxname):
             box = getattr(entry, boxname)
             ct = box.GetItemCount() + 1
             index = box.InsertStringItem(ct, name)
             box.SetStringItem(index, 0, name)
             box.SetItemData(index, ct)

         def onUpdateListBox_T1(self, name, box):
             self.doUpdate(self.guiComT1, name, box)

         def onUpdateListBox_T2(self, name, box):
             self.doUpdate(self.guiComT2, name, box)

I personally prefer the latter.

--Scott David Daniels
Scott.Daniels@Acm.Org

Thanks! I didn't even realize I was falling back to my old code habits.
I spent my first 4 years as a developer writing VB code for MS Access and the like (not my choice but it was my first programming job), so I still have allot to unlearn. :slight_smile:

Scott

Scott David Daniels wrote:

···

scott wrote:

  ct = getattr(self.guiComT1, box).GetItemCount()
  ct=+1

Assuming this as as typo for:

  ct = getattr(self.guiComT1, box).GetItemCount()
  ct += 1

You could simplify as either:

    def updater(entry):
        def onUpdate(self, name, boxname):
            box = getattr(getattr(self, entry), boxname)
            ct = box.GetItemCount() + 1
            index = box.InsertStringItem(ct, name)
            box.SetStringItem(index, 0, name)
            box.SetItemData(index, ct)
        return onUpdate
    ...
        class Whatever ...
            onUpdateListBox_T1 = updater('guiComT1')
            onUpdateListBox_T2 = updater('guiComT2')

_or_:

    class Whatever ...
        @staticmethod
        def doUpdate(entry, name, boxname):
            box = getattr(entry, boxname)
            ct = box.GetItemCount() + 1
            index = box.InsertStringItem(ct, name)
            box.SetStringItem(index, 0, name)
            box.SetItemData(index, ct)

        def onUpdateListBox_T1(self, name, box):
            self.doUpdate(self.guiComT1, name, box)

        def onUpdateListBox_T2(self, name, box):
            self.doUpdate(self.guiComT2, name, box)

I personally prefer the latter.

--Scott David Daniels
Scott.Daniels@Acm.Org

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

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.6.7 - Release Date: 6/10/2005