Skip to content

Commit f3a3b26

Browse files
Change HasTraits -> HasStrictTraits.
Other small changes and additions.
1 parent 2a68657 commit f3a3b26

3 files changed

Lines changed: 127 additions & 32 deletions

File tree

slides/01_intro.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ jupyter:
44
text_representation:
55
extension: .md
66
format_name: markdown
7-
format_version: '1.2'
8-
jupytext_version: 1.3.2
7+
format_version: '1.3'
8+
jupytext_version: 1.13.7
99
kernelspec:
10-
display_name: Python 3
10+
display_name: Python 3 (ipykernel)
1111
language: python
1212
name: python3
1313
---
1414

1515
<!-- #region slideshow={"slide_type": "slide"} -->
1616
## Sharing scientific tools: script to desktop application
1717

18-
**Jonathan Rocher, Siddhant Wahal, Jason Chambless, Prabhu Ramachandran**
18+
**Jonathan Rocher, Siddhant Wahal, Jason Chambless, Corran Webster, Prabhu Ramachandran**
1919

2020
**SciPy 2022**
2121

slides/02_traits.md

Lines changed: 122 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ jupyter:
44
text_representation:
55
extension: .md
66
format_name: markdown
7-
format_version: '1.2'
8-
jupytext_version: 1.3.2
7+
format_version: '1.3'
8+
jupytext_version: 1.13.7
99
kernelspec:
10-
display_name: Python 3
10+
display_name: Python 3 (ipykernel)
1111
language: python
1212
name: python3
1313
---
@@ -45,7 +45,7 @@ jupyter:
4545

4646
- Small change to thinking yields big benefits
4747

48-
- Do no conflate GUI with the core model
48+
- Do not mix GUI code with core model
4949

5050
- Build a clean model first
5151

@@ -73,17 +73,16 @@ jupyter:
7373
<!-- #endregion -->
7474

7575
```python
76-
from traits.api import (Delegate, HasTraits,
77-
Instance, Int, Str, observe)
76+
from traits.api import Delegate, HasStrictTraits, Instance, Int, Str, observe
7877

79-
class Parent(HasTraits):
78+
class Parent(HasStrictTraits):
8079
# INITIALIZATION: 'last_name' initialized to ''
8180
last_name = Str('')
8281

8382
```
8483

8584
```python
86-
class Child(HasTraits):
85+
class Child(HasStrictTraits):
8786
age = Int
8887
# VALIDATION: 'father' must be Parent instance
8988
father = Instance(Parent)
@@ -153,30 +152,27 @@ moe.age = 21
153152
<!-- #endregion -->
154153

155154
<!-- #region slideshow={"slide_type": "slide"} -->
156-
## Trait change notification
157-
158-
- Static: `def _<trait_name>_changed()`
159-
- Decorator: `@observe('extended.trait.name')`
160-
- Dynamic:
155+
## `HasStrictTraits` vs. `HasStrictTraits`
161156

162-
```obj.observe(handler, 'extended.trait.name')
163-
```
157+
- Better to use `HasStrictTraits`
158+
- Will catch errors when you mistype or misspell an attribute
159+
- Will not allow setting any attribute not already declared
164160

165-
- See documentation: https://docs.enthought.com/traits/traits_user_manual/notification.html
166161

167162
<!-- #endregion -->
168163

164+
169165
<!-- #region slideshow={"slide_type": "slide"} -->
170166
## Notification example
171167

172168
<!-- #endregion -->
173169

174170
```python
175-
class Parent(HasTraits):
171+
class Parent(HasStrictTraits):
176172
last_name = Str('')
177173

178174

179-
class Child(HasTraits):
175+
class Child(HasStrictTraits):
180176
age = Int
181177
father = Instance(Parent)
182178

