##// END OF EJS Templates
A few more small traitlets features.
Brian Granger -
Show More
@@ -46,7 +46,7 b' class HasTraitletsStub(object):'
46 self._traitlet_values = {}
46 self._traitlet_values = {}
47 self._traitlet_notifiers = {}
47 self._traitlet_notifiers = {}
48
48
49 def _notify(self, name, old, new):
49 def _notify_traitlet(self, name, old, new):
50 self._notify_name = name
50 self._notify_name = name
51 self._notify_old = old
51 self._notify_old = old
52 self._notify_new = new
52 self._notify_new = new
@@ -321,6 +321,15 b' class TestAddTraitlet(TestCase):'
321 self.assertEquals(a.b,10.0)
321 self.assertEquals(a.b,10.0)
322 self.assertRaises(TraitletError, setattr, a, 'b', 'bad value')
322 self.assertRaises(TraitletError, setattr, a, 'b', 'bad value')
323
323
324 class TestTraitletKeys(TestCase):
325
326 def test_keys(self):
327 class A(HasTraitlets):
328 a = Int
329 b = Float
330 a = A()
331 self.assertEquals(a.traitlet_keys(),['a','b'])
332
324
333
325 #-----------------------------------------------------------------------------
334 #-----------------------------------------------------------------------------
326 # Tests for specific traitlet types
335 # Tests for specific traitlet types
@@ -16,9 +16,15 b" We don't support:"
16
16
17 * Delegation
17 * Delegation
18 * Automatic GUI generation
18 * Automatic GUI generation
19 * A full set of trait types
19 * A full set of trait types. Most importantly, we don't provide container
20 traitlets (list, dict, tuple) that can trigger notifications if their
21 contents change.
20 * API compatibility with enthought.traits
22 * API compatibility with enthought.traits
21
23
24 There are also some important difference in our design:
25
26 * enthought.traits does not validate default values. We do.
27
22 We choose to create this module because we need these capabilities, but
28 We choose to create this module because we need these capabilities, but
23 we need them to be pure Python so they work in all Python implementations,
29 we need them to be pure Python so they work in all Python implementations,
24 including Jython and IronPython.
30 including Jython and IronPython.
@@ -184,7 +190,7 b' class TraitletType(object):'
184 old_value = self.__get__(obj)
190 old_value = self.__get__(obj)
185 if old_value != new_value:
191 if old_value != new_value:
186 obj._traitlet_values[self.name] = new_value
192 obj._traitlet_values[self.name] = new_value
187 obj._notify(self.name, old_value, new_value)
193 obj._notify_traitlet(self.name, old_value, new_value)
188 else:
194 else:
189 obj._traitlet_values[self.name] = new_value
195 obj._traitlet_values[self.name] = new_value
190
196
@@ -248,7 +254,7 b' class HasTraitlets(object):'
248 self._traitlet_values = {}
254 self._traitlet_values = {}
249 self._traitlet_notifiers = {}
255 self._traitlet_notifiers = {}
250
256
251 def _notify(self, name, old_value, new_value):
257 def _notify_traitlet(self, name, old_value, new_value):
252
258
253 # First dynamic ones
259 # First dynamic ones
254 callables = self._traitlet_notifiers.get(name,[])
260 callables = self._traitlet_notifiers.get(name,[])
@@ -368,6 +374,10 b' class HasTraitlets(object):'
368 inst.name = name
374 inst.name = name
369 setattr(self.__class__, name, inst)
375 setattr(self.__class__, name, inst)
370
376
377 def traitlet_keys(self):
378 """Get a list of all the names of this classes traitlets."""
379 return [memb[0] for memb in inspect.getmembers(self.__class__) if isinstance(memb[1], TraitletType)]
380
371
381
372 #-----------------------------------------------------------------------------
382 #-----------------------------------------------------------------------------
373 # Actual TraitletTypes implementations/subclasses
383 # Actual TraitletTypes implementations/subclasses
General Comments 0
You need to be logged in to leave comments. Login now