I am working on the presentation of traits from PyCon 2010 . At about 2:30:45, the host begins to cover notifications of feature events that allow (among other things) the ability to automatically call the subroutine at any time a trait has changed.
I run a modified copy of the example that he gave ... In this test, I try to check if I can fire a static event whenever I make changes to volume or inputs .
from traits.api import HasTraits, Range, List, Float import traits class Amplifier(HasTraits): """ Define an Amplifier (a la Spinal Tap) with Enthought traits. Use traits to enforce values boundaries on the Amplifier objects. Use events to notify via the console when the volume trait is changed and when new volume traits are added to inputs. """ volume = Range(value=5.0, trait=Float, low=0.0, high=11.0) inputs = List(volume) # I want to fire a static trait event notification # when another volume element is added def __init__(self, volume=5.0): super(Amplifier, self).__init__() self.volume = volume self.inputs.append(volume) def _volume_changed(self, old, new): # static event listener for self.volume if not (new in self.inputs): self.inputs.append(self.volume) if new == 11.0: print "This one goes to eleven... so far, we have seen", self.inputs def _inputs_changed(self, old, new): # static event listener for self.inputs print "Check it out!!" if __name__=='__main__': spinal_tap = Amplifier() spinal_tap.volume = 11.0 print "DIRECTLY adding a new volume input..." spinal_tap.inputs.append(4.0) try: print "NEGATIVE Test... adding 12.0" spinal_tap.inputs.append(12.0) except traits.trait_errors.TraitError: print "Test passed"
When I run this script, I can see This one goes to eleven... so far, we have seen [5.0, 11.0] on the console output, so I know that _volume_changed() triggered when I assign 11.0 to spinal_tap.volume .
However, I never see any events from _inputs_changed() . No matter what example I am preparing, I cannot get a List to fire the event.
This is the result that I see ... I note that there is no evidence that _inputs_changed() ever works.
[ mpenning@Bucksnort ~]$ python spinaltap.py This one goes to eleven... so far, we have seen [5.0, 11.0] DIRECTLY adding a new volume input... NEGATIVE Test... adding 12.0 Test passed [ mpenning@Bucksnort ~]$
I ran this in both Python2.6 / Cygwin / Windows 7 and Python 2.5 / Linux (all using traits version 4.0.0, which I easy_install directly from the Enthought site ). The results are the same no matter what I have tried so far.
If List can fire a static event when using attributes? If so, am I doing something wrong?