What appears to be a bug in Property display

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

What appears to be a bug in Property display

Jeff Moorcroft
 Hello all,

 Found what appears to be a bug when displaying a Property in traitsui.
 Doing a search of enthought-dev doesn't show similar.

 Running conditions:
 traits 4.1.1 under python 2.7.1 ubuntu 12.04
 traits 4.3 under python 2.7.6 mac OSX10.8.4 (qt backend)

 Observations:
 Item('sizeLista'), sizeLista being a Property, does not update as
 expected. It should be displaying the length of the associated list.
 self.trait_property_changed('sizeLista', self._get_sizeLista())  will
 change it.
 accessing the trait from within a _lista_changed or
 _lista_items_changed
 will produce expected value.

 To Replicate:
 To observe the affect run the following code click on the different
 buttons.  Expected: the items labeled "#lista items", "#listb items" to
 change each time the length of the associated list changes.




 import traits.api as ta
 import traitsui.api as tui

 class PropertyTestGUI(ta.HasTraits):
     lista = ta.List
     listb = ta.List
     sizeLista = ta.Property(depends = 'lista')
     sizeListb = ta.Property#(depends = 'listb')
     changeList1 = ta.Button('change1')
     changeList2 = ta.Button('change2')
     changeList3 = ta.Button('change3')
     strLista = ta.Str
     strListb = ta.Str
 
     traits_view = tui.View(
         tui.Group(
            tui.VGroup(
               tui.Item('strLista', style='readonly', label="lista",),
               tui.Item('strListb', style='readonly', label="listb"),
 
             ),
             tui.HGroup(
               tui.Item('sizeLista', style='readonly', label="#lista
 items"),
               tui.Item('sizeListb', style='readonly', label="#listb
 items"),
 
             ),
             tui.HGroup(
                 tui.Item('changeList1', show_label = False),
                 tui.Item('changeList2', show_label = False),
                 tui.Item('changeList3', show_label = False),
             ),
         ),
         title = 'test_trait_properties',
         resizable = True, scrollable = True
     )
     def __init__(self):
         self.aList = []
         self.anotherList = range(10)
 
     def __str__(self):
         return "lista:={} ||list||{}, listb:={} ||listb||{}".format(
             self.lista, self.sizeLista, self.listb, self.sizeListb)
 
     def _lista_changed(self):
         self.strLista = "{} sizes:{} {}".format(self.lista,
 self.sizeLista, len(self.lista))
 
     def _listb_changed(self):
         self.strListb = "{} sizes:{} {}".format(self.listb,
 self.sizeListb, len(self.listb))
 
     def _lista_items_changed(self):
         self._lista_changed()
 
     def _listb_items_changed(self):
         self._listb_changed()
 
     def _changeList1_fired(self):
         self.lista = range(4)
         self.listb = range(6,10)
         if self.sizeLista != 4 and 4 != len(self.lista):
             print ('property fail!')
 
     def _changeList2_fired(self):
         self.lista = range(1,6)
         self.listb = range(7,12)
         if self.sizeLista != 5 and 5 != len(self.lista):
             print ('property fail!')
         self.trait_property_changed('sizeLista', self._get_sizeLista())
         self.trait_property_changed('sizeListb', self._get_sizeListb())
 
     def _changeList3_fired(self):
         self.lista.append(4)
         self.trait_property_changed('sizeLista', self._get_sizeLista())
         self.listb.append(6)
 
     def _get_sizeLista(self):
         return len(self.lista)
 
     def _get_sizeListb(self):
         return len(self.listb)
 
 if __name__ == '__main__':
     ptg =PropertyTestGUI()
     ptg.lista = [4,5,6]
     print(str(ptg))
     ptg.configure_traits()
     ptg._changeList1_fired()
     if ptg.sizeLista != 4 and 4 != len(ptg.lista):
         print ('property fail!', len(ptg.lista), ptg.sizeLista)
     ptg._changeList2_fired()
     if ptg.sizeLista != 5 and 5 != len(ptg.lista):
         print ('property fail!!',  len(ptg.lista), ptg.sizeLista)
     print(str(ptg))
 

--

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: What appears to be a bug in Property display

Robert Kern
1. You override __init__() but do not call the superclass's
__init__(). This is necessary to hook up some of the notification
mechanism that Properties use. Assuming that aList is actually meant
to be lista, I would write the __init__() like this:

  def __init__(self, **traits):
      traits.setdefault('lista', [])
      traits.setdefault('listb', range(10))
      super(PropertyTestGUI, self).__init__(**traits)

2. It's Property(depends_on=['lista', 'lista_items']), not
Property(depends='lista'). With those changes, you can remove the
explicit self.trait_property_changed() calls.


On Fri, Dec 6, 2013 at 12:12 AM, Jeff Moorcroft
<[hidden email]> wrote:

>  Hello all,
>
>  Found what appears to be a bug when displaying a Property in traitsui.
>  Doing a search of enthought-dev doesn't show similar.
>
>  Running conditions:
>  traits 4.1.1 under python 2.7.1 ubuntu 12.04
>  traits 4.3 under python 2.7.6 mac OSX10.8.4 (qt backend)
>
>  Observations:
>  Item('sizeLista'), sizeLista being a Property, does not update as
>  expected. It should be displaying the length of the associated list.
>  self.trait_property_changed('sizeLista', self._get_sizeLista())  will
>  change it.
>  accessing the trait from within a _lista_changed or
>  _lista_items_changed
>  will produce expected value.
>
>  To Replicate:
>  To observe the affect run the following code click on the different
>  buttons.  Expected: the items labeled "#lista items", "#listb items" to
>  change each time the length of the associated list changes.
>
>
>
>
>  import traits.api as ta
>  import traitsui.api as tui
>
>  class PropertyTestGUI(ta.HasTraits):
>      lista = ta.List
>      listb = ta.List
>      sizeLista = ta.Property(depends = 'lista')
>      sizeListb = ta.Property#(depends = 'listb')
>      changeList1 = ta.Button('change1')
>      changeList2 = ta.Button('change2')
>      changeList3 = ta.Button('change3')
>      strLista = ta.Str
>      strListb = ta.Str
>
>      traits_view = tui.View(
>          tui.Group(
>             tui.VGroup(
>                tui.Item('strLista', style='readonly', label="lista",),
>                tui.Item('strListb', style='readonly', label="listb"),
>
>              ),
>              tui.HGroup(
>                tui.Item('sizeLista', style='readonly', label="#lista
>  items"),
>                tui.Item('sizeListb', style='readonly', label="#listb
>  items"),
>
>              ),
>              tui.HGroup(
>                  tui.Item('changeList1', show_label = False),
>                  tui.Item('changeList2', show_label = False),
>                  tui.Item('changeList3', show_label = False),
>              ),
>          ),
>          title = 'test_trait_properties',
>          resizable = True, scrollable = True
>      )
>      def __init__(self):
>          self.aList = []
>          self.anotherList = range(10)
>
>      def __str__(self):
>          return "lista:={} ||list||{}, listb:={} ||listb||{}".format(
>              self.lista, self.sizeLista, self.listb, self.sizeListb)
>
>      def _lista_changed(self):
>          self.strLista = "{} sizes:{} {}".format(self.lista,
>  self.sizeLista, len(self.lista))
>
>      def _listb_changed(self):
>          self.strListb = "{} sizes:{} {}".format(self.listb,
>  self.sizeListb, len(self.listb))
>
>      def _lista_items_changed(self):
>          self._lista_changed()
>
>      def _listb_items_changed(self):
>          self._listb_changed()
>
>      def _changeList1_fired(self):
>          self.lista = range(4)
>          self.listb = range(6,10)
>          if self.sizeLista != 4 and 4 != len(self.lista):
>              print ('property fail!')
>
>      def _changeList2_fired(self):
>          self.lista = range(1,6)
>          self.listb = range(7,12)
>          if self.sizeLista != 5 and 5 != len(self.lista):
>              print ('property fail!')
>          self.trait_property_changed('sizeLista', self._get_sizeLista())
>          self.trait_property_changed('sizeListb', self._get_sizeListb())
>
>      def _changeList3_fired(self):
>          self.lista.append(4)
>          self.trait_property_changed('sizeLista', self._get_sizeLista())
>          self.listb.append(6)
>
>      def _get_sizeLista(self):
>          return len(self.lista)
>
>      def _get_sizeListb(self):
>          return len(self.listb)
>
>  if __name__ == '__main__':
>      ptg =PropertyTestGUI()
>      ptg.lista = [4,5,6]
>      print(str(ptg))
>      ptg.configure_traits()
>      ptg._changeList1_fired()
>      if ptg.sizeLista != 4 and 4 != len(ptg.lista):
>          print ('property fail!', len(ptg.lista), ptg.sizeLista)
>      ptg._changeList2_fired()
>      if ptg.sizeLista != 5 and 5 != len(ptg.lista):
>          print ('property fail!!',  len(ptg.lista), ptg.sizeLista)
>      print(str(ptg))
>
>
> --
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev



--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev