@@ -41,7 +41,7 @@ jupyter:
4141<!-- #region slideshow={"slide_type": "slide"} -->
4242## Why all this?
4343
44- - No pain, no gain!
44+ - No pain, no gain! (Only a little pain, we promise!)
4545
4646- Small change to thinking yields big benefits
4747
@@ -181,7 +181,7 @@ class Child(HasStrictTraits):
181181
182182 @observe (' father.last_name' )
183183 def _dad_name_updated (self , event ):
184- print (' DAD name' , self .father.last_name)
184+ print (Father name changed to ' , self.father.last_name)
185185
186186```
187187
@@ -194,25 +194,11 @@ c = Child(father=Parent)
194194dad.last_name = ' Valderrama'
195195```
196196
197- ``` python
198- def handler (event ):
199- print (" handler" , event.object, event.name, event.old, event.new)
200- ```
201-
202- ``` python
203- c = Child(father = Parent(last_name = ' Ram' ))
204- c.observe(handler, ' father, age' )
205- ```
206-
207197< !-- # region slideshow={"slide_type": "slide"} -->
208198# # Trait change notification
209199
210200- Static: `def _< trait_name> _changed()`
211201- Decorator: `@ observe(' extended.trait.name' )`
212- - Dynamic:
213-
214- ``` obj.observe(handler, 'extended.trait.name')
215- ```
216202
217203- See documentation: https:// docs.enthought.com/ traits/ traits_user_manual/ notification.html
218204
@@ -245,8 +231,8 @@ class Child(HasStrictTraits):
245231 age = Int
246232 father = Instance(Parent)
247233 first_name = Str(' ' )
248- alive = Bool(True )
249- gender = Enum(' female ' , ' male ' , ' neither ' )
234+ likes_queso = Bool(True )
235+ handedness = Enum(' right ' , ' left ' )
250236
251237 def _age_changed(self , old, new):
252238 print (' Age changed from %s to %s ' % (old, new))
@@ -259,113 +245,96 @@ class Child(HasStrictTraits):
259245
260246```python
261247p = Parent(last_name = ' Ray' )
262- c = Child(age = 21 , father = p, first_name = ' Romano' , gender = ' male ' )
248+ c = Child(age = 21 , father = p, first_name = ' Romano' , handedness = ' right ' )
263249```
264250
265- <!-- #region slideshow={"slide_type": "slide"} -->
266- ## Setting default values
267251
268- - For simple cases, use the default of the trait
269- - For more complex cases use a special method
270- - A simple example
252+ < !-- # region slideshow={"slide_type": "slide"} -->
253+ # # Trait change event
271254
255+ - Recall this
272256< !-- # endregion -->
273257
274258```python
275- import datetime
259+ @ observe(' father.last_name' )
260+ def _dad_name_updated(self , event):
261+ print (' Dad name' , self .father.last_name)
262+ ```
276263
277- from traits.api import HasStrictTraits, Date, Range
264+ - `event` is a `TraitChangeEvent` instance
278265
279- class Thing (HasStrictTraits ):
280- date = Date()
281- age = Int(12 )
282266
283- def _date_default (self ):
284- print (' default' )
285- return datetime.datetime.today()
286- ```
267+ ```python slideshow={" slide_type" : " slide" }
268+ class Child(HasStrictTraits):
269+ age = Int
270+ father = Instance(Parent)
271+ first_name = Str(' ' )
272+ likes_queso = Bool(True )
273+ handedness = Enum(' right' , ' left' )
287274
275+ def _age_changed(self , old, new):
276+ print (' Age changed from %s to %s ' % (old, new))
277+
278+ @ observe(' father.last_name' )
279+ def _dad_name_updated(self , event):
280+ print (event.object, event.name, event.old, event.new)
281+ print (' Dad name' , self .father.last_name)
282+
283+ ```
288284
289285```python
290- t = Thing()
286+ p = Parent(last_name = ' Ray' )
287+ c = Child(age = 21 , father = p, first_name = ' Romano' , handedness = ' right' )
291288```
292289
293290```python
294- type (c.age)
291+ p.last_name = ' Ahmed '
295292```
296293
297294< !-- # region slideshow={"slide_type": "slide"} -->
298- ## Trait Lists
295+ # # Other Trait Events
299296
297+ - Advanced, for other kind of traits
298+ - `List` trait changes: `ListChangeEvent`
299+ - `Dict` trait changes: `DictChangeEvent`
300+ - `Set` trait changes: `SetChangeEvent`
300301
301302< !-- # endregion -->
302303
303- ``` python
304- from traits.api import List
305-
306- class Bowl (HasStrictTraits ):
307- fruits = List(Str)
308-
309- def _fruits_changed (self , o , n ):
310- print (" Fruits changed" , o, n)
311-
312- ```
313-
314- ``` python
315- b = Bowl()
316- b.fruits = [' apple' ]
317- b.fruits.append(' mango' )
318- ```
319304
320305< !-- # region slideshow={"slide_type": "slide"} -->
321- ## Trait List events
306+ # # Setting default values
307+
308+ - For simple cases, use the default of the trait
309+ - For more complex cases use a special method
310+ - A simple example
322311
323312< !-- # endregion -->
324313
325314```python
326- class Bowl (HasStrictTraits ):
327- fruits = List(Str)
328- def _fruits_changed (self , o , n ):
329- print (" Fruits changed" , o, n)
315+ import datetime
330316
331- def _fruits_items_changed (self , list_event ):
332- print (list_event.index)
333- print (list_event.removed)
334- print (list_event.added)
317+ from traits.api import HasStrictTraits, Date, Range
335318
336- ```
319+ class Thing(HasStrictTraits):
320+ date = Date()
321+ age = Int(12 )
337322
338- ``` python
339- b = Bowl()
340- b.fruits = [' apple' ]
341- b.fruits.append(' mango' )
323+ def _date_default(self ):slideshow = {" slide_type" : " slide" }
324+ print (' default' )
325+ return datetime.datetime.today()
342326```
343327
344- ``` python
345- def handler (event ):
346- print (" h:" , event)
347328
348- b.observe(handler, ' fruits.items ' )
349- b.fruits.append( ' peach ' )
329+ ```python
330+ t = Thing( )
350331```
351332
352333```python
353- # Remove the handler
354- b.observe(handler, ' fruits.items' , remove = True )
334+ type (c.age)
355335```
356336
357337
358- <!-- #region slideshow={"slide_type": "slide"} -->
359- ## Other events
360-
361- - ` TraitChangeEvent `
362- - ` ListChangeEvent `
363- - ` DictChangeEvent `
364- - ` SetChangeEvent `
365-
366- <!-- #endregion -->
367-
368-
369338
370339< !-- # region slideshow={"slide_type": "slide"} -->
371340# # Exercise time!
0 commit comments