@@ -190,20 +186,38 @@ class Child(HasTraits):
190186
```
191187

192188
```python
193-
def handler(event):
194-
print("handler", event.object, event.name, event.old, event.new)
189+
dad = Parent(last_name='Zubizaretta')
190+
c = Child(father=Parent)
195191
```
196192

197193
```python
198-
c.age = 21
199-
c.father = Parent(last_name='Shyam')
194+
dad.last_name = 'Valderrama'
195+
```
196+
197+
```python
198+
def handler(event):
199+
print("handler", event.object, event.name, event.old, event.new)
200200
```
201201

202202
```python
203203
c = Child(father=Parent(last_name='Ram'))
204204
c.observe(handler, 'father, age')
205205
```
206206

207+
<!-- #region slideshow={"slide_type": "slide"} -->
208+
## Trait change notification
209+
210+
- Static: `def _<trait_name>_changed()`
211+
- Decorator: `@observe('extended.trait.name')`
212+
- Dynamic:
213+
214+
```obj.observe(handler, 'extended.trait.name')
215+
```
216+
217+
- See documentation: https://docs.enthought.com/traits/traits_user_manual/notification.html
218+
219+
<!-- #endregion -->
220+
207221
<!-- #region slideshow={"slide_type": "slide"} -->
208222
## Exercise
209223

@@ -223,11 +237,11 @@ c.observe(handler, 'father, age')
223237
```python
224238
from traits.api import Bool, Enum
225239

226-
class Parent(HasTraits):
240+
class Parent(HasStrictTraits):
227241
last_name = Str('')
228242

229243

230-
class Child(HasTraits):
244+
class Child(HasStrictTraits):
231245
age = Int
232246
father = Instance(Parent)
233247
first_name = Str('')
@@ -239,7 +253,7 @@ class Child(HasTraits):
239253

240254
@observe('father.last_name')
241255
def _dad_name_updated(self, event):
242-
print('DAD name', self.father.last_name)
256+
print('Dad name', self.father.last_name)
243257

244258
```
245259

@@ -260,9 +274,9 @@ c = Child(age=21, father=p, first_name='Romano', gender='male')
260274
```python
261275
import datetime
262276

263-
from traits.api import HasTraits, Date, Range
277+
from traits.api import HasStrictTraits, Date, Range
264278

265-
class Thing(HasTraits):
279+
class Thing(HasStrictTraits):
266280
date = Date()
267281
age = Int(12)
268282

@@ -279,3 +293,84 @@ t = Thing()
279293
```python
280294
type(c.age)
281295
```
296+
297+
<!-- #region slideshow={"slide_type": "slide"} -->
298+
## Trait Lists
299+
300+
301+
<!-- #endregion -->
302+
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+
```
319+
320+
<!-- #region slideshow={"slide_type": "slide"} -->
321+
## Trait List events
322+
323+
<!-- #endregion -->
324+
325+
```python
326+
class Bowl(HasStrictTraits):
327+
fruits = List(Str)
328+
def _fruits_changed(self, o, n):
329+
print("Fruits changed", o, n)
330+
331+
def _fruits_items_changed(self, list_event):
332+
print(list_event.index)
333+
print(list_event.removed)
334+
print(list_event.added)
335+
336+
```
337+
338+
```python
339+
b = Bowl()
340+
b.fruits = ['apple']
341+
b.fruits.append('mango')
342+
```
343+
344+
```python
345+
def handler(event):
346+
print("h:", event)
347+
348+
b.observe(handler, 'fruits.items')
349+
b.fruits.append('peach')
350+
```
351+
352+
```python
353+
# Remove the handler
354+
b.observe(handler, 'fruits.items', remove=True)
355+
```
356+
357+
358+
<!-- #region slideshow={"slide_type": "slide"} -->
359+
## Other events
360+
361+
- `TraitChangeEvent`
362+
- `ListChangeEvent`
363+
- `DictChangeEvent`
364+
- `SetChangeEvent`
365+
366+
<!-- #endregion -->
367+
368+
369+
370+
<!-- #region slideshow={"slide_type": "slide"} -->
371+
## Exercise time!
372+
373+
- Take the simple example
374+
- Create a simple Traits model
375+
376+
<!-- #endregion -->

slides/template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jupyter:
1515
<!-- #region slideshow={"slide_type": "slide"} -->
1616
## Sharing scientific tools: script to desktop application
1717

18-
**Jonathan Rocher, Siddhant Wahal, Jason Chambless, Prabhu Ramachandran**
18+
**Jonathan Rocher, Siddhant Wahal, Jason Chambless, Corran Webster, Prabhu Ramachandran**
1919

2020
**SciPy 2022**
2121

0 commit comments

Comments
 (0)