Debian Codegraph @

examples/tutorials/traits_4.0/delegation/delegation.py

raw
1 # (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10
11 # --(Delegation Fixes and Improvements)----------------------------------------
12 """
13 Delegation Fixes and Improvements
14 =================================
15
16 In previous versions of Traits there were a number of problems (i.e. bugs) in
17 the delegation support that made delegation virtually unusable in some
18 situations.
19
20 As a result, one of the primary goals of Traits 3.0 was to fix all known
21 problems with delegation, thus allowing it to reclaim its role as one of the
22 five keys pillars of the Traits package (those pillars being *initialization*,
23 *validation*, *notification*, *delegation* and *visualization*).
24
25 We are happy to report that not only have all known bugs been fixed, but a
26 previously unsupported, though often requested, feature has been added as well.
27
28 Delegation Event Notification
29 -----------------------------
30
31 Previously, many Traits users implicitly assumed that changes made to a
32 *delegatee* trait would generate a change notification on any *delegater* trait
33 (no matter how many such traits there might be). Unfortunately, this was not
34 the case.
35
36 However, starting with Traits 3.0, this feature has now been implemented. An
37 example of what this means is shown below::
38
39 class Parent(HasTraits):
40
41 first_name = Str
42 last_name = Str
43
44 class Child(HasTraits):
45
46 mother = Instance(Parent)
47 father = Instance(Parent)
48
49 first_name = Str
50 last_name = Delegate('father')
51
52 In this example, we've created two classes, **Parent** and **Child**, and the
53 value of the **Child** class's *last_name* trait delegates its value to its
54 *father* object's *last_name* trait.
55
56 Next, we'll set up a simple set of test objects::
57
58 mom = Parent(first_name='Julia', last_name='Wilson')
59 dad = Parent(first_name='William', last_name='Chase')
60 son = Child(mother=mom, father=dad, first_name='John')
61
62 Finally, we'll set up a notification handler on the *son* object's *last_name*
63 trait and then try out various combinations of setting both the *father* and
64 *son* object's *last_name* trait to see in which cases the notification handler
65 is called::
66
67 def name_changed(name):
68 print('Your last name has been changed to %s.' % name)
69
70 # Set up a change notification handler on the son's last name:
71 son.on_trait_change(name_changed, 'last_name')
72
73 # This should cause the son's last name to change as well:
74 print("Changing dad's last name to Jones.")
75 dad.last_name = 'Jones'
76
77 # This change overrides the father's last name for the son:
78 print("Changing son's last name to Thomas.")
79 son.last_name = 'Thomas'
80
81 # This should no longer have any effect on the son's last name:
82 print("Changing dad's last name to Riley.")
83 dad.last_name = 'Riley'
84
85 # Son decides to revert his name back to his father's name:
86 print("Reverting son's last name.")
87 del son.last_name
88
89 # Now changing the father's name should affect the son again:
90 print("Changing dad's last name to Simmons.")
91 dad.last_name = 'Simmons'
92
93 For the actual results of running this code, refer to the **Output** tab. Note
94 that for each case in which an explicit or implicit change is made to the *son*
95 object's *last_name* trait, a corresponding call is made to the change
96 notification handler.
97
98 """
99
100 # FIXME - this need to be redone without traitsui
101
102 from traits.api import Delegate, HasTraits, Instance, Str
103
104
105 # --[Parent Class]-------------------------------------------------------------
106 class Parent(HasTraits):
107
108 first_name = Str
109 last_name = Str
110
111
112 # --[Child Class]--------------------------------------------------------------
113 class Child(HasTraits):
114
115 mother = Instance(Parent)
116 father = Instance(Parent)
117
118 first_name = Str
119 last_name = Delegate("father")
120
121
122 # --[Example*]-----------------------------------------------------------------
123
124 mom = Parent(first_name="Julia", last_name="Wilson")
125 dad = Parent(first_name="William", last_name="Chase")
126 son = Child(mother=mom, father=dad, first_name="John")
127
128
129 def name_changed(name):
130 print("Your last name has been changed to %s." % name)
131
132
133 # Set up a change notification handler on the son's last name:
134 son.on_trait_change(name_changed, "last_name")
135
136 # This should cause the son's last name to change as well:
137 print("Changing dad's last name to Jones.")
138 dad.last_name = "Jones"
139
140 # This change overrides the father's last name for the son:
141 print("Changing son's last name to Thomas.")
142 son.last_name = "Thomas"
143
144 # This should no longer have any effect on the son's last name:
145 print("Changing dad's last name to Riley.")
146 dad.last_name = "Riley"
147
148 # Son decides to revert his name back to his father's name:
149 print("Reverting son's last name.")
150 del son.last_name
151
152 # Now changing the father's name should affect the son again:
153 print("Changing dad's last name to Simmons.")
154 dad.last_name = "Simmons"