Python

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Sugestions

    7 answers - 1196 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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.1 | | 1746 bytes | |

    scott a :
    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)
  • No.2 | | 2433 bytes | |

    Fri, 10 Jun 2005 19:34:00 +0300, scott <scott (AT) ebbyfish (DOT) comwrote:

    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 :)
    also why use items instead of directly accessing ndata via
    ndata.iteritems() next in like is that getattr stuff :) 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.

    :)

    Peter.

    To unsubscribe, e-mail: wxPython-users-unsubscribe (AT) lists (DOT) wxwidgets.org
    For additional commands, e-mail: wxPython-users-help (AT) lists (DOT) wxwidgets.org
  • No.3 | | 3234 bytes | |

    sorry Scott but the code you provided is sily

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

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

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

    Scott

    Peter Damoc wrote:
    Fri, 10 Jun 2005 19:34:00 +0300, scott <scott (AT) ebbyfish (DOT) comwrote:

    >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 :)
    also why use items instead of directly accessing ndata via
    ndata.iteritems() next in like is that getattr stuff :) 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.

    :)

    Peter.

    To unsubscribe, e-mail: wxPython-users-unsubscribe (AT) lists (DOT) wxwidgets.org
    For additional commands, e-mail: wxPython-users-help (AT) lists (DOT) wxwidgets.org
  • No.4 | | 1305 bytes | |

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

    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.5 | | 1926 bytes | |

    Fri, 10 Jun 2005 21:15:35 +0300, Scott Mallory <sm (AT) ebbyfish (DOT) comwrote:

    sorry Scott but the code you provided is sily

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

    It takes one to know one ;) I still try to resist the urge to cut'n'paste
    but I seldom succeed :)
    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 (LL). ( and by the way I cut-n-pasted that
    little method for the wx demo ;) )

    I hope you don't think I've worked with Guido on Python's design :)
    I have maybe 18 months with python so I'm not all that far ahead of you :)
    I still aspire to the rank of newbie ;)
    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 ;)

    they will provide for a much pleasanter read than chapter 2.3.8 of
    python's documentation ;)

    Scott

    Peter.

    To unsubscribe, e-mail: wxPython-users-unsubscribe (AT) lists (DOT) wxwidgets.org
    For additional commands, e-mail: wxPython-users-help (AT) lists (DOT) wxwidgets.org
  • No.6 | | 1354 bytes | |

    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.

    David Daniels
    Scott.Daniels@A

    To unsubscribe, e-mail: wxPython-users-unsubscribe (AT) lists (DOT) wxwidgets.org
    For additional commands, e-mail: wxPython-users-help (AT) lists (DOT) wxwidgets.org
  • No.7 | | 1696 bytes | |

    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. :)

    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.

    David Daniels
    Scott.Daniels@A

    To unsubscribe, e-mail: wxPython-users-unsubscribe (AT) lists (DOT) wxwidgets.org
    For additional commands, e-mail: wxPython-users-help (AT) lists (DOT) wxwidgets.org

Re: Sugestions


max 4000 letters.
Your nickname that display:
In order to stop the spam: 8 + 7 =
QUESTION ON "Python"

EMSDN.COM