##// END OF EJS Templates
A few more small traitlets features.
Brian Granger -
Show More
@@ -46,7 +46,7 b' class HasTraitletsStub(object):'
46 46 self._traitlet_values = {}
47 47 self._traitlet_notifiers = {}
48 48
49 def _notify(self, name, old, new):
49 def _notify_traitlet(self, name, old, new):
50 50 self._notify_name = name
51 51 self._notify_old = old
52 52 self._notify_new = new
@@ -321,6 +321,15 b' class TestAddTraitlet(TestCase):'
321 321 self.assertEquals(a.b,10.0)
322 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 335 # Tests for specific traitlet types
@@ -16,9 +16,15 b" We don't support:"
16 16
17 17 * Delegation
18 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 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 28 We choose to create this module because we need these capabilities, but
23 29 we need them to be pure Python so they work in all Python implementations,
24 30 including Jython and IronPython.
@@ -184,7 +190,7 b' class TraitletType(object):'
184 190 old_value = self.__get__(obj)
185 191 if old_value != new_value:
186 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 194 else:
189 195 obj._traitlet_values[self.name] = new_value
190 196
@@ -248,7 +254,7 b' class HasTraitlets(object):'
248 254 self._traitlet_values = {}
249 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 259 # First dynamic ones
254 260 callables = self._traitlet_notifiers.get(name,[])
@@ -368,6 +374,10 b' class HasTraitlets(object):'
368 374 inst.name = name
369 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 383 # Actual TraitletTypes implementations/subclasses
General Comments 0
You need to be logged in to leave comments. Login now