Merge lp:~ipython-contrib/ipython/traitlets-rename into lp:ipython/0.11
- traitlets-rename
- Merge into trunk
Proposed by
Dav Clark
Status: | Merged |
---|---|
Merged at revision: | not available |
Proposed branch: | lp:~ipython-contrib/ipython/traitlets-rename |
Merge into: | lp:ipython/0.11 |
Prerequisite: | lp:~fdo.perez/ipython/trunk-dev |
Diff against target: |
1673 lines (+326/-315) 7 files modified
IPython/core/application.py (+1/-2) IPython/core/component.py (+10/-10) IPython/core/iplib.py (+4/-4) IPython/core/tests/test_component.py (+2/-2) IPython/utils/tests/test_traitlets.py (+136/-136) IPython/utils/traitlets.py (+172/-160) docs/source/config/overview.txt (+1/-1) |
To merge this branch: | bzr merge lp:~ipython-contrib/ipython/traitlets-rename |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Fernando Perez | Approve | ||
Review via email: mp+17498@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
Dav Clark (davclark) wrote : | # |
- 1223. By Fernando Perez
-
Merging with upstream trunk-dev, which is the base for this branch
Revision history for this message
Fernando Perez (fdo.perez) wrote : | # |
This was already discussed and Brian OK'd it, I'm moving ahead with the merge.
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'IPython/core/application.py' |
2 | --- IPython/core/application.py 2010-01-18 09:16:17 +0000 |
3 | +++ IPython/core/application.py 2010-01-18 09:16:19 +0000 |
4 | @@ -248,7 +248,7 @@ |
5 | |
6 | For the most part, we try to set default in the class attributes |
7 | of Components. But, defaults the top-level Application (which is |
8 | - not a HasTraitlets or Component) are not set in this way. Instead |
9 | + not a HasTraits or Component) are not set in this way. Instead |
10 | we set them here. The Global section is for variables like this that |
11 | don't belong to a particular component. |
12 | """ |
13 | @@ -486,4 +486,3 @@ |
14 | raise |
15 | elif action == 'exit': |
16 | self.exit(0) |
17 | - |
18 | |
19 | === modified file 'IPython/core/component.py' |
20 | --- IPython/core/component.py 2010-01-08 22:28:30 +0000 |
21 | +++ IPython/core/component.py 2010-01-18 09:16:19 +0000 |
22 | @@ -27,7 +27,7 @@ |
23 | from IPython.utils.importstring import import_item |
24 | from IPython.config.loader import Config |
25 | from IPython.utils.traitlets import ( |
26 | - HasTraitlets, TraitletError, MetaHasTraitlets, Instance, This |
27 | + HasTraits, TraitError, MetaHasTraits, Instance, This |
28 | ) |
29 | |
30 | |
31 | @@ -173,7 +173,7 @@ |
32 | ComponentNameGenerator = __ComponentNameGenerator('ipython.component') |
33 | |
34 | |
35 | -class MetaComponent(MetaHasTraitlets, MetaComponentTracker): |
36 | +class MetaComponent(MetaHasTraits, MetaComponentTracker): |
37 | pass |
38 | |
39 | |
40 | @@ -182,11 +182,11 @@ |
41 | #----------------------------------------------------------------------------- |
42 | |
43 | |
44 | -class Component(HasTraitlets): |
45 | +class Component(HasTraits): |
46 | |
47 | __metaclass__ = MetaComponent |
48 | |
49 | - # Traitlets are fun! |
50 | + # Traits are fun! |
51 | config = Instance(Config,(),{}) |
52 | parent = This() |
53 | root = This() |
54 | @@ -256,7 +256,7 @@ |
55 | self.created = datetime.datetime.now() |
56 | |
57 | #------------------------------------------------------------------------- |
58 | - # Static traitlet notifiations |
59 | + # Static trait notifiations |
60 | #------------------------------------------------------------------------- |
61 | |
62 | def _parent_changed(self, name, old, new): |
63 | @@ -282,12 +282,12 @@ |
64 | def _config_changed(self, name, old, new): |
65 | """Update all the class traits having ``config=True`` as metadata. |
66 | |
67 | - For any class traitlet with a ``config`` metadata attribute that is |
68 | - ``True``, we update the traitlet with the value of the corresponding |
69 | + For any class trait with a ``config`` metadata attribute that is |
70 | + ``True``, we update the trait with the value of the corresponding |
71 | config entry. |
72 | """ |
73 | - # Get all traitlets with a config metadata entry that is True |
74 | - traitlets = self.traitlets(config=True) |
75 | + # Get all traits with a config metadata entry that is True |
76 | + traits = self.traits(config=True) |
77 | |
78 | # We auto-load config section for this class as well as any parent |
79 | # classes that are Component subclasses. This starts with Component |
80 | @@ -301,7 +301,7 @@ |
81 | # dynamically create the section with name self.__class__.__name__. |
82 | if new._has_section(sname): |
83 | my_config = new[sname] |
84 | - for k, v in traitlets.items(): |
85 | + for k, v in traits.items(): |
86 | # Don't allow traitlets with config=True to start with |
87 | # uppercase. Otherwise, they are confused with Config |
88 | # subsections. But, developers shouldn't have uppercase |
89 | |
90 | === modified file 'IPython/core/iplib.py' |
91 | --- IPython/core/iplib.py 2010-01-18 09:16:17 +0000 |
92 | +++ IPython/core/iplib.py 2010-01-18 09:16:19 +0000 |
93 | @@ -177,7 +177,7 @@ |
94 | class SeparateStr(Str): |
95 | """A Str subclass to validate separate_in, separate_out, etc. |
96 | |
97 | - This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'. |
98 | + This is a Str based trait that converts '0'->'' and '\\n'->'\n'. |
99 | """ |
100 | |
101 | def validate(self, obj, value): |
102 | @@ -316,7 +316,7 @@ |
103 | |
104 | screen_length = Int(0, config=True) |
105 | |
106 | - # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n' |
107 | + # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
108 | separate_in = SeparateStr('\n', config=True) |
109 | separate_out = SeparateStr('', config=True) |
110 | separate_out2 = SeparateStr('', config=True) |
111 | @@ -339,7 +339,7 @@ |
112 | banner1=None, banner2=None, display_banner=None, |
113 | custom_exceptions=((),None)): |
114 | |
115 | - # This is where traitlets with a config_key argument are updated |
116 | + # This is where traits with a config_key argument are updated |
117 | # from the values on config. |
118 | super(InteractiveShell, self).__init__(parent, config=config) |
119 | |
120 | @@ -394,7 +394,7 @@ |
121 | return self |
122 | |
123 | #------------------------------------------------------------------------- |
124 | - # Traitlet changed handlers |
125 | + # Trait changed handlers |
126 | #------------------------------------------------------------------------- |
127 | |
128 | def _banner1_changed(self): |
129 | |
130 | === modified file 'IPython/core/tests/test_component.py' |
131 | --- IPython/core/tests/test_component.py 2009-09-25 17:57:29 +0000 |
132 | +++ IPython/core/tests/test_component.py 2010-01-18 09:16:19 +0000 |
133 | @@ -24,7 +24,7 @@ |
134 | |
135 | from IPython.core.component import Component, ComponentError |
136 | from IPython.utils.traitlets import ( |
137 | - TraitletError, Int, Float, Str |
138 | + TraitError, Int, Float, Str |
139 | ) |
140 | from IPython.config.loader import Config |
141 | |
142 | @@ -109,7 +109,7 @@ |
143 | |
144 | def test_subclass_parent(self): |
145 | c1 = Component(None) |
146 | - self.assertRaises(TraitletError, setattr, c1, 'parent', 10) |
147 | + self.assertRaises(TraitError, setattr, c1, 'parent', 10) |
148 | |
149 | class MyComponent(Component): |
150 | pass |
151 | |
152 | === modified file 'IPython/utils/tests/test_traitlets.py' |
153 | --- IPython/utils/tests/test_traitlets.py 2009-09-12 15:58:55 +0000 |
154 | +++ IPython/utils/tests/test_traitlets.py 2010-01-18 09:16:19 +0000 |
155 | @@ -29,8 +29,8 @@ |
156 | from unittest import TestCase |
157 | |
158 | from IPython.utils.traitlets import ( |
159 | - HasTraitlets, MetaHasTraitlets, TraitletType, Any, |
160 | - Int, Long, Float, Complex, Str, Unicode, Bool, TraitletError, |
161 | + HasTraits, MetaHasTraits, TraitType, Any, |
162 | + Int, Long, Float, Complex, Str, Unicode, Bool, TraitError, |
163 | Undefined, Type, This, Instance |
164 | ) |
165 | |
166 | @@ -40,9 +40,9 @@ |
167 | #----------------------------------------------------------------------------- |
168 | |
169 | |
170 | -class HasTraitletsStub(HasTraitlets): |
171 | +class HasTraitsStub(HasTraits): |
172 | |
173 | - def _notify_traitlet(self, name, old, new): |
174 | + def _notify_trait(self, name, old, new): |
175 | self._notify_name = name |
176 | self._notify_old = old |
177 | self._notify_new = new |
178 | @@ -53,17 +53,17 @@ |
179 | #----------------------------------------------------------------------------- |
180 | |
181 | |
182 | -class TestTraitletType(TestCase): |
183 | +class TestTraitType(TestCase): |
184 | |
185 | def test_get_undefined(self): |
186 | - class A(HasTraitlets): |
187 | - a = TraitletType |
188 | + class A(HasTraits): |
189 | + a = TraitType |
190 | a = A() |
191 | self.assertEquals(a.a, Undefined) |
192 | |
193 | def test_set(self): |
194 | - class A(HasTraitletsStub): |
195 | - a = TraitletType |
196 | + class A(HasTraitsStub): |
197 | + a = TraitType |
198 | |
199 | a = A() |
200 | a.a = 10 |
201 | @@ -73,10 +73,10 @@ |
202 | self.assertEquals(a._notify_new, 10) |
203 | |
204 | def test_validate(self): |
205 | - class MyTT(TraitletType): |
206 | + class MyTT(TraitType): |
207 | def validate(self, inst, value): |
208 | return -1 |
209 | - class A(HasTraitletsStub): |
210 | + class A(HasTraitsStub): |
211 | tt = MyTT |
212 | |
213 | a = A() |
214 | @@ -84,26 +84,26 @@ |
215 | self.assertEquals(a.tt, -1) |
216 | |
217 | def test_default_validate(self): |
218 | - class MyIntTT(TraitletType): |
219 | + class MyIntTT(TraitType): |
220 | def validate(self, obj, value): |
221 | if isinstance(value, int): |
222 | return value |
223 | self.error(obj, value) |
224 | - class A(HasTraitlets): |
225 | + class A(HasTraits): |
226 | tt = MyIntTT(10) |
227 | a = A() |
228 | self.assertEquals(a.tt, 10) |
229 | |
230 | - # Defaults are validated when the HasTraitlets is instantiated |
231 | - class B(HasTraitlets): |
232 | + # Defaults are validated when the HasTraits is instantiated |
233 | + class B(HasTraits): |
234 | tt = MyIntTT('bad default') |
235 | - self.assertRaises(TraitletError, B) |
236 | + self.assertRaises(TraitError, B) |
237 | |
238 | def test_is_valid_for(self): |
239 | - class MyTT(TraitletType): |
240 | + class MyTT(TraitType): |
241 | def is_valid_for(self, value): |
242 | return True |
243 | - class A(HasTraitlets): |
244 | + class A(HasTraits): |
245 | tt = MyTT |
246 | |
247 | a = A() |
248 | @@ -111,10 +111,10 @@ |
249 | self.assertEquals(a.tt, 10) |
250 | |
251 | def test_value_for(self): |
252 | - class MyTT(TraitletType): |
253 | + class MyTT(TraitType): |
254 | def value_for(self, value): |
255 | return 20 |
256 | - class A(HasTraitlets): |
257 | + class A(HasTraits): |
258 | tt = MyTT |
259 | |
260 | a = A() |
261 | @@ -122,33 +122,33 @@ |
262 | self.assertEquals(a.tt, 20) |
263 | |
264 | def test_info(self): |
265 | - class A(HasTraitlets): |
266 | - tt = TraitletType |
267 | + class A(HasTraits): |
268 | + tt = TraitType |
269 | a = A() |
270 | self.assertEquals(A.tt.info(), 'any value') |
271 | |
272 | def test_error(self): |
273 | - class A(HasTraitlets): |
274 | - tt = TraitletType |
275 | + class A(HasTraits): |
276 | + tt = TraitType |
277 | a = A() |
278 | - self.assertRaises(TraitletError, A.tt.error, a, 10) |
279 | - |
280 | - |
281 | -class TestHasTraitletsMeta(TestCase): |
282 | + self.assertRaises(TraitError, A.tt.error, a, 10) |
283 | + |
284 | + |
285 | +class TestHasTraitsMeta(TestCase): |
286 | |
287 | def test_metaclass(self): |
288 | - self.assertEquals(type(HasTraitlets), MetaHasTraitlets) |
289 | + self.assertEquals(type(HasTraits), MetaHasTraits) |
290 | |
291 | - class A(HasTraitlets): |
292 | + class A(HasTraits): |
293 | a = Int |
294 | |
295 | a = A() |
296 | - self.assertEquals(type(a.__class__), MetaHasTraitlets) |
297 | + self.assertEquals(type(a.__class__), MetaHasTraits) |
298 | self.assertEquals(a.a,0) |
299 | a.a = 10 |
300 | self.assertEquals(a.a,10) |
301 | |
302 | - class B(HasTraitlets): |
303 | + class B(HasTraits): |
304 | b = Int() |
305 | |
306 | b = B() |
307 | @@ -156,7 +156,7 @@ |
308 | b.b = 10 |
309 | self.assertEquals(b.b,10) |
310 | |
311 | - class C(HasTraitlets): |
312 | + class C(HasTraits): |
313 | c = Int(30) |
314 | |
315 | c = C() |
316 | @@ -165,7 +165,7 @@ |
317 | self.assertEquals(c.c,10) |
318 | |
319 | def test_this_class(self): |
320 | - class A(HasTraitlets): |
321 | + class A(HasTraits): |
322 | t = This() |
323 | tt = This() |
324 | class B(A): |
325 | @@ -176,7 +176,7 @@ |
326 | self.assertEquals(B.tt.this_class, B) |
327 | self.assertEquals(B.ttt.this_class, B) |
328 | |
329 | -class TestHasTraitletsNotify(TestCase): |
330 | +class TestHasTraitsNotify(TestCase): |
331 | |
332 | def setUp(self): |
333 | self._notify1 = [] |
334 | @@ -190,12 +190,12 @@ |
335 | |
336 | def test_notify_all(self): |
337 | |
338 | - class A(HasTraitlets): |
339 | + class A(HasTraits): |
340 | a = Int |
341 | b = Float |
342 | |
343 | a = A() |
344 | - a.on_traitlet_change(self.notify1) |
345 | + a.on_trait_change(self.notify1) |
346 | a.a = 0 |
347 | self.assertEquals(len(self._notify1),0) |
348 | a.b = 0.0 |
349 | @@ -204,31 +204,31 @@ |
350 | self.assert_(('a',0,10) in self._notify1) |
351 | a.b = 10.0 |
352 | self.assert_(('b',0.0,10.0) in self._notify1) |
353 | - self.assertRaises(TraitletError,setattr,a,'a','bad string') |
354 | - self.assertRaises(TraitletError,setattr,a,'b','bad string') |
355 | + self.assertRaises(TraitError,setattr,a,'a','bad string') |
356 | + self.assertRaises(TraitError,setattr,a,'b','bad string') |
357 | self._notify1 = [] |
358 | - a.on_traitlet_change(self.notify1,remove=True) |
359 | + a.on_trait_change(self.notify1,remove=True) |
360 | a.a = 20 |
361 | a.b = 20.0 |
362 | self.assertEquals(len(self._notify1),0) |
363 | |
364 | def test_notify_one(self): |
365 | |
366 | - class A(HasTraitlets): |
367 | + class A(HasTraits): |
368 | a = Int |
369 | b = Float |
370 | |
371 | a = A() |
372 | - a.on_traitlet_change(self.notify1, 'a') |
373 | + a.on_trait_change(self.notify1, 'a') |
374 | a.a = 0 |
375 | self.assertEquals(len(self._notify1),0) |
376 | a.a = 10 |
377 | self.assert_(('a',0,10) in self._notify1) |
378 | - self.assertRaises(TraitletError,setattr,a,'a','bad string') |
379 | + self.assertRaises(TraitError,setattr,a,'a','bad string') |
380 | |
381 | def test_subclass(self): |
382 | |
383 | - class A(HasTraitlets): |
384 | + class A(HasTraits): |
385 | a = Int |
386 | |
387 | class B(A): |
388 | @@ -244,15 +244,15 @@ |
389 | |
390 | def test_notify_subclass(self): |
391 | |
392 | - class A(HasTraitlets): |
393 | + class A(HasTraits): |
394 | a = Int |
395 | |
396 | class B(A): |
397 | b = Float |
398 | |
399 | b = B() |
400 | - b.on_traitlet_change(self.notify1, 'a') |
401 | - b.on_traitlet_change(self.notify2, 'b') |
402 | + b.on_trait_change(self.notify1, 'a') |
403 | + b.on_trait_change(self.notify2, 'b') |
404 | b.a = 0 |
405 | b.b = 0.0 |
406 | self.assertEquals(len(self._notify1),0) |
407 | @@ -264,7 +264,7 @@ |
408 | |
409 | def test_static_notify(self): |
410 | |
411 | - class A(HasTraitlets): |
412 | + class A(HasTraits): |
413 | a = Int |
414 | _notify1 = [] |
415 | def _a_changed(self, name, old, new): |
416 | @@ -300,73 +300,73 @@ |
417 | def callback3(name, old, new): |
418 | self.cb = (name, old, new) |
419 | |
420 | - class A(HasTraitlets): |
421 | + class A(HasTraits): |
422 | a = Int |
423 | |
424 | a = A() |
425 | - a.on_traitlet_change(callback0, 'a') |
426 | + a.on_trait_change(callback0, 'a') |
427 | a.a = 10 |
428 | self.assertEquals(self.cb,()) |
429 | - a.on_traitlet_change(callback0, 'a', remove=True) |
430 | + a.on_trait_change(callback0, 'a', remove=True) |
431 | |
432 | - a.on_traitlet_change(callback1, 'a') |
433 | + a.on_trait_change(callback1, 'a') |
434 | a.a = 100 |
435 | self.assertEquals(self.cb,('a',)) |
436 | - a.on_traitlet_change(callback1, 'a', remove=True) |
437 | + a.on_trait_change(callback1, 'a', remove=True) |
438 | |
439 | - a.on_traitlet_change(callback2, 'a') |
440 | + a.on_trait_change(callback2, 'a') |
441 | a.a = 1000 |
442 | self.assertEquals(self.cb,('a',1000)) |
443 | - a.on_traitlet_change(callback2, 'a', remove=True) |
444 | + a.on_trait_change(callback2, 'a', remove=True) |
445 | |
446 | - a.on_traitlet_change(callback3, 'a') |
447 | + a.on_trait_change(callback3, 'a') |
448 | a.a = 10000 |
449 | self.assertEquals(self.cb,('a',1000,10000)) |
450 | - a.on_traitlet_change(callback3, 'a', remove=True) |
451 | - |
452 | - self.assertEquals(len(a._traitlet_notifiers['a']),0) |
453 | - |
454 | - |
455 | -class TestHasTraitlets(TestCase): |
456 | - |
457 | - def test_traitlet_names(self): |
458 | - class A(HasTraitlets): |
459 | + a.on_trait_change(callback3, 'a', remove=True) |
460 | + |
461 | + self.assertEquals(len(a._trait_notifiers['a']),0) |
462 | + |
463 | + |
464 | +class TestHasTraits(TestCase): |
465 | + |
466 | + def test_trait_names(self): |
467 | + class A(HasTraits): |
468 | i = Int |
469 | f = Float |
470 | a = A() |
471 | - self.assertEquals(a.traitlet_names(),['i','f']) |
472 | + self.assertEquals(a.trait_names(),['i','f']) |
473 | |
474 | - def test_traitlet_metadata(self): |
475 | - class A(HasTraitlets): |
476 | + def test_trait_metadata(self): |
477 | + class A(HasTraits): |
478 | i = Int(config_key='MY_VALUE') |
479 | a = A() |
480 | - self.assertEquals(a.traitlet_metadata('i','config_key'), 'MY_VALUE') |
481 | + self.assertEquals(a.trait_metadata('i','config_key'), 'MY_VALUE') |
482 | |
483 | - def test_traitlets(self): |
484 | - class A(HasTraitlets): |
485 | + def test_traits(self): |
486 | + class A(HasTraits): |
487 | i = Int |
488 | f = Float |
489 | a = A() |
490 | - self.assertEquals(a.traitlets(), dict(i=A.i, f=A.f)) |
491 | + self.assertEquals(a.traits(), dict(i=A.i, f=A.f)) |
492 | |
493 | - def test_traitlets_metadata(self): |
494 | - class A(HasTraitlets): |
495 | + def test_traits_metadata(self): |
496 | + class A(HasTraits): |
497 | i = Int(config_key='VALUE1', other_thing='VALUE2') |
498 | f = Float(config_key='VALUE3', other_thing='VALUE2') |
499 | j = Int(0) |
500 | a = A() |
501 | - self.assertEquals(a.traitlets(), dict(i=A.i, f=A.f, j=A.j)) |
502 | - traitlets = a.traitlets(config_key='VALUE1', other_thing='VALUE2') |
503 | - self.assertEquals(traitlets, dict(i=A.i)) |
504 | + self.assertEquals(a.traits(), dict(i=A.i, f=A.f, j=A.j)) |
505 | + traits = a.traits(config_key='VALUE1', other_thing='VALUE2') |
506 | + self.assertEquals(traits, dict(i=A.i)) |
507 | |
508 | # This passes, but it shouldn't because I am replicating a bug in |
509 | # traits. |
510 | - traitlets = a.traitlets(config_key=lambda v: True) |
511 | - self.assertEquals(traitlets, dict(i=A.i, f=A.f, j=A.j)) |
512 | + traits = a.traits(config_key=lambda v: True) |
513 | + self.assertEquals(traits, dict(i=A.i, f=A.f, j=A.j)) |
514 | |
515 | |
516 | #----------------------------------------------------------------------------- |
517 | -# Tests for specific traitlet types |
518 | +# Tests for specific trait types |
519 | #----------------------------------------------------------------------------- |
520 | |
521 | |
522 | @@ -375,7 +375,7 @@ |
523 | def test_default(self): |
524 | |
525 | class B(object): pass |
526 | - class A(HasTraitlets): |
527 | + class A(HasTraits): |
528 | klass = Type |
529 | |
530 | a = A() |
531 | @@ -383,42 +383,42 @@ |
532 | |
533 | a.klass = B |
534 | self.assertEquals(a.klass, B) |
535 | - self.assertRaises(TraitletError, setattr, a, 'klass', 10) |
536 | + self.assertRaises(TraitError, setattr, a, 'klass', 10) |
537 | |
538 | def test_value(self): |
539 | |
540 | class B(object): pass |
541 | class C(object): pass |
542 | - class A(HasTraitlets): |
543 | + class A(HasTraits): |
544 | klass = Type(B) |
545 | |
546 | a = A() |
547 | self.assertEquals(a.klass, B) |
548 | - self.assertRaises(TraitletError, setattr, a, 'klass', C) |
549 | - self.assertRaises(TraitletError, setattr, a, 'klass', object) |
550 | + self.assertRaises(TraitError, setattr, a, 'klass', C) |
551 | + self.assertRaises(TraitError, setattr, a, 'klass', object) |
552 | a.klass = B |
553 | |
554 | def test_allow_none(self): |
555 | |
556 | class B(object): pass |
557 | class C(B): pass |
558 | - class A(HasTraitlets): |
559 | + class A(HasTraits): |
560 | klass = Type(B, allow_none=False) |
561 | |
562 | a = A() |
563 | self.assertEquals(a.klass, B) |
564 | - self.assertRaises(TraitletError, setattr, a, 'klass', None) |
565 | + self.assertRaises(TraitError, setattr, a, 'klass', None) |
566 | a.klass = C |
567 | self.assertEquals(a.klass, C) |
568 | |
569 | def test_validate_klass(self): |
570 | |
571 | - class A(HasTraitlets): |
572 | + class A(HasTraits): |
573 | klass = Type('no strings allowed') |
574 | |
575 | self.assertRaises(ImportError, A) |
576 | |
577 | - class A(HasTraitlets): |
578 | + class A(HasTraits): |
579 | klass = Type('rub.adub.Duck') |
580 | |
581 | self.assertRaises(ImportError, A) |
582 | @@ -426,19 +426,19 @@ |
583 | def test_validate_default(self): |
584 | |
585 | class B(object): pass |
586 | - class A(HasTraitlets): |
587 | + class A(HasTraits): |
588 | klass = Type('bad default', B) |
589 | |
590 | self.assertRaises(ImportError, A) |
591 | |
592 | - class C(HasTraitlets): |
593 | + class C(HasTraits): |
594 | klass = Type(None, B, allow_none=False) |
595 | |
596 | - self.assertRaises(TraitletError, C) |
597 | + self.assertRaises(TraitError, C) |
598 | |
599 | def test_str_klass(self): |
600 | |
601 | - class A(HasTraitlets): |
602 | + class A(HasTraits): |
603 | klass = Type('IPython.utils.ipstruct.Struct') |
604 | |
605 | from IPython.utils.ipstruct import Struct |
606 | @@ -446,7 +446,7 @@ |
607 | a.klass = Struct |
608 | self.assertEquals(a.klass, Struct) |
609 | |
610 | - self.assertRaises(TraitletError, setattr, a, 'klass', 10) |
611 | + self.assertRaises(TraitError, setattr, a, 'klass', 10) |
612 | |
613 | class TestInstance(TestCase): |
614 | |
615 | @@ -455,7 +455,7 @@ |
616 | class Bar(Foo): pass |
617 | class Bah(object): pass |
618 | |
619 | - class A(HasTraitlets): |
620 | + class A(HasTraits): |
621 | inst = Instance(Foo) |
622 | |
623 | a = A() |
624 | @@ -464,13 +464,13 @@ |
625 | self.assert_(isinstance(a.inst, Foo)) |
626 | a.inst = Bar() |
627 | self.assert_(isinstance(a.inst, Foo)) |
628 | - self.assertRaises(TraitletError, setattr, a, 'inst', Foo) |
629 | - self.assertRaises(TraitletError, setattr, a, 'inst', Bar) |
630 | - self.assertRaises(TraitletError, setattr, a, 'inst', Bah()) |
631 | + self.assertRaises(TraitError, setattr, a, 'inst', Foo) |
632 | + self.assertRaises(TraitError, setattr, a, 'inst', Bar) |
633 | + self.assertRaises(TraitError, setattr, a, 'inst', Bah()) |
634 | |
635 | def test_unique_default_value(self): |
636 | class Foo(object): pass |
637 | - class A(HasTraitlets): |
638 | + class A(HasTraits): |
639 | inst = Instance(Foo,(),{}) |
640 | |
641 | a = A() |
642 | @@ -485,18 +485,18 @@ |
643 | def __init__(self, c, d): |
644 | self.c = c; self.d = d |
645 | |
646 | - class A(HasTraitlets): |
647 | + class A(HasTraits): |
648 | inst = Instance(Foo, (10,)) |
649 | a = A() |
650 | self.assertEquals(a.inst.c, 10) |
651 | |
652 | - class B(HasTraitlets): |
653 | + class B(HasTraits): |
654 | inst = Instance(Bah, args=(10,), kw=dict(d=20)) |
655 | b = B() |
656 | self.assertEquals(b.inst.c, 10) |
657 | self.assertEquals(b.inst.d, 20) |
658 | |
659 | - class C(HasTraitlets): |
660 | + class C(HasTraits): |
661 | inst = Instance(Foo) |
662 | c = C() |
663 | self.assert_(c.inst is None) |
664 | @@ -504,25 +504,25 @@ |
665 | def test_bad_default(self): |
666 | class Foo(object): pass |
667 | |
668 | - class A(HasTraitlets): |
669 | + class A(HasTraits): |
670 | inst = Instance(Foo, allow_none=False) |
671 | |
672 | - self.assertRaises(TraitletError, A) |
673 | + self.assertRaises(TraitError, A) |
674 | |
675 | def test_instance(self): |
676 | class Foo(object): pass |
677 | |
678 | def inner(): |
679 | - class A(HasTraitlets): |
680 | + class A(HasTraits): |
681 | inst = Instance(Foo()) |
682 | |
683 | - self.assertRaises(TraitletError, inner) |
684 | + self.assertRaises(TraitError, inner) |
685 | |
686 | |
687 | class TestThis(TestCase): |
688 | |
689 | def test_this_class(self): |
690 | - class Foo(HasTraitlets): |
691 | + class Foo(HasTraits): |
692 | this = This |
693 | |
694 | f = Foo() |
695 | @@ -530,10 +530,10 @@ |
696 | g = Foo() |
697 | f.this = g |
698 | self.assertEquals(f.this, g) |
699 | - self.assertRaises(TraitletError, setattr, f, 'this', 10) |
700 | + self.assertRaises(TraitError, setattr, f, 'this', 10) |
701 | |
702 | def test_this_inst(self): |
703 | - class Foo(HasTraitlets): |
704 | + class Foo(HasTraits): |
705 | this = This() |
706 | |
707 | f = Foo() |
708 | @@ -541,7 +541,7 @@ |
709 | self.assert_(isinstance(f.this, Foo)) |
710 | |
711 | def test_subclass(self): |
712 | - class Foo(HasTraitlets): |
713 | + class Foo(HasTraits): |
714 | t = This() |
715 | class Bar(Foo): |
716 | pass |
717 | @@ -553,7 +553,7 @@ |
718 | self.assertEquals(b.t, f) |
719 | |
720 | def test_subclass_override(self): |
721 | - class Foo(HasTraitlets): |
722 | + class Foo(HasTraits): |
723 | t = This() |
724 | class Bar(Foo): |
725 | t = This() |
726 | @@ -561,10 +561,10 @@ |
727 | b = Bar() |
728 | f.t = b |
729 | self.assertEquals(f.t, b) |
730 | - self.assertRaises(TraitletError, setattr, b, 't', f) |
731 | + self.assertRaises(TraitError, setattr, b, 't', f) |
732 | |
733 | -class TraitletTestBase(TestCase): |
734 | - """A best testing class for basic traitlet types.""" |
735 | +class TraitTestBase(TestCase): |
736 | + """A best testing class for basic trait types.""" |
737 | |
738 | def assign(self, value): |
739 | self.obj.value = value |
740 | @@ -581,33 +581,33 @@ |
741 | def test_bad_values(self): |
742 | if hasattr(self, '_bad_values'): |
743 | for value in self._bad_values: |
744 | - self.assertRaises(TraitletError, self.assign, value) |
745 | + self.assertRaises(TraitError, self.assign, value) |
746 | |
747 | def test_default_value(self): |
748 | if hasattr(self, '_default_value'): |
749 | self.assertEquals(self._default_value, self.obj.value) |
750 | |
751 | |
752 | -class AnyTraitlet(HasTraitlets): |
753 | +class AnyTrait(HasTraits): |
754 | |
755 | value = Any |
756 | |
757 | -class AnyTraitTest(TraitletTestBase): |
758 | +class AnyTraitTest(TraitTestBase): |
759 | |
760 | - obj = AnyTraitlet() |
761 | + obj = AnyTrait() |
762 | |
763 | _default_value = None |
764 | _good_values = [10.0, 'ten', u'ten', [10], {'ten': 10},(10,), None, 1j] |
765 | _bad_values = [] |
766 | |
767 | |
768 | -class IntTraitlet(HasTraitlets): |
769 | +class IntTrait(HasTraits): |
770 | |
771 | value = Int(99) |
772 | |
773 | -class TestInt(TraitletTestBase): |
774 | +class TestInt(TraitTestBase): |
775 | |
776 | - obj = IntTraitlet() |
777 | + obj = IntTrait() |
778 | _default_value = 99 |
779 | _good_values = [10, -10] |
780 | _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, 1j, 10L, |
781 | @@ -615,13 +615,13 @@ |
782 | u'-10L', u'10.1', u'-10.1', '10', '-10', u'10', u'-10'] |
783 | |
784 | |
785 | -class LongTraitlet(HasTraitlets): |
786 | +class LongTrait(HasTraits): |
787 | |
788 | value = Long(99L) |
789 | |
790 | -class TestLong(TraitletTestBase): |
791 | +class TestLong(TraitTestBase): |
792 | |
793 | - obj = LongTraitlet() |
794 | + obj = LongTrait() |
795 | |
796 | _default_value = 99L |
797 | _good_values = [10, -10, 10L, -10L] |
798 | @@ -631,13 +631,13 @@ |
799 | u'-10.1'] |
800 | |
801 | |
802 | -class FloatTraitlet(HasTraitlets): |
803 | +class FloatTrait(HasTraits): |
804 | |
805 | value = Float(99.0) |
806 | |
807 | -class TestFloat(TraitletTestBase): |
808 | +class TestFloat(TraitTestBase): |
809 | |
810 | - obj = FloatTraitlet() |
811 | + obj = FloatTrait() |
812 | |
813 | _default_value = 99.0 |
814 | _good_values = [10, -10, 10.1, -10.1] |
815 | @@ -646,13 +646,13 @@ |
816 | u'-10', u'10L', u'-10L', u'10.1', u'-10.1'] |
817 | |
818 | |
819 | -class ComplexTraitlet(HasTraitlets): |
820 | +class ComplexTrait(HasTraits): |
821 | |
822 | value = Complex(99.0-99.0j) |
823 | |
824 | -class TestComplex(TraitletTestBase): |
825 | +class TestComplex(TraitTestBase): |
826 | |
827 | - obj = ComplexTraitlet() |
828 | + obj = ComplexTrait() |
829 | |
830 | _default_value = 99.0-99.0j |
831 | _good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j, |
832 | @@ -660,13 +660,13 @@ |
833 | _bad_values = [10L, -10L, u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None] |
834 | |
835 | |
836 | -class StringTraitlet(HasTraitlets): |
837 | +class StringTrait(HasTraits): |
838 | |
839 | value = Str('string') |
840 | |
841 | -class TestString(TraitletTestBase): |
842 | +class TestString(TraitTestBase): |
843 | |
844 | - obj = StringTraitlet() |
845 | + obj = StringTrait() |
846 | |
847 | _default_value = 'string' |
848 | _good_values = ['10', '-10', '10L', |
849 | @@ -675,13 +675,13 @@ |
850 | ['ten'],{'ten': 10},(10,), None, u'string'] |
851 | |
852 | |
853 | -class UnicodeTraitlet(HasTraitlets): |
854 | +class UnicodeTrait(HasTraits): |
855 | |
856 | value = Unicode(u'unicode') |
857 | |
858 | -class TestUnicode(TraitletTestBase): |
859 | +class TestUnicode(TraitTestBase): |
860 | |
861 | - obj = UnicodeTraitlet() |
862 | + obj = UnicodeTrait() |
863 | |
864 | _default_value = u'unicode' |
865 | _good_values = ['10', '-10', '10L', '-10L', '10.1', |
866 | |
867 | === modified file 'IPython/utils/traitlets.py' |
868 | --- IPython/utils/traitlets.py 2009-10-17 17:38:44 +0000 |
869 | +++ IPython/utils/traitlets.py 2010-01-18 09:16:18 +0000 |
870 | @@ -17,7 +17,7 @@ |
871 | * Delegation |
872 | * Automatic GUI generation |
873 | * A full set of trait types. Most importantly, we don't provide container |
874 | - traitlets (list, dict, tuple) that can trigger notifications if their |
875 | + traits (list, dict, tuple) that can trigger notifications if their |
876 | contents change. |
877 | * API compatibility with enthought.traits |
878 | |
879 | @@ -57,7 +57,18 @@ |
880 | ListType, TupleType |
881 | ) |
882 | |
883 | -from IPython.utils.importstring import import_item |
884 | +def import_item(name): |
885 | + """Import and return bar given the string foo.bar.""" |
886 | + package = '.'.join(name.split('.')[0:-1]) |
887 | + obj = name.split('.')[-1] |
888 | + execString = 'from %s import %s' % (package, obj) |
889 | + try: |
890 | + exec execString |
891 | + except SyntaxError: |
892 | + raise ImportError("Invalid class specification: %s" % name) |
893 | + exec 'temp = %s' % obj |
894 | + return temp |
895 | + |
896 | |
897 | ClassTypes = (ClassType, type) |
898 | |
899 | @@ -75,11 +86,9 @@ |
900 | class Undefined ( object ): pass |
901 | Undefined = Undefined() |
902 | |
903 | - |
904 | -class TraitletError(Exception): |
905 | +class TraitError(Exception): |
906 | pass |
907 | |
908 | - |
909 | #----------------------------------------------------------------------------- |
910 | # Utilities |
911 | #----------------------------------------------------------------------------- |
912 | @@ -129,12 +138,12 @@ |
913 | >>> parse_notifier_name(['a','b']) |
914 | ['a', 'b'] |
915 | >>> parse_notifier_name(None) |
916 | - ['anytraitlet'] |
917 | + ['anytrait'] |
918 | """ |
919 | if isinstance(name, str): |
920 | return [name] |
921 | elif name is None: |
922 | - return ['anytraitlet'] |
923 | + return ['anytrait'] |
924 | elif isinstance(name, (list, tuple)): |
925 | for n in name: |
926 | assert isinstance(n, str), "names must be strings" |
927 | @@ -172,25 +181,25 @@ |
928 | |
929 | |
930 | #----------------------------------------------------------------------------- |
931 | -# Base TraitletType for all traitlets |
932 | +# Base TraitType for all traits |
933 | #----------------------------------------------------------------------------- |
934 | |
935 | |
936 | -class TraitletType(object): |
937 | - """A base class for all traitlet descriptors. |
938 | +class TraitType(object): |
939 | + """A base class for all trait descriptors. |
940 | |
941 | Notes |
942 | ----- |
943 | - Our implementation of traitlets is based on Python's descriptor |
944 | + Our implementation of traits is based on Python's descriptor |
945 | prototol. This class is the base class for all such descriptors. The |
946 | - only magic we use is a custom metaclass for the main :class:`HasTraitlets` |
947 | + only magic we use is a custom metaclass for the main :class:`HasTraits` |
948 | class that does the following: |
949 | |
950 | - 1. Sets the :attr:`name` attribute of every :class:`TraitletType` |
951 | + 1. Sets the :attr:`name` attribute of every :class:`TraitType` |
952 | instance in the class dict to the name of the attribute. |
953 | - 2. Sets the :attr:`this_class` attribute of every :class:`TraitletType` |
954 | - instance in the class dict to the *class* that declared the traitlet. |
955 | - This is used by the :class:`This` traitlet to allow subclasses to |
956 | + 2. Sets the :attr:`this_class` attribute of every :class:`TraitType` |
957 | + instance in the class dict to the *class* that declared the trait. |
958 | + This is used by the :class:`This` trait to allow subclasses to |
959 | accept superclasses for :class:`This` values. |
960 | """ |
961 | |
962 | @@ -200,7 +209,7 @@ |
963 | info_text = 'any value' |
964 | |
965 | def __init__(self, default_value=NoDefaultSpecified, **metadata): |
966 | - """Create a TraitletType. |
967 | + """Create a TraitType. |
968 | """ |
969 | if default_value is not NoDefaultSpecified: |
970 | self.default_value = default_value |
971 | @@ -225,11 +234,11 @@ |
972 | return dv |
973 | |
974 | def instance_init(self, obj): |
975 | - """This is called by :meth:`HasTraitlets.__new__` to finish init'ing. |
976 | + """This is called by :meth:`HasTraits.__new__` to finish init'ing. |
977 | |
978 | Some stages of initialization must be delayed until the parent |
979 | - :class:`HasTraitlets` instance has been created. This method is |
980 | - called in :meth:`HasTraitlets.__new__` after the instance has been |
981 | + :class:`HasTraits` instance has been created. This method is |
982 | + called in :meth:`HasTraits.__new__` after the instance has been |
983 | created. |
984 | |
985 | This method trigger the creation and validation of default values |
986 | @@ -238,8 +247,8 @@ |
987 | |
988 | Parameters |
989 | ---------- |
990 | - obj : :class:`HasTraitlets` instance |
991 | - The parent :class:`HasTraitlets` instance that has just been |
992 | + obj : :class:`HasTraits` instance |
993 | + The parent :class:`HasTraits` instance that has just been |
994 | created. |
995 | """ |
996 | self.set_default_value(obj) |
997 | @@ -249,30 +258,30 @@ |
998 | |
999 | This method is called by :meth:`instance_init` to create and |
1000 | validate the default value. The creation and validation of |
1001 | - default values must be delayed until the parent :class:`HasTraitlets` |
1002 | + default values must be delayed until the parent :class:`HasTraits` |
1003 | class has been instantiated. |
1004 | """ |
1005 | dv = self.get_default_value() |
1006 | newdv = self._validate(obj, dv) |
1007 | - obj._traitlet_values[self.name] = newdv |
1008 | + obj._trait_values[self.name] = newdv |
1009 | |
1010 | def __get__(self, obj, cls=None): |
1011 | - """Get the value of the traitlet by self.name for the instance. |
1012 | + """Get the value of the trait by self.name for the instance. |
1013 | |
1014 | - Default values are instantiated when :meth:`HasTraitlets.__new__` |
1015 | + Default values are instantiated when :meth:`HasTraits.__new__` |
1016 | is called. Thus by the time this method gets called either the |
1017 | default value or a user defined value (they called :meth:`__set__`) |
1018 | - is in the :class:`HasTraitlets` instance. |
1019 | + is in the :class:`HasTraits` instance. |
1020 | """ |
1021 | if obj is None: |
1022 | return self |
1023 | else: |
1024 | try: |
1025 | - value = obj._traitlet_values[self.name] |
1026 | + value = obj._trait_values[self.name] |
1027 | except: |
1028 | - # HasTraitlets should call set_default_value to populate |
1029 | + # HasTraits should call set_default_value to populate |
1030 | # this. So this should never be reached. |
1031 | - raise TraitletError('Unexpected error in TraitletType: ' |
1032 | + raise TraitError('Unexpected error in TraitType: ' |
1033 | 'default value not set properly') |
1034 | else: |
1035 | return value |
1036 | @@ -281,8 +290,8 @@ |
1037 | new_value = self._validate(obj, value) |
1038 | old_value = self.__get__(obj) |
1039 | if old_value != new_value: |
1040 | - obj._traitlet_values[self.name] = new_value |
1041 | - obj._notify_traitlet(self.name, old_value, new_value) |
1042 | + obj._trait_values[self.name] = new_value |
1043 | + obj._notify_trait(self.name, old_value, new_value) |
1044 | |
1045 | def _validate(self, obj, value): |
1046 | if hasattr(self, 'validate'): |
1047 | @@ -292,7 +301,7 @@ |
1048 | if valid: |
1049 | return value |
1050 | else: |
1051 | - raise TraitletError('invalid value for type: %r' % value) |
1052 | + raise TraitError('invalid value for type: %r' % value) |
1053 | elif hasattr(self, 'value_for'): |
1054 | return self.value_for(value) |
1055 | else: |
1056 | @@ -303,13 +312,13 @@ |
1057 | |
1058 | def error(self, obj, value): |
1059 | if obj is not None: |
1060 | - e = "The '%s' traitlet of %s instance must be %s, but a value of %s was specified." \ |
1061 | + e = "The '%s' trait of %s instance must be %s, but a value of %s was specified." \ |
1062 | % (self.name, class_of(obj), |
1063 | self.info(), repr_type(value)) |
1064 | else: |
1065 | - e = "The '%s' traitlet must be %s, but a value of %r was specified." \ |
1066 | + e = "The '%s' trait must be %s, but a value of %r was specified." \ |
1067 | % (self.name, self.info(), repr_type(value)) |
1068 | - raise TraitletError(e) |
1069 | + raise TraitError(e) |
1070 | |
1071 | def get_metadata(self, key): |
1072 | return getattr(self, '_metadata', {}).get(key, None) |
1073 | @@ -319,62 +328,62 @@ |
1074 | |
1075 | |
1076 | #----------------------------------------------------------------------------- |
1077 | -# The HasTraitlets implementation |
1078 | +# The HasTraits implementation |
1079 | #----------------------------------------------------------------------------- |
1080 | |
1081 | |
1082 | -class MetaHasTraitlets(type): |
1083 | - """A metaclass for HasTraitlets. |
1084 | +class MetaHasTraits(type): |
1085 | + """A metaclass for HasTraits. |
1086 | |
1087 | - This metaclass makes sure that any TraitletType class attributes are |
1088 | + This metaclass makes sure that any TraitType class attributes are |
1089 | instantiated and sets their name attribute. |
1090 | """ |
1091 | |
1092 | def __new__(mcls, name, bases, classdict): |
1093 | - """Create the HasTraitlets class. |
1094 | + """Create the HasTraits class. |
1095 | |
1096 | - This instantiates all TraitletTypes in the class dict and sets their |
1097 | + This instantiates all TraitTypes in the class dict and sets their |
1098 | :attr:`name` attribute. |
1099 | """ |
1100 | # print "MetaHasTraitlets (mcls, name): ", mcls, name |
1101 | # print "MetaHasTraitlets (bases): ", bases |
1102 | # print "MetaHasTraitlets (classdict): ", classdict |
1103 | for k,v in classdict.iteritems(): |
1104 | - if isinstance(v, TraitletType): |
1105 | + if isinstance(v, TraitType): |
1106 | v.name = k |
1107 | elif inspect.isclass(v): |
1108 | - if issubclass(v, TraitletType): |
1109 | + if issubclass(v, TraitType): |
1110 | vinst = v() |
1111 | vinst.name = k |
1112 | classdict[k] = vinst |
1113 | - return super(MetaHasTraitlets, mcls).__new__(mcls, name, bases, classdict) |
1114 | + return super(MetaHasTraits, mcls).__new__(mcls, name, bases, classdict) |
1115 | |
1116 | def __init__(cls, name, bases, classdict): |
1117 | - """Finish initializing the HasTraitlets class. |
1118 | + """Finish initializing the HasTraits class. |
1119 | |
1120 | - This sets the :attr:`this_class` attribute of each TraitletType in the |
1121 | + This sets the :attr:`this_class` attribute of each TraitType in the |
1122 | class dict to the newly created class ``cls``. |
1123 | """ |
1124 | for k, v in classdict.iteritems(): |
1125 | - if isinstance(v, TraitletType): |
1126 | + if isinstance(v, TraitType): |
1127 | v.this_class = cls |
1128 | - super(MetaHasTraitlets, cls).__init__(name, bases, classdict) |
1129 | - |
1130 | -class HasTraitlets(object): |
1131 | - |
1132 | - __metaclass__ = MetaHasTraitlets |
1133 | + super(MetaHasTraits, cls).__init__(name, bases, classdict) |
1134 | + |
1135 | +class HasTraits(object): |
1136 | + |
1137 | + __metaclass__ = MetaHasTraits |
1138 | |
1139 | def __new__(cls, *args, **kw): |
1140 | # This is needed because in Python 2.6 object.__new__ only accepts |
1141 | # the cls argument. |
1142 | - new_meth = super(HasTraitlets, cls).__new__ |
1143 | + new_meth = super(HasTraits, cls).__new__ |
1144 | if new_meth is object.__new__: |
1145 | inst = new_meth(cls) |
1146 | else: |
1147 | inst = new_meth(cls, *args, **kw) |
1148 | - inst._traitlet_values = {} |
1149 | - inst._traitlet_notifiers = {} |
1150 | - # Here we tell all the TraitletType instances to set their default |
1151 | + inst._trait_values = {} |
1152 | + inst._trait_notifiers = {} |
1153 | + # Here we tell all the TraitType instances to set their default |
1154 | # values on the instance. |
1155 | for key in dir(cls): |
1156 | # Some descriptors raise AttributeError like zope.interface's |
1157 | @@ -385,19 +394,20 @@ |
1158 | except AttributeError: |
1159 | pass |
1160 | else: |
1161 | - if isinstance(value, TraitletType): |
1162 | + if isinstance(value, TraitType): |
1163 | value.instance_init(inst) |
1164 | + |
1165 | return inst |
1166 | |
1167 | # def __init__(self): |
1168 | - # self._traitlet_values = {} |
1169 | - # self._traitlet_notifiers = {} |
1170 | + # self._trait_values = {} |
1171 | + # self._trait_notifiers = {} |
1172 | |
1173 | - def _notify_traitlet(self, name, old_value, new_value): |
1174 | + def _notify_trait(self, name, old_value, new_value): |
1175 | |
1176 | # First dynamic ones |
1177 | - callables = self._traitlet_notifiers.get(name,[]) |
1178 | - more_callables = self._traitlet_notifiers.get('anytraitlet',[]) |
1179 | + callables = self._trait_notifiers.get(name,[]) |
1180 | + more_callables = self._trait_notifiers.get('anytrait',[]) |
1181 | callables.extend(more_callables) |
1182 | |
1183 | # Now static ones |
1184 | @@ -430,25 +440,25 @@ |
1185 | elif nargs + offset == 3: |
1186 | c(name, old_value, new_value) |
1187 | else: |
1188 | - raise TraitletError('a traitlet changed callback ' |
1189 | + raise TraitError('a trait changed callback ' |
1190 | 'must have 0-3 arguments.') |
1191 | else: |
1192 | - raise TraitletError('a traitlet changed callback ' |
1193 | + raise TraitError('a trait changed callback ' |
1194 | 'must be callable.') |
1195 | |
1196 | |
1197 | def _add_notifiers(self, handler, name): |
1198 | - if not self._traitlet_notifiers.has_key(name): |
1199 | + if not self._trait_notifiers.has_key(name): |
1200 | nlist = [] |
1201 | - self._traitlet_notifiers[name] = nlist |
1202 | + self._trait_notifiers[name] = nlist |
1203 | else: |
1204 | - nlist = self._traitlet_notifiers[name] |
1205 | + nlist = self._trait_notifiers[name] |
1206 | if handler not in nlist: |
1207 | nlist.append(handler) |
1208 | |
1209 | def _remove_notifiers(self, handler, name): |
1210 | - if self._traitlet_notifiers.has_key(name): |
1211 | - nlist = self._traitlet_notifiers[name] |
1212 | + if self._trait_notifiers.has_key(name): |
1213 | + nlist = self._trait_notifiers[name] |
1214 | try: |
1215 | index = nlist.index(handler) |
1216 | except ValueError: |
1217 | @@ -456,25 +466,25 @@ |
1218 | else: |
1219 | del nlist[index] |
1220 | |
1221 | - def on_traitlet_change(self, handler, name=None, remove=False): |
1222 | - """Setup a handler to be called when a traitlet changes. |
1223 | + def on_trait_change(self, handler, name=None, remove=False): |
1224 | + """Setup a handler to be called when a trait changes. |
1225 | |
1226 | - This is used to setup dynamic notifications of traitlet changes. |
1227 | + This is used to setup dynamic notifications of trait changes. |
1228 | |
1229 | - Static handlers can be created by creating methods on a HasTraitlets |
1230 | - subclass with the naming convention '_[traitletname]_changed'. Thus, |
1231 | - to create static handler for the traitlet 'a', create the method |
1232 | + Static handlers can be created by creating methods on a HasTraits |
1233 | + subclass with the naming convention '_[traitname]_changed'. Thus, |
1234 | + to create static handler for the trait 'a', create the method |
1235 | _a_changed(self, name, old, new) (fewer arguments can be used, see |
1236 | below). |
1237 | |
1238 | Parameters |
1239 | ---------- |
1240 | handler : callable |
1241 | - A callable that is called when a traitlet changes. Its |
1242 | + A callable that is called when a trait changes. Its |
1243 | signature can be handler(), handler(name), handler(name, new) |
1244 | or handler(name, old, new). |
1245 | name : list, str, None |
1246 | - If None, the handler will apply to all traitlets. If a list |
1247 | + If None, the handler will apply to all traits. If a list |
1248 | of str, handler will apply to all names in the list. If a |
1249 | str, the handler will apply just to that name. |
1250 | remove : bool |
1251 | @@ -490,62 +500,62 @@ |
1252 | for n in names: |
1253 | self._add_notifiers(handler, n) |
1254 | |
1255 | - def traitlet_names(self, **metadata): |
1256 | - """Get a list of all the names of this classes traitlets.""" |
1257 | - return self.traitlets(**metadata).keys() |
1258 | - |
1259 | - def traitlets(self, **metadata): |
1260 | - """Get a list of all the traitlets of this class. |
1261 | - |
1262 | - The TraitletTypes returned don't know anything about the values |
1263 | - that the various HasTraitlet's instances are holding. |
1264 | + def trait_names(self, **metadata): |
1265 | + """Get a list of all the names of this classes traits.""" |
1266 | + return self.traits(**metadata).keys() |
1267 | + |
1268 | + def traits(self, **metadata): |
1269 | + """Get a list of all the traits of this class. |
1270 | + |
1271 | + The TraitTypes returned don't know anything about the values |
1272 | + that the various HasTrait's instances are holding. |
1273 | |
1274 | This follows the same algorithm as traits does and does not allow |
1275 | for any simple way of specifying merely that a metadata name |
1276 | exists, but has any value. This is because get_metadata returns |
1277 | None if a metadata key doesn't exist. |
1278 | """ |
1279 | - traitlets = dict([memb for memb in getmembers(self.__class__) if \ |
1280 | - isinstance(memb[1], TraitletType)]) |
1281 | + traits = dict([memb for memb in getmembers(self.__class__) if \ |
1282 | + isinstance(memb[1], TraitType)]) |
1283 | |
1284 | if len(metadata) == 0: |
1285 | - return traitlets |
1286 | + return traits |
1287 | |
1288 | for meta_name, meta_eval in metadata.items(): |
1289 | if type(meta_eval) is not FunctionType: |
1290 | metadata[meta_name] = _SimpleTest(meta_eval) |
1291 | |
1292 | result = {} |
1293 | - for name, traitlet in traitlets.items(): |
1294 | + for name, trait in traits.items(): |
1295 | for meta_name, meta_eval in metadata.items(): |
1296 | - if not meta_eval(traitlet.get_metadata(meta_name)): |
1297 | + if not meta_eval(trait.get_metadata(meta_name)): |
1298 | break |
1299 | else: |
1300 | - result[name] = traitlet |
1301 | + result[name] = trait |
1302 | |
1303 | return result |
1304 | |
1305 | - def traitlet_metadata(self, traitletname, key): |
1306 | - """Get metadata values for traitlet by key.""" |
1307 | + def trait_metadata(self, traitname, key): |
1308 | + """Get metadata values for trait by key.""" |
1309 | try: |
1310 | - traitlet = getattr(self.__class__, traitletname) |
1311 | + trait = getattr(self.__class__, traitname) |
1312 | except AttributeError: |
1313 | - raise TraitletError("Class %s does not have a traitlet named %s" % |
1314 | - (self.__class__.__name__, traitletname)) |
1315 | + raise TraitError("Class %s does not have a trait named %s" % |
1316 | + (self.__class__.__name__, traitname)) |
1317 | else: |
1318 | - return traitlet.get_metadata(key) |
1319 | - |
1320 | -#----------------------------------------------------------------------------- |
1321 | -# Actual TraitletTypes implementations/subclasses |
1322 | -#----------------------------------------------------------------------------- |
1323 | - |
1324 | -#----------------------------------------------------------------------------- |
1325 | -# TraitletTypes subclasses for handling classes and instances of classes |
1326 | -#----------------------------------------------------------------------------- |
1327 | - |
1328 | - |
1329 | -class ClassBasedTraitletType(TraitletType): |
1330 | - """A traitlet with error reporting for Type, Instance and This.""" |
1331 | + return trait.get_metadata(key) |
1332 | + |
1333 | +#----------------------------------------------------------------------------- |
1334 | +# Actual TraitTypes implementations/subclasses |
1335 | +#----------------------------------------------------------------------------- |
1336 | + |
1337 | +#----------------------------------------------------------------------------- |
1338 | +# TraitTypes subclasses for handling classes and instances of classes |
1339 | +#----------------------------------------------------------------------------- |
1340 | + |
1341 | + |
1342 | +class ClassBasedTraitType(TraitType): |
1343 | + """A trait with error reporting for Type, Instance and This.""" |
1344 | |
1345 | def error(self, obj, value): |
1346 | kind = type(value) |
1347 | @@ -554,16 +564,16 @@ |
1348 | else: |
1349 | msg = '%s (i.e. %s)' % ( str( kind )[1:-1], repr( value ) ) |
1350 | |
1351 | - super(ClassBasedTraitletType, self).error(obj, msg) |
1352 | - |
1353 | - |
1354 | -class Type(ClassBasedTraitletType): |
1355 | - """A traitlet whose value must be a subclass of a specified class.""" |
1356 | + super(ClassBasedTraitType, self).error(obj, msg) |
1357 | + |
1358 | + |
1359 | +class Type(ClassBasedTraitType): |
1360 | + """A trait whose value must be a subclass of a specified class.""" |
1361 | |
1362 | def __init__ (self, default_value=None, klass=None, allow_none=True, **metadata ): |
1363 | - """Construct a Type traitlet |
1364 | + """Construct a Type trait |
1365 | |
1366 | - A Type traitlet specifies that its values must be subclasses of |
1367 | + A Type trait specifies that its values must be subclasses of |
1368 | a particular class. |
1369 | |
1370 | If only ``default_value`` is given, it is used for the ``klass`` as |
1371 | @@ -575,12 +585,12 @@ |
1372 | The default value must be a subclass of klass. If an str, |
1373 | the str must be a fully specified class name, like 'foo.bar.Bah'. |
1374 | The string is resolved into real class, when the parent |
1375 | - :class:`HasTraitlets` class is instantiated. |
1376 | + :class:`HasTraits` class is instantiated. |
1377 | klass : class, str, None |
1378 | - Values of this traitlet must be a subclass of klass. The klass |
1379 | + Values of this trait must be a subclass of klass. The klass |
1380 | may be specified in a string like: 'foo.bar.MyClass'. |
1381 | The string is resolved into real class, when the parent |
1382 | - :class:`HasTraitlets` class is instantiated. |
1383 | + :class:`HasTraits` class is instantiated. |
1384 | allow_none : boolean |
1385 | Indicates whether None is allowed as an assignable value. Even if |
1386 | ``False``, the default value may be ``None``. |
1387 | @@ -592,7 +602,7 @@ |
1388 | klass = default_value |
1389 | |
1390 | if not (inspect.isclass(klass) or isinstance(klass, basestring)): |
1391 | - raise TraitletError("A Type traitlet must specify a class.") |
1392 | + raise TraitError("A Type trait must specify a class.") |
1393 | |
1394 | self.klass = klass |
1395 | self._allow_none = allow_none |
1396 | @@ -646,7 +656,7 @@ |
1397 | return klass(*self.args, **self.kw) |
1398 | |
1399 | |
1400 | -class Instance(ClassBasedTraitletType): |
1401 | +class Instance(ClassBasedTraitType): |
1402 | """A trait whose value must be an instance of a specified class. |
1403 | |
1404 | The value can also be an instance of a subclass of the specified class. |
1405 | @@ -654,9 +664,9 @@ |
1406 | |
1407 | def __init__(self, klass=None, args=None, kw=None, |
1408 | allow_none=True, **metadata ): |
1409 | - """Construct an Instance traitlet. |
1410 | + """Construct an Instance trait. |
1411 | |
1412 | - This traitlet allows values that are instances of a particular |
1413 | + This trait allows values that are instances of a particular |
1414 | class or its sublclasses. Our implementation is quite different |
1415 | from that of enthough.traits as we don't allow instances to be used |
1416 | for klass and we handle the ``args`` and ``kw`` arguments differently. |
1417 | @@ -664,7 +674,7 @@ |
1418 | Parameters |
1419 | ---------- |
1420 | klass : class, str |
1421 | - The class that forms the basis for the traitlet. Class names |
1422 | + The class that forms the basis for the trait. Class names |
1423 | can also be specified as strings, like 'foo.bar.Bar'. |
1424 | args : tuple |
1425 | Positional arguments for generating the default value. |
1426 | @@ -684,7 +694,7 @@ |
1427 | self._allow_none = allow_none |
1428 | |
1429 | if (klass is None) or (not (inspect.isclass(klass) or isinstance(klass, basestring))): |
1430 | - raise TraitletError('The klass argument must be a class' |
1431 | + raise TraitError('The klass argument must be a class' |
1432 | ' you gave: %r' % klass) |
1433 | self.klass = klass |
1434 | |
1435 | @@ -700,9 +710,9 @@ |
1436 | kw = {} |
1437 | |
1438 | if not isinstance(kw, dict): |
1439 | - raise TraitletError("The 'kw' argument must be a dict or None.") |
1440 | + raise TraitError("The 'kw' argument must be a dict or None.") |
1441 | if not isinstance(args, tuple): |
1442 | - raise TraitletError("The 'args' argument must be a tuple or None.") |
1443 | + raise TraitError("The 'args' argument must be a tuple or None.") |
1444 | |
1445 | default_value = DefaultValueGenerator(*args, **kw) |
1446 | |
1447 | @@ -741,9 +751,9 @@ |
1448 | def get_default_value(self): |
1449 | """Instantiate a default value instance. |
1450 | |
1451 | - This is called when the containing HasTraitlets classes' |
1452 | + This is called when the containing HasTraits classes' |
1453 | :meth:`__new__` method is called to ensure that a unique instance |
1454 | - is created for each HasTraitlets instance. |
1455 | + is created for each HasTraits instance. |
1456 | """ |
1457 | dv = self.default_value |
1458 | if isinstance(dv, DefaultValueGenerator): |
1459 | @@ -752,11 +762,11 @@ |
1460 | return dv |
1461 | |
1462 | |
1463 | -class This(ClassBasedTraitletType): |
1464 | - """A traitlet for instances of the class containing this trait. |
1465 | +class This(ClassBasedTraitType): |
1466 | + """A trait for instances of the class containing this trait. |
1467 | |
1468 | Because how how and when class bodies are executed, the ``This`` |
1469 | - traitlet can only have a default value of None. This, and because we |
1470 | + trait can only have a default value of None. This, and because we |
1471 | always validate default values, ``allow_none`` is *always* true. |
1472 | """ |
1473 | |
1474 | @@ -768,7 +778,7 @@ |
1475 | def validate(self, obj, value): |
1476 | # What if value is a superclass of obj.__class__? This is |
1477 | # complicated if it was the superclass that defined the This |
1478 | - # traitlet. |
1479 | + # trait. |
1480 | if isinstance(value, self.this_class) or (value is None): |
1481 | return value |
1482 | else: |
1483 | @@ -776,17 +786,17 @@ |
1484 | |
1485 | |
1486 | #----------------------------------------------------------------------------- |
1487 | -# Basic TraitletTypes implementations/subclasses |
1488 | +# Basic TraitTypes implementations/subclasses |
1489 | #----------------------------------------------------------------------------- |
1490 | |
1491 | |
1492 | -class Any(TraitletType): |
1493 | +class Any(TraitType): |
1494 | default_value = None |
1495 | info_text = 'any value' |
1496 | |
1497 | |
1498 | -class Int(TraitletType): |
1499 | - """A integer traitlet.""" |
1500 | +class Int(TraitType): |
1501 | + """A integer trait.""" |
1502 | |
1503 | evaluate = int |
1504 | default_value = 0 |
1505 | @@ -798,7 +808,7 @@ |
1506 | self.error(obj, value) |
1507 | |
1508 | class CInt(Int): |
1509 | - """A casting version of the int traitlet.""" |
1510 | + """A casting version of the int trait.""" |
1511 | |
1512 | def validate(self, obj, value): |
1513 | try: |
1514 | @@ -807,8 +817,8 @@ |
1515 | self.error(obj, value) |
1516 | |
1517 | |
1518 | -class Long(TraitletType): |
1519 | - """A long integer traitlet.""" |
1520 | +class Long(TraitType): |
1521 | + """A long integer trait.""" |
1522 | |
1523 | evaluate = long |
1524 | default_value = 0L |
1525 | @@ -823,7 +833,7 @@ |
1526 | |
1527 | |
1528 | class CLong(Long): |
1529 | - """A casting version of the long integer traitlet.""" |
1530 | + """A casting version of the long integer trait.""" |
1531 | |
1532 | def validate(self, obj, value): |
1533 | try: |
1534 | @@ -832,8 +842,8 @@ |
1535 | self.error(obj, value) |
1536 | |
1537 | |
1538 | -class Float(TraitletType): |
1539 | - """A float traitlet.""" |
1540 | +class Float(TraitType): |
1541 | + """A float trait.""" |
1542 | |
1543 | evaluate = float |
1544 | default_value = 0.0 |
1545 | @@ -848,7 +858,7 @@ |
1546 | |
1547 | |
1548 | class CFloat(Float): |
1549 | - """A casting version of the float traitlet.""" |
1550 | + """A casting version of the float trait.""" |
1551 | |
1552 | def validate(self, obj, value): |
1553 | try: |
1554 | @@ -856,8 +866,8 @@ |
1555 | except: |
1556 | self.error(obj, value) |
1557 | |
1558 | -class Complex(TraitletType): |
1559 | - """A traitlet for complex numbers.""" |
1560 | +class Complex(TraitType): |
1561 | + """A trait for complex numbers.""" |
1562 | |
1563 | evaluate = complex |
1564 | default_value = 0.0 + 0.0j |
1565 | @@ -872,7 +882,7 @@ |
1566 | |
1567 | |
1568 | class CComplex(Complex): |
1569 | - """A casting version of the complex number traitlet.""" |
1570 | + """A casting version of the complex number trait.""" |
1571 | |
1572 | def validate (self, obj, value): |
1573 | try: |
1574 | @@ -881,8 +891,8 @@ |
1575 | self.error(obj, value) |
1576 | |
1577 | |
1578 | -class Str(TraitletType): |
1579 | - """A traitlet for strings.""" |
1580 | +class Str(TraitType): |
1581 | + """A trait for strings.""" |
1582 | |
1583 | evaluate = lambda x: x |
1584 | default_value = '' |
1585 | @@ -895,7 +905,7 @@ |
1586 | |
1587 | |
1588 | class CStr(Str): |
1589 | - """A casting version of the string traitlet.""" |
1590 | + """A casting version of the string trait.""" |
1591 | |
1592 | def validate(self, obj, value): |
1593 | try: |
1594 | @@ -907,8 +917,8 @@ |
1595 | self.error(obj, value) |
1596 | |
1597 | |
1598 | -class Unicode(TraitletType): |
1599 | - """A traitlet for unicode strings.""" |
1600 | +class Unicode(TraitType): |
1601 | + """A trait for unicode strings.""" |
1602 | |
1603 | evaluate = unicode |
1604 | default_value = u'' |
1605 | @@ -923,7 +933,7 @@ |
1606 | |
1607 | |
1608 | class CUnicode(Unicode): |
1609 | - """A casting version of the unicode traitlet.""" |
1610 | + """A casting version of the unicode trait.""" |
1611 | |
1612 | def validate(self, obj, value): |
1613 | try: |
1614 | @@ -932,8 +942,8 @@ |
1615 | self.error(obj, value) |
1616 | |
1617 | |
1618 | -class Bool(TraitletType): |
1619 | - """A boolean (True, False) traitlet.""" |
1620 | +class Bool(TraitType): |
1621 | + """A boolean (True, False) trait.""" |
1622 | evaluate = bool |
1623 | default_value = False |
1624 | info_text = 'a boolean' |
1625 | @@ -945,7 +955,7 @@ |
1626 | |
1627 | |
1628 | class CBool(Bool): |
1629 | - """A casting version of the boolean traitlet.""" |
1630 | + """A casting version of the boolean trait.""" |
1631 | |
1632 | def validate(self, obj, value): |
1633 | try: |
1634 | @@ -954,7 +964,7 @@ |
1635 | self.error(obj, value) |
1636 | |
1637 | |
1638 | -class Enum(TraitletType): |
1639 | +class Enum(TraitType): |
1640 | """An enum that whose value must be in a given sequence.""" |
1641 | |
1642 | def __init__(self, values, default_value=None, allow_none=True, **metadata): |
1643 | @@ -999,7 +1009,7 @@ |
1644 | """An instance of a Python list.""" |
1645 | |
1646 | def __init__(self, default_value=None, allow_none=True, **metadata): |
1647 | - """Create a list traitlet type from a list or tuple. |
1648 | + """Create a list trait type from a list or tuple. |
1649 | |
1650 | The default value is created by doing ``list(default_value)``, |
1651 | which creates a copy of the ``default_value``. |
1652 | @@ -1008,6 +1018,8 @@ |
1653 | args = ((),) |
1654 | elif isinstance(default_value, SequenceTypes): |
1655 | args = (default_value,) |
1656 | + else: |
1657 | + raise TypeError('default value of List was %s' % default_value) |
1658 | |
1659 | super(List,self).__init__(klass=list, args=args, |
1660 | allow_none=allow_none, **metadata) |
1661 | |
1662 | === modified file 'docs/source/config/overview.txt' |
1663 | --- docs/source/config/overview.txt 2009-11-09 01:49:24 +0000 |
1664 | +++ docs/source/config/overview.txt 2010-01-18 09:16:19 +0000 |
1665 | @@ -134,7 +134,7 @@ |
1666 | .. note:: |
1667 | |
1668 | Underneath the hood, the :class:`Component` base class is a subclass of |
1669 | - :class:`IPython.utils.traitlets.HasTraitlets`. The |
1670 | + :class:`IPython.utils.traitlets.HasTraits`. The |
1671 | :mod:`IPython.utils.traitlets` module is a lightweight version of |
1672 | :mod:`enthought.traits`. Our implementation is a pure Python subset |
1673 | (mostly API compatible) of :mod:`enthought.traits` that does not have any |
Hopefully pretty straightforward! Note - most of the nose tests that were failing before integrating Fernando's trunk-dev are still failing. They seem unrelated to traitlets, though, and ipython seems to work (at least with casual use).