traits.trait_type
Module¶
Defines the TraitType class.
The TraitType
class a trait handler that is the base class for modern
traits, and provides a richer API than the old-style traits derived from
TraitHandler
.
Classes¶
-
traits.trait_type.
NoDefaultSpecified
= <traits.trait_type._NoDefaultSpecifiedType object>¶ Singleton object that can be passed for the
default_value
argument in theTraitType
constructor, to indicate that no default value was specified.
-
class
traits.trait_type.
TraitType
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Base class for new trait types.
This class enables you to define new traits using a class-based approach, instead of by calling the Trait() factory function with an instance of a TraitHandler derived object.
When subclassing this class, you can implement one or more of the method signatures below. Note that these methods are defined only as comments, because the absence of method definitions in the subclass definition implicitly provides information about how the trait should operate.
The optional methods are as follows:
get(self, object, name)
This is the getter method of a trait that behaves like a property.
If neither this method nor the
set()
method is defined, the value of the trait is handled like a normal object attribute. If this method is not defined, but theset()
method is defined, the trait behaves like a write-only property. This method should return the value of thename
property for theobject
object.- Parameters
- objectany
The object that the property applies to.
- namestr
The name of the property on
object
.
set(self, object, name, value)
This is the setter method of a trait that behaves like a property.
If neither this method nor the
get()
method is implemented, the trait behaves like a normal trait attribute. If this method is not defined, but theget()
method is defined, the trait behaves like a read-only property. This method does not need to return a value, but it should raise aTraitError
exception if the specifiedvalue
is not valid and cannot be coerced or adapted to a valid value.- Parameters
- objectany
The object that the property applies to.
- namestr
The name of the property on
object
.- valueany
The value being assigned as the value of the property.
validate(self, object, name, value)
This method validates, coerces, or adapts the specified
value
as the value of thename
trait of theobject
object. This method is called when a value is assigned to an object trait that is based on this subclass ofTraitType
and the class does not contain a definition for either the get() or set() methods. This method must return the originalvalue
or any suitably coerced or adapted value that is a legal value for the trait. Ifvalue
is not a legal value for the trait, and cannot be coerced or adapted to a legal value, the method should either raise aTraitError
or call theerror
method to raise theTraitError
on its behalf.is_valid_for(self, value)
As an alternative to implementing the
validate
method, you can instead implement theis_valid_for
method, which receives only thevalue
being assigned. It should returnTrue
if the value is valid, andFalse
otherwise.value_for ( self, value )
As another alternative to implementing the
validate
method, you can instead implement thevalue_for
method, which receives only thevalue
being assigned. It should return the validated form ofvalue
if it is valid, or raise aTraitError
if the value is not valid.post_setattr(self, object, name, value)
This method allows the trait to do additional processing after
value
has been successfully assigned to thename
trait of theobject
object. For most traits there is no additional processing that needs to be done, and this method need not be defined. It is normally used for creating “shadow” (i.e., “mapped” traits), but other uses may arise as well. This method does not need to return a value, and should normally not raise any exceptions.-
metadata
= {}¶ The metadata for the trait.
-
default_value
= <undefined>¶ The default value for the trait type.
-
get_default_value
()[source]¶ Get information about the default value.
The default implementation analyzes the value of the trait’s
default_value
attribute and determines an appropriatedefault_value_type
for thedefault_value
. If you need to override this method to provide a different result tuple, the following values are valid values fordefault_value_type
:0, 1: The
default_value
item of the tuple is the default value.2: The object containing the trait is the default value.
3: A new copy of the list specified by
default_value
is the default value.4: A new copy of the dictionary specified by
default_value
is the default value.5: A new instance of TraitListObject constructed using the
default_value
list is the default value.6: A new instance of TraitDictObject constructed using the
default_value
dictionary is the default value.7:
default_value
is a tuple of the form:(callable, args, kw)
, wherecallable
is a callable,args
is a tuple, andkw
is either a dictionary or None. The default value is the result obtained by invokingcallable(\*args, \*\*kw)
.8:
default_value
is a callable. The default value is the result obtained by invokingdefault_value(object)
, whereobject
is the object containing the trait. If the trait has avalidate()
method, thevalidate()
method is also called to validate the result.9: A new instance of
TraitSetObject
constructed using thedefault_value
set is the default value.
- Returns
default_value_type, default_value – The default value information, consisting of an integer, giving the type of default value, and the corresponding default value as described above.
- Return type
int, any
-
clone
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Copy, optionally modifying default value and metadata.
Clones the contents of this object into a new instance of the same class, and then modifies the cloned copy using the specified
default_value
andmetadata
. Returns the cloned object as the result.Note that subclasses can change the signature of this method if needed, but should always call the ‘super’ method if possible.