API Reference for Enthought Tool Suite 3.0.1
Defines the 'core' traits for the Traits package. A trait is a type definition that can be used for normal Python object attributes, giving the attributes some additional characteristics:
Note: 'trait' is a synonym for 'property', but is used instead of the word 'property' to differentiate it from the Python language 'property' feature.
CallableTypes = (FunctionType, MethodType)
ConstantTypes = (NoneType, IntType, LongType, FloatType, ComplexType, StringType, UnicodeType)
DefaultValues = { Name('StringType') : Const(''), Name('UnicodeType') : Const(u''), Name('IntType') : Const(0), Name('LongType') : Const(0L), Name('FloatType') : Const(0.0), Name('ComplexType') : Const(0j), Name('ListType') : List(()), Name('TupleType') : Tuple(()), Name('DictType') : Dict(()), Name('BooleanType') : Name('False') }
DefaultValueSpecial = [ Missing, Self ]
DefaultValueTypes = [ ListType, DictType ]
Generic trait with 'object' behavior:
generic_trait = CTrait(8)
HTMLTextEditor = None
Mapping from 'ctrait' default value types to a string representation:
KindMap = { Const(0) : Const('value'), Const(1) : Const('value'), Const(2) : Const('self'), Const(3) : Const('list'), Const(4) : Const('dict'), Const(5) : Const('list'), Const(6) : Const('dict'), Const(7) : Const('factory'), Const(8) : Const('method') }
MultilineTextEditor = None
PasswordEditor = None
PythonShellEditor = None
PythonTypes = (StringType, UnicodeType, IntType, LongType, FloatType, ComplexType, ListType, TupleType, DictType, FunctionType, MethodType, ClassType, InstanceType, TypeType, NoneType)
SourceCodeEditor = None
SpecialNames = { }
TraitTypes = (TraitHandler, CTrait)
Unpickles new-style objects.
Factory function that returns an editor that treats a multi-line string as source code.
Returns a trait whose value must be a GUI toolkit-specific color.
For wxPython, the returned trait accepts any of the following values:
For wxPython, 0x000000 (that is, white)
Returns a trait whose value must be a GUI toolkit-specific font.
For wxPython, the returned trait accepts any of the following:
For wxPython, 'Arial 10'
Factory function for an "editor" that displays a multi-line string as interpreted HTML.
Factory function that returns a text editor for multi-line strings.
Factory function that returns an editor for passwords.
Returns a trait whose value is a Python property.
If no getter or setter functions are specified (and force is not True), it is assumed that they are defined elsewhere on the class whose attribute this trait is assigned to. For example:
class Bar(HasTraits):
foo = Property(Float)
# Shadow trait attribute
_foo = Float
def _set_foo(self,x):
self._foo = x
def _get_foo(self):
return self._foo
You can use the depends_on metadata attribute to indicate that the property depends on the value of another trait. The value of depends_on is an extended name specifier for traits that the property depends on. The property will a trait change notification if any of the traits specified by depends_on change. For example:
class Wheel ( Part ):
axle = Instanced( Axle )
position = Property( depends_on = 'axle.chassis.position' )
For details of the extended trait name syntax, refer to the on_trait_change() method of the HasTraits class.
For wxPython, the returned trait accepts any of the following values:
For wxPython, (0.0, 0.0, 0.0) (that is, white)
Factory function that returns a Python shell for editing Python values.
Creates a trait definition.
This function accepts a variety of forms of parameter lists:
| Format | Example | Description |
|---|---|---|
| Trait(default) | Trait(150.0) | The type of the trait is inferred from the type of the default value, which must be in ConstantTypes. |
| Trait(default, other1, other2, ...) | Trait(None, 0, 1, 2, 'many') | The trait accepts any of the enumerated values, with the first value being the default value. The values must be of types in ConstantTypes, but they need not be of the same type. The default value is not valid for assignment unless it is repeated later in the list. |
| Trait([default, other1, other2, ...]) | Trait([None, 0, 1, 2, 'many']) | Similar to the previous format, but takes an explicit list or a list variable. |
| Trait(type) | Trait(Int) | The type parameter must be a name of a Python type (see PythonTypes). Assigned values must be of exactly the specified type; no casting or coercion is performed. The default value is the appropriate form of zero, False, or emtpy string, set or sequence. |
| Trait(class) | class MyClass: pass foo = Trait( MyClass) |
Values must be instances of class or of a subclass of class. The default value is None, but None cannot be assigned as a value. |
| Trait(None, class) | class MyClass: pass foo = Trait( None, MyClass) |
Similar to the previous format, but None can be assigned as a value. |
| Trait(instance) | class MyClass: pass i = MyClass() foo = Trait(i) |
Values must be instances of the same class as instance, or of a subclass of that class. The specified instance is the default value. |
| Trait(handler) | Trait( TraitEnum ) | Assignment to this trait is validated by an object derived from enthought.traits.TraitHandler. |
| Trait(default, { type | constant | dict | class | function | handler | trait }+ ) | Trait(0.0, 0.0 'stuff', TupleType) | This is the most general form of the function. The notation: {...|...|...}+ means a list of one or more of any of the items listed between the braces. Thus, the most general form of the function consists of a default value, followed by one or more of several possible items. A trait defined by multiple items is called a "compound" trait. |
All forms of the Trait function accept both predefined and arbitrary keyword arguments. The value of each keyword argument becomes bound to the resulting trait object as the value of an attribute having the same name as the keyword. This feature lets you associate metadata with a trait.
The following predefined keywords are accepted:
Casts a CTrait, TraitFactory or TraitType to a CTrait but returns None if it is none of those.
Returns a trait derived from its input.
Attempts to cast a value to a trait. Returns either a trait or the original value.
© Copyright 2002-2008 Enthought, Inc.