From fb2ee2ac9c902857fbe7f28638d6428467dd32d9 2010-01-05 23:19:09 From: Dav Clark Date: 2010-01-05 23:19:09 Subject: [PATCH] Final changes traitlet -> trait for review --- diff --git a/IPython/core/component.py b/IPython/core/component.py index 6526ed6..9efd4cd 100644 --- a/IPython/core/component.py +++ b/IPython/core/component.py @@ -27,7 +27,7 @@ from weakref import WeakValueDictionary from IPython.utils.importstring import import_item from IPython.config.loader import Config from IPython.utils.traitlets import ( - HasTraits, TraitletError, MetaHasTraits, Instance, This + HasTraits, TraitError, MetaHasTraits, Instance, This ) @@ -186,7 +186,7 @@ class Component(HasTraits): __metaclass__ = MetaComponent - # Traitlets are fun! + # Traits are fun! config = Instance(Config,(),{}) parent = This() root = This() @@ -250,7 +250,7 @@ class Component(HasTraits): self.created = datetime.datetime.now() #------------------------------------------------------------------------- - # Static traitlet notifiations + # Static trait notifiations #------------------------------------------------------------------------- def _parent_changed(self, name, old, new): @@ -276,12 +276,12 @@ class Component(HasTraits): def _config_changed(self, name, old, new): """Update all the class traits having ``config=True`` as metadata. - For any class traitlet with a ``config`` metadata attribute that is - ``True``, we update the traitlet with the value of the corresponding + For any class trait with a ``config`` metadata attribute that is + ``True``, we update the trait with the value of the corresponding config entry. """ - # Get all traitlets with a config metadata entry that is True - traitlets = self.traits(config=True) + # Get all traits with a config metadata entry that is True + traits = self.traits(config=True) # We auto-load config section for this class as well as any parent # classes that are Component subclasses. This starts with Component @@ -295,7 +295,7 @@ class Component(HasTraits): # dynamically create the section with name self.__class__.__name__. if new._has_section(sname): my_config = new[sname] - for k, v in traitlets.items(): + for k, v in traits.items(): try: config_value = my_config[k] except KeyError: diff --git a/IPython/core/iplib.py b/IPython/core/iplib.py index cb6aa2c..7e70584 100644 --- a/IPython/core/iplib.py +++ b/IPython/core/iplib.py @@ -165,7 +165,7 @@ def get_default_editor(): class SeparateStr(Str): """A Str subclass to validate separate_in, separate_out, etc. - This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'. + This is a Str based trait that converts '0'->'' and '\\n'->'\n'. """ def validate(self, obj, value): @@ -246,7 +246,7 @@ class InteractiveShell(Component, Magic): screen_length = Int(0, config=True) - # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n' + # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' separate_in = SeparateStr('\n', config=True) separate_out = SeparateStr('', config=True) separate_out2 = SeparateStr('', config=True) @@ -269,7 +269,7 @@ class InteractiveShell(Component, Magic): banner1=None, banner2=None, display_banner=None, custom_exceptions=((),None)): - # This is where traitlets with a config_key argument are updated + # This is where traits with a config_key argument are updated # from the values on config. super(InteractiveShell, self).__init__(parent, config=config) @@ -323,7 +323,7 @@ class InteractiveShell(Component, Magic): return self #------------------------------------------------------------------------- - # Traitlet changed handlers + # Trait changed handlers #------------------------------------------------------------------------- def _banner1_changed(self): diff --git a/IPython/core/tests/test_component.py b/IPython/core/tests/test_component.py index 9774320..5ffb169 100644 --- a/IPython/core/tests/test_component.py +++ b/IPython/core/tests/test_component.py @@ -24,7 +24,7 @@ from unittest import TestCase from IPython.core.component import Component, ComponentError from IPython.utils.traitlets import ( - TraitletError, Int, Float, Str + TraitError, Int, Float, Str ) from IPython.config.loader import Config @@ -109,7 +109,7 @@ class TestComponent(TestCase): def test_subclass_parent(self): c1 = Component(None) - self.assertRaises(TraitletError, setattr, c1, 'parent', 10) + self.assertRaises(TraitError, setattr, c1, 'parent', 10) class MyComponent(Component): pass diff --git a/IPython/utils/tests/test_traitlets.py b/IPython/utils/tests/test_traitlets.py index e003d9a..f1a2b7b 100644 --- a/IPython/utils/tests/test_traitlets.py +++ b/IPython/utils/tests/test_traitlets.py @@ -29,8 +29,8 @@ import os from unittest import TestCase from IPython.utils.traitlets import ( - HasTraits, MetaHasTraits, TraitletType, Any, - Int, Long, Float, Complex, Str, Unicode, Bool, TraitletError, + HasTraits, MetaHasTraits, TraitType, Any, + Int, Long, Float, Complex, Str, Unicode, Bool, TraitError, Undefined, Type, This, Instance ) @@ -40,9 +40,9 @@ from IPython.utils.traitlets import ( #----------------------------------------------------------------------------- -class HasTraitletsStub(HasTraits): +class HasTraitsStub(HasTraits): - def _notify_traitlet(self, name, old, new): + def _notify_trait(self, name, old, new): self._notify_name = name self._notify_old = old self._notify_new = new @@ -53,17 +53,17 @@ class HasTraitletsStub(HasTraits): #----------------------------------------------------------------------------- -class TestTraitletType(TestCase): +class TestTraitType(TestCase): def test_get_undefined(self): class A(HasTraits): - a = TraitletType + a = TraitType a = A() self.assertEquals(a.a, Undefined) def test_set(self): - class A(HasTraitletsStub): - a = TraitletType + class A(HasTraitsStub): + a = TraitType a = A() a.a = 10 @@ -73,10 +73,10 @@ class TestTraitletType(TestCase): self.assertEquals(a._notify_new, 10) def test_validate(self): - class MyTT(TraitletType): + class MyTT(TraitType): def validate(self, inst, value): return -1 - class A(HasTraitletsStub): + class A(HasTraitsStub): tt = MyTT a = A() @@ -84,7 +84,7 @@ class TestTraitletType(TestCase): self.assertEquals(a.tt, -1) def test_default_validate(self): - class MyIntTT(TraitletType): + class MyIntTT(TraitType): def validate(self, obj, value): if isinstance(value, int): return value @@ -97,10 +97,10 @@ class TestTraitletType(TestCase): # Defaults are validated when the HasTraits is instantiated class B(HasTraits): tt = MyIntTT('bad default') - self.assertRaises(TraitletError, B) + self.assertRaises(TraitError, B) def test_is_valid_for(self): - class MyTT(TraitletType): + class MyTT(TraitType): def is_valid_for(self, value): return True class A(HasTraits): @@ -111,7 +111,7 @@ class TestTraitletType(TestCase): self.assertEquals(a.tt, 10) def test_value_for(self): - class MyTT(TraitletType): + class MyTT(TraitType): def value_for(self, value): return 20 class A(HasTraits): @@ -123,15 +123,15 @@ class TestTraitletType(TestCase): def test_info(self): class A(HasTraits): - tt = TraitletType + tt = TraitType a = A() self.assertEquals(A.tt.info(), 'any value') def test_error(self): class A(HasTraits): - tt = TraitletType + tt = TraitType a = A() - self.assertRaises(TraitletError, A.tt.error, a, 10) + self.assertRaises(TraitError, A.tt.error, a, 10) class TestHasTraitsMeta(TestCase): @@ -204,8 +204,8 @@ class TestHasTraitsNotify(TestCase): self.assert_(('a',0,10) in self._notify1) a.b = 10.0 self.assert_(('b',0.0,10.0) in self._notify1) - self.assertRaises(TraitletError,setattr,a,'a','bad string') - self.assertRaises(TraitletError,setattr,a,'b','bad string') + self.assertRaises(TraitError,setattr,a,'a','bad string') + self.assertRaises(TraitError,setattr,a,'b','bad string') self._notify1 = [] a.on_trait_change(self.notify1,remove=True) a.a = 20 @@ -224,7 +224,7 @@ class TestHasTraitsNotify(TestCase): self.assertEquals(len(self._notify1),0) a.a = 10 self.assert_(('a',0,10) in self._notify1) - self.assertRaises(TraitletError,setattr,a,'a','bad string') + self.assertRaises(TraitError,setattr,a,'a','bad string') def test_subclass(self): @@ -324,7 +324,7 @@ class TestHasTraitsNotify(TestCase): self.assertEquals(self.cb,('a',1000,10000)) a.on_trait_change(callback3, 'a', remove=True) - self.assertEquals(len(a._traitlet_notifiers['a']),0) + self.assertEquals(len(a._trait_notifiers['a']),0) class TestHasTraits(TestCase): @@ -356,17 +356,17 @@ class TestHasTraits(TestCase): j = Int(0) a = A() self.assertEquals(a.traits(), dict(i=A.i, f=A.f, j=A.j)) - traitlets = a.traits(config_key='VALUE1', other_thing='VALUE2') - self.assertEquals(traitlets, dict(i=A.i)) + traits = a.traits(config_key='VALUE1', other_thing='VALUE2') + self.assertEquals(traits, dict(i=A.i)) # This passes, but it shouldn't because I am replicating a bug in # traits. - traitlets = a.traits(config_key=lambda v: True) - self.assertEquals(traitlets, dict(i=A.i, f=A.f, j=A.j)) + traits = a.traits(config_key=lambda v: True) + self.assertEquals(traits, dict(i=A.i, f=A.f, j=A.j)) #----------------------------------------------------------------------------- -# Tests for specific traitlet types +# Tests for specific trait types #----------------------------------------------------------------------------- @@ -383,7 +383,7 @@ class TestType(TestCase): a.klass = B self.assertEquals(a.klass, B) - self.assertRaises(TraitletError, setattr, a, 'klass', 10) + self.assertRaises(TraitError, setattr, a, 'klass', 10) def test_value(self): @@ -394,8 +394,8 @@ class TestType(TestCase): a = A() self.assertEquals(a.klass, B) - self.assertRaises(TraitletError, setattr, a, 'klass', C) - self.assertRaises(TraitletError, setattr, a, 'klass', object) + self.assertRaises(TraitError, setattr, a, 'klass', C) + self.assertRaises(TraitError, setattr, a, 'klass', object) a.klass = B def test_allow_none(self): @@ -407,7 +407,7 @@ class TestType(TestCase): a = A() self.assertEquals(a.klass, B) - self.assertRaises(TraitletError, setattr, a, 'klass', None) + self.assertRaises(TraitError, setattr, a, 'klass', None) a.klass = C self.assertEquals(a.klass, C) @@ -434,7 +434,7 @@ class TestType(TestCase): class C(HasTraits): klass = Type(None, B, allow_none=False) - self.assertRaises(TraitletError, C) + self.assertRaises(TraitError, C) def test_str_klass(self): @@ -446,7 +446,7 @@ class TestType(TestCase): a.klass = Struct self.assertEquals(a.klass, Struct) - self.assertRaises(TraitletError, setattr, a, 'klass', 10) + self.assertRaises(TraitError, setattr, a, 'klass', 10) class TestInstance(TestCase): @@ -464,9 +464,9 @@ class TestInstance(TestCase): self.assert_(isinstance(a.inst, Foo)) a.inst = Bar() self.assert_(isinstance(a.inst, Foo)) - self.assertRaises(TraitletError, setattr, a, 'inst', Foo) - self.assertRaises(TraitletError, setattr, a, 'inst', Bar) - self.assertRaises(TraitletError, setattr, a, 'inst', Bah()) + self.assertRaises(TraitError, setattr, a, 'inst', Foo) + self.assertRaises(TraitError, setattr, a, 'inst', Bar) + self.assertRaises(TraitError, setattr, a, 'inst', Bah()) def test_unique_default_value(self): class Foo(object): pass @@ -507,7 +507,7 @@ class TestInstance(TestCase): class A(HasTraits): inst = Instance(Foo, allow_none=False) - self.assertRaises(TraitletError, A) + self.assertRaises(TraitError, A) def test_instance(self): class Foo(object): pass @@ -516,7 +516,7 @@ class TestInstance(TestCase): class A(HasTraits): inst = Instance(Foo()) - self.assertRaises(TraitletError, inner) + self.assertRaises(TraitError, inner) class TestThis(TestCase): @@ -530,7 +530,7 @@ class TestThis(TestCase): g = Foo() f.this = g self.assertEquals(f.this, g) - self.assertRaises(TraitletError, setattr, f, 'this', 10) + self.assertRaises(TraitError, setattr, f, 'this', 10) def test_this_inst(self): class Foo(HasTraits): @@ -561,10 +561,10 @@ class TestThis(TestCase): b = Bar() f.t = b self.assertEquals(f.t, b) - self.assertRaises(TraitletError, setattr, b, 't', f) + self.assertRaises(TraitError, setattr, b, 't', f) -class TraitletTestBase(TestCase): - """A best testing class for basic traitlet types.""" +class TraitTestBase(TestCase): + """A best testing class for basic trait types.""" def assign(self, value): self.obj.value = value @@ -581,33 +581,33 @@ class TraitletTestBase(TestCase): def test_bad_values(self): if hasattr(self, '_bad_values'): for value in self._bad_values: - self.assertRaises(TraitletError, self.assign, value) + self.assertRaises(TraitError, self.assign, value) def test_default_value(self): if hasattr(self, '_default_value'): self.assertEquals(self._default_value, self.obj.value) -class AnyTraitlet(HasTraits): +class AnyTrait(HasTraits): value = Any -class AnyTraitTest(TraitletTestBase): +class AnyTraitTest(TraitTestBase): - obj = AnyTraitlet() + obj = AnyTrait() _default_value = None _good_values = [10.0, 'ten', u'ten', [10], {'ten': 10},(10,), None, 1j] _bad_values = [] -class IntTraitlet(HasTraits): +class IntTrait(HasTraits): value = Int(99) -class TestInt(TraitletTestBase): +class TestInt(TraitTestBase): - obj = IntTraitlet() + obj = IntTrait() _default_value = 99 _good_values = [10, -10] _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, 1j, 10L, @@ -615,13 +615,13 @@ class TestInt(TraitletTestBase): u'-10L', u'10.1', u'-10.1', '10', '-10', u'10', u'-10'] -class LongTraitlet(HasTraits): +class LongTrait(HasTraits): value = Long(99L) -class TestLong(TraitletTestBase): +class TestLong(TraitTestBase): - obj = LongTraitlet() + obj = LongTrait() _default_value = 99L _good_values = [10, -10, 10L, -10L] @@ -631,13 +631,13 @@ class TestLong(TraitletTestBase): u'-10.1'] -class FloatTraitlet(HasTraits): +class FloatTrait(HasTraits): value = Float(99.0) -class TestFloat(TraitletTestBase): +class TestFloat(TraitTestBase): - obj = FloatTraitlet() + obj = FloatTrait() _default_value = 99.0 _good_values = [10, -10, 10.1, -10.1] @@ -646,13 +646,13 @@ class TestFloat(TraitletTestBase): u'-10', u'10L', u'-10L', u'10.1', u'-10.1'] -class ComplexTraitlet(HasTraits): +class ComplexTrait(HasTraits): value = Complex(99.0-99.0j) -class TestComplex(TraitletTestBase): +class TestComplex(TraitTestBase): - obj = ComplexTraitlet() + obj = ComplexTrait() _default_value = 99.0-99.0j _good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j, @@ -660,13 +660,13 @@ class TestComplex(TraitletTestBase): _bad_values = [10L, -10L, u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None] -class StringTraitlet(HasTraits): +class StringTrait(HasTraits): value = Str('string') -class TestString(TraitletTestBase): +class TestString(TraitTestBase): - obj = StringTraitlet() + obj = StringTrait() _default_value = 'string' _good_values = ['10', '-10', '10L', @@ -675,13 +675,13 @@ class TestString(TraitletTestBase): ['ten'],{'ten': 10},(10,), None, u'string'] -class UnicodeTraitlet(HasTraits): +class UnicodeTrait(HasTraits): value = Unicode(u'unicode') -class TestUnicode(TraitletTestBase): +class TestUnicode(TraitTestBase): - obj = UnicodeTraitlet() + obj = UnicodeTrait() _default_value = u'unicode' _good_values = ['10', '-10', '10L', '-10L', '10.1', diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index e100ba4..5d739b2 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -17,7 +17,7 @@ We don't support: * Delegation * Automatic GUI generation * A full set of trait types. Most importantly, we don't provide container - traitlets (list, dict, tuple) that can trigger notifications if their + traits (list, dict, tuple) that can trigger notifications if their contents change. * API compatibility with enthought.traits @@ -86,16 +86,9 @@ NoDefaultSpecified = NoDefaultSpecified() class Undefined ( object ): pass Undefined = Undefined() -# The following allows us to test specifically for a TraitletError, or more -# generally for a TraitError if we are going for compatability with Enthought -# Traits class TraitError(Exception): pass -class TraitletError(TraitError): - pass - - #----------------------------------------------------------------------------- # Utilities #----------------------------------------------------------------------------- @@ -168,25 +161,25 @@ class _SimpleTest: #----------------------------------------------------------------------------- -# Base TraitletType for all traitlets +# Base TraitType for all traits #----------------------------------------------------------------------------- -class TraitletType(object): - """A base class for all traitlet descriptors. +class TraitType(object): + """A base class for all trait descriptors. Notes ----- - Our implementation of traitlets is based on Python's descriptor + Our implementation of traits is based on Python's descriptor prototol. This class is the base class for all such descriptors. The only magic we use is a custom metaclass for the main :class:`HasTraits` class that does the following: - 1. Sets the :attr:`name` attribute of every :class:`TraitletType` + 1. Sets the :attr:`name` attribute of every :class:`TraitType` instance in the class dict to the name of the attribute. - 2. Sets the :attr:`this_class` attribute of every :class:`TraitletType` - instance in the class dict to the *class* that declared the traitlet. - This is used by the :class:`This` traitlet to allow subclasses to + 2. Sets the :attr:`this_class` attribute of every :class:`TraitType` + instance in the class dict to the *class* that declared the trait. + This is used by the :class:`This` trait to allow subclasses to accept superclasses for :class:`This` values. """ @@ -196,7 +189,7 @@ class TraitletType(object): info_text = 'any value' def __init__(self, default_value=NoDefaultSpecified, **metadata): - """Create a TraitletType. + """Create a TraitType. """ if default_value is not NoDefaultSpecified: self.default_value = default_value @@ -250,10 +243,10 @@ class TraitletType(object): """ dv = self.get_default_value() newdv = self._validate(obj, dv) - obj._traitlet_values[self.name] = newdv + obj._trait_values[self.name] = newdv def __get__(self, obj, cls=None): - """Get the value of the traitlet by self.name for the instance. + """Get the value of the trait by self.name for the instance. Default values are instantiated when :meth:`HasTraits.__new__` is called. Thus by the time this method gets called either the @@ -264,11 +257,11 @@ class TraitletType(object): return self else: try: - value = obj._traitlet_values[self.name] + value = obj._trait_values[self.name] except: # HasTraits should call set_default_value to populate # this. So this should never be reached. - raise TraitletError('Unexpected error in TraitletType: ' + raise TraitError('Unexpected error in TraitType: ' 'default value not set properly') else: return value @@ -277,8 +270,8 @@ class TraitletType(object): new_value = self._validate(obj, value) old_value = self.__get__(obj) if old_value != new_value: - obj._traitlet_values[self.name] = new_value - obj._notify_traitlet(self.name, old_value, new_value) + obj._trait_values[self.name] = new_value + obj._notify_trait(self.name, old_value, new_value) def _validate(self, obj, value): if hasattr(self, 'validate'): @@ -288,7 +281,7 @@ class TraitletType(object): if valid: return value else: - raise TraitletError('invalid value for type: %r' % value) + raise TraitError('invalid value for type: %r' % value) elif hasattr(self, 'value_for'): return self.value_for(value) else: @@ -299,13 +292,13 @@ class TraitletType(object): def error(self, obj, value): if obj is not None: - e = "The '%s' traitlet of %s instance must be %s, but a value of %s was specified." \ + e = "The '%s' trait of %s instance must be %s, but a value of %s was specified." \ % (self.name, class_of(obj), self.info(), repr_type(value)) else: - e = "The '%s' traitlet must be %s, but a value of %r was specified." \ + e = "The '%s' trait must be %s, but a value of %r was specified." \ % (self.name, self.info(), repr_type(value)) - raise TraitletError(e) + raise TraitError(e) def get_metadata(self, key): return getattr(self, '_metadata', {}).get(key, None) @@ -322,21 +315,21 @@ class TraitletType(object): class MetaHasTraits(type): """A metaclass for HasTraits. - This metaclass makes sure that any TraitletType class attributes are + This metaclass makes sure that any TraitType class attributes are instantiated and sets their name attribute. """ def __new__(mcls, name, bases, classdict): """Create the HasTraits class. - This instantiates all TraitletTypes in the class dict and sets their + This instantiates all TraitTypes in the class dict and sets their :attr:`name` attribute. """ for k,v in classdict.iteritems(): - if isinstance(v, TraitletType): + if isinstance(v, TraitType): v.name = k elif inspect.isclass(v): - if issubclass(v, TraitletType): + if issubclass(v, TraitType): vinst = v() vinst.name = k classdict[k] = vinst @@ -345,11 +338,11 @@ class MetaHasTraits(type): def __init__(cls, name, bases, classdict): """Finish initializing the HasTraits class. - This sets the :attr:`this_class` attribute of each TraitletType in the + This sets the :attr:`this_class` attribute of each TraitType in the class dict to the newly created class ``cls``. """ for k, v in classdict.iteritems(): - if isinstance(v, TraitletType): + if isinstance(v, TraitType): v.this_class = cls super(MetaHasTraits, cls).__init__(name, bases, classdict) @@ -365,25 +358,25 @@ class HasTraits(object): inst = new_meth(cls) else: inst = new_meth(cls, *args, **kw) - inst._traitlet_values = {} - inst._traitlet_notifiers = {} - # Here we tell all the TraitletType instances to set their default + inst._trait_values = {} + inst._trait_notifiers = {} + # Here we tell all the TraitType instances to set their default # values on the instance. for key in dir(cls): value = getattr(cls, key) - if isinstance(value, TraitletType): + if isinstance(value, TraitType): value.instance_init(inst) return inst # def __init__(self): - # self._traitlet_values = {} - # self._traitlet_notifiers = {} + # self._trait_values = {} + # self._trait_notifiers = {} - def _notify_traitlet(self, name, old_value, new_value): + def _notify_trait(self, name, old_value, new_value): # First dynamic ones - callables = self._traitlet_notifiers.get(name,[]) - more_callables = self._traitlet_notifiers.get('anytrait',[]) + callables = self._trait_notifiers.get(name,[]) + more_callables = self._trait_notifiers.get('anytrait',[]) callables.extend(more_callables) # Now static ones @@ -416,25 +409,25 @@ class HasTraits(object): elif nargs + offset == 3: c(name, old_value, new_value) else: - raise TraitletError('a traitlet changed callback ' + raise TraitError('a trait changed callback ' 'must have 0-3 arguments.') else: - raise TraitletError('a traitlet changed callback ' + raise TraitError('a trait changed callback ' 'must be callable.') def _add_notifiers(self, handler, name): - if not self._traitlet_notifiers.has_key(name): + if not self._trait_notifiers.has_key(name): nlist = [] - self._traitlet_notifiers[name] = nlist + self._trait_notifiers[name] = nlist else: - nlist = self._traitlet_notifiers[name] + nlist = self._trait_notifiers[name] if handler not in nlist: nlist.append(handler) def _remove_notifiers(self, handler, name): - if self._traitlet_notifiers.has_key(name): - nlist = self._traitlet_notifiers[name] + if self._trait_notifiers.has_key(name): + nlist = self._trait_notifiers[name] try: index = nlist.index(handler) except ValueError: @@ -443,24 +436,24 @@ class HasTraits(object): del nlist[index] def on_trait_change(self, handler, name=None, remove=False): - """Setup a handler to be called when a traitlet changes. + """Setup a handler to be called when a trait changes. - This is used to setup dynamic notifications of traitlet changes. + This is used to setup dynamic notifications of trait changes. Static handlers can be created by creating methods on a HasTraits - subclass with the naming convention '_[traitletname]_changed'. Thus, - to create static handler for the traitlet 'a', create the method + subclass with the naming convention '_[traitname]_changed'. Thus, + to create static handler for the trait 'a', create the method _a_changed(self, name, old, new) (fewer arguments can be used, see below). Parameters ---------- handler : callable - A callable that is called when a traitlet changes. Its + A callable that is called when a trait changes. Its signature can be handler(), handler(name), handler(name, new) or handler(name, old, new). name : list, str, None - If None, the handler will apply to all traitlets. If a list + If None, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name. remove : bool @@ -477,61 +470,61 @@ class HasTraits(object): self._add_notifiers(handler, n) def trait_names(self, **metadata): - """Get a list of all the names of this classes traitlets.""" + """Get a list of all the names of this classes traits.""" return self.traits(**metadata).keys() def traits(self, **metadata): - """Get a list of all the traitlets of this class. + """Get a list of all the traits of this class. - The TraitletTypes returned don't know anything about the values - that the various HasTraitlet's instances are holding. + The TraitTypes returned don't know anything about the values + that the various HasTrait's instances are holding. This follows the same algorithm as traits does and does not allow for any simple way of specifying merely that a metadata name exists, but has any value. This is because get_metadata returns None if a metadata key doesn't exist. """ - traitlets = dict([memb for memb in inspect.getmembers(self.__class__) if \ - isinstance(memb[1], TraitletType)]) + traits = dict([memb for memb in inspect.getmembers(self.__class__) if \ + isinstance(memb[1], TraitType)]) if len(metadata) == 0: - return traitlets + return traits for meta_name, meta_eval in metadata.items(): if type(meta_eval) is not FunctionType: metadata[meta_name] = _SimpleTest(meta_eval) result = {} - for name, traitlet in traitlets.items(): + for name, trait in traits.items(): for meta_name, meta_eval in metadata.items(): - if not meta_eval(traitlet.get_metadata(meta_name)): + if not meta_eval(trait.get_metadata(meta_name)): break else: - result[name] = traitlet + result[name] = trait return result - def trait_metadata(self, traitletname, key): - """Get metadata values for traitlet by key.""" + def trait_metadata(self, traitname, key): + """Get metadata values for trait by key.""" try: - traitlet = getattr(self.__class__, traitletname) + trait = getattr(self.__class__, traitname) except AttributeError: - raise TraitletError("Class %s does not have a traitlet named %s" % - (self.__class__.__name__, traitletname)) + raise TraitError("Class %s does not have a trait named %s" % + (self.__class__.__name__, traitname)) else: - return traitlet.get_metadata(key) + return trait.get_metadata(key) #----------------------------------------------------------------------------- -# Actual TraitletTypes implementations/subclasses +# Actual TraitTypes implementations/subclasses #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- -# TraitletTypes subclasses for handling classes and instances of classes +# TraitTypes subclasses for handling classes and instances of classes #----------------------------------------------------------------------------- -class ClassBasedTraitletType(TraitletType): - """A traitlet with error reporting for Type, Instance and This.""" +class ClassBasedTraitType(TraitType): + """A trait with error reporting for Type, Instance and This.""" def error(self, obj, value): kind = type(value) @@ -540,16 +533,16 @@ class ClassBasedTraitletType(TraitletType): else: msg = '%s (i.e. %s)' % ( str( kind )[1:-1], repr( value ) ) - super(ClassBasedTraitletType, self).error(obj, msg) + super(ClassBasedTraitType, self).error(obj, msg) -class Type(ClassBasedTraitletType): - """A traitlet whose value must be a subclass of a specified class.""" +class Type(ClassBasedTraitType): + """A trait whose value must be a subclass of a specified class.""" def __init__ (self, default_value=None, klass=None, allow_none=True, **metadata ): - """Construct a Type traitlet + """Construct a Type trait - A Type traitlet specifies that its values must be subclasses of + A Type trait specifies that its values must be subclasses of a particular class. If only ``default_value`` is given, it is used for the ``klass`` as @@ -563,7 +556,7 @@ class Type(ClassBasedTraitletType): The string is resolved into real class, when the parent :class:`HasTraits` class is instantiated. klass : class, str, None - Values of this traitlet must be a subclass of klass. The klass + Values of this trait must be a subclass of klass. The klass may be specified in a string like: 'foo.bar.MyClass'. The string is resolved into real class, when the parent :class:`HasTraits` class is instantiated. @@ -578,7 +571,7 @@ class Type(ClassBasedTraitletType): klass = default_value if not (inspect.isclass(klass) or isinstance(klass, basestring)): - raise TraitletError("A Type traitlet must specify a class.") + raise TraitError("A Type trait must specify a class.") self.klass = klass self._allow_none = allow_none @@ -632,7 +625,7 @@ class DefaultValueGenerator(object): return klass(*self.args, **self.kw) -class Instance(ClassBasedTraitletType): +class Instance(ClassBasedTraitType): """A trait whose value must be an instance of a specified class. The value can also be an instance of a subclass of the specified class. @@ -640,9 +633,9 @@ class Instance(ClassBasedTraitletType): def __init__(self, klass=None, args=None, kw=None, allow_none=True, **metadata ): - """Construct an Instance traitlet. + """Construct an Instance trait. - This traitlet allows values that are instances of a particular + This trait allows values that are instances of a particular class or its sublclasses. Our implementation is quite different from that of enthough.traits as we don't allow instances to be used for klass and we handle the ``args`` and ``kw`` arguments differently. @@ -650,7 +643,7 @@ class Instance(ClassBasedTraitletType): Parameters ---------- klass : class, str - The class that forms the basis for the traitlet. Class names + The class that forms the basis for the trait. Class names can also be specified as strings, like 'foo.bar.Bar'. args : tuple Positional arguments for generating the default value. @@ -670,7 +663,7 @@ class Instance(ClassBasedTraitletType): self._allow_none = allow_none if (klass is None) or (not (inspect.isclass(klass) or isinstance(klass, basestring))): - raise TraitletError('The klass argument must be a class' + raise TraitError('The klass argument must be a class' ' you gave: %r' % klass) self.klass = klass @@ -686,9 +679,9 @@ class Instance(ClassBasedTraitletType): kw = {} if not isinstance(kw, dict): - raise TraitletError("The 'kw' argument must be a dict or None.") + raise TraitError("The 'kw' argument must be a dict or None.") if not isinstance(args, tuple): - raise TraitletError("The 'args' argument must be a tuple or None.") + raise TraitError("The 'args' argument must be a tuple or None.") default_value = DefaultValueGenerator(*args, **kw) @@ -738,11 +731,11 @@ class Instance(ClassBasedTraitletType): return dv -class This(ClassBasedTraitletType): - """A traitlet for instances of the class containing this trait. +class This(ClassBasedTraitType): + """A trait for instances of the class containing this trait. Because how how and when class bodies are executed, the ``This`` - traitlet can only have a default value of None. This, and because we + trait can only have a default value of None. This, and because we always validate default values, ``allow_none`` is *always* true. """ @@ -754,7 +747,7 @@ class This(ClassBasedTraitletType): def validate(self, obj, value): # What if value is a superclass of obj.__class__? This is # complicated if it was the superclass that defined the This - # traitlet. + # trait. if isinstance(value, self.this_class) or (value is None): return value else: @@ -762,17 +755,17 @@ class This(ClassBasedTraitletType): #----------------------------------------------------------------------------- -# Basic TraitletTypes implementations/subclasses +# Basic TraitTypes implementations/subclasses #----------------------------------------------------------------------------- -class Any(TraitletType): +class Any(TraitType): default_value = None info_text = 'any value' -class Int(TraitletType): - """A integer traitlet.""" +class Int(TraitType): + """A integer trait.""" evaluate = int default_value = 0 @@ -784,7 +777,7 @@ class Int(TraitletType): self.error(obj, value) class CInt(Int): - """A casting version of the int traitlet.""" + """A casting version of the int trait.""" def validate(self, obj, value): try: @@ -793,8 +786,8 @@ class CInt(Int): self.error(obj, value) -class Long(TraitletType): - """A long integer traitlet.""" +class Long(TraitType): + """A long integer trait.""" evaluate = long default_value = 0L @@ -809,7 +802,7 @@ class Long(TraitletType): class CLong(Long): - """A casting version of the long integer traitlet.""" + """A casting version of the long integer trait.""" def validate(self, obj, value): try: @@ -818,8 +811,8 @@ class CLong(Long): self.error(obj, value) -class Float(TraitletType): - """A float traitlet.""" +class Float(TraitType): + """A float trait.""" evaluate = float default_value = 0.0 @@ -834,7 +827,7 @@ class Float(TraitletType): class CFloat(Float): - """A casting version of the float traitlet.""" + """A casting version of the float trait.""" def validate(self, obj, value): try: @@ -842,8 +835,8 @@ class CFloat(Float): except: self.error(obj, value) -class Complex(TraitletType): - """A traitlet for complex numbers.""" +class Complex(TraitType): + """A trait for complex numbers.""" evaluate = complex default_value = 0.0 + 0.0j @@ -858,7 +851,7 @@ class Complex(TraitletType): class CComplex(Complex): - """A casting version of the complex number traitlet.""" + """A casting version of the complex number trait.""" def validate (self, obj, value): try: @@ -867,8 +860,8 @@ class CComplex(Complex): self.error(obj, value) -class Str(TraitletType): - """A traitlet for strings.""" +class Str(TraitType): + """A trait for strings.""" evaluate = lambda x: x default_value = '' @@ -881,7 +874,7 @@ class Str(TraitletType): class CStr(Str): - """A casting version of the string traitlet.""" + """A casting version of the string trait.""" def validate(self, obj, value): try: @@ -893,8 +886,8 @@ class CStr(Str): self.error(obj, value) -class Unicode(TraitletType): - """A traitlet for unicode strings.""" +class Unicode(TraitType): + """A trait for unicode strings.""" evaluate = unicode default_value = u'' @@ -909,7 +902,7 @@ class Unicode(TraitletType): class CUnicode(Unicode): - """A casting version of the unicode traitlet.""" + """A casting version of the unicode trait.""" def validate(self, obj, value): try: @@ -918,8 +911,8 @@ class CUnicode(Unicode): self.error(obj, value) -class Bool(TraitletType): - """A boolean (True, False) traitlet.""" +class Bool(TraitType): + """A boolean (True, False) trait.""" evaluate = bool default_value = False info_text = 'a boolean' @@ -931,7 +924,7 @@ class Bool(TraitletType): class CBool(Bool): - """A casting version of the boolean traitlet.""" + """A casting version of the boolean trait.""" def validate(self, obj, value): try: @@ -940,7 +933,7 @@ class CBool(Bool): self.error(obj, value) -class Enum(TraitletType): +class Enum(TraitType): """An enum that whose value must be in a given sequence.""" def __init__(self, values, default_value=None, allow_none=True, **metadata): @@ -985,7 +978,7 @@ class List(Instance): """An instance of a Python list.""" def __init__(self, default_value=None, allow_none=True, **metadata): - """Create a list traitlet type from a list or tuple. + """Create a list trait type from a list or tuple. The default value is created by doing ``list(default_value)``, which creates a copy of the ``default_value``.