API Reference for Enthought Tool Suite 2.7.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.
Callable values; default is None.
Callable = Trait(TraitCallable(), copy = 'ref')
CallableTypes = (FunctionType, MethodType)
Class values (old-style, i.e., type.ClassType) only; default value is None.
Class = Trait(ClassType)
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 ]
Only a dictionary of string:Any values can be assigned; only string keys can be inserted. The default value is {}.
DictStrAny = Dict(str, Any)
Only a dictionary of string:Boolean values can be assigned; only string keys with Boolean values can be inserted. The default value is {}.
DictStrBool = Dict(str, bool)
Only a dictionary of string:float values can be assigned; only string keys with float values can be inserted. The default value is {}.
DictStrFloat = Dict(str, float)
Only a dictionary of string:integer values can be assigned; only string keys with integer values can be inserted. The default value is {}.
DictStrInt = Dict(str, int)
Only a dictionary of string:list values can be assigned; only string keys with list values can be assigned. The default value is {}.
DictStrList = Dict(str, list)
Only a dictionary of string:long-integer values can be assigned; only string keys with long-integer values can be inserted. The default value is {}.
DictStrLong = Dict(str, long)
Only a dictionary of string:string values can be assigned; only string keys with string values can be inserted. The default value is {}.
DictStrStr = Dict(str, str)
Prevents any value from being assigned or read. That is, any attempt to get or set the value of the trait attribute raises an exception. This trait is most often used in conjunction with wildcard naming, for example, to catch spelling mistakes in attribute names. See the Traits User Manual for details on wildcards.
Disallow = CTrait(5)
Either(A,B,...,Z) allows any of the traits A,B,...,Z. ('Either' is grammatically imprecise, but it reads better than 'AnyOf' or 'OneOf'.)
Either = lambda args: Trait(None, *args)
Synonym for Bool; default value is False.
false = Bool
Function values only (i.e., types.FunctionType); default value is None.
Function = Trait(FunctionType)
Generic trait with 'object' behavior
generic_trait = CTrait(8)
HTMLTextEditor = None
List of Boolean values; default value is [].
ListBool = List(bool)
List of class values; default value is [].
ListClass = List(ClassType)
List of complex values; default value is [].
ListComplex = List(complex)
List of float values; default value is [].
ListFloat = List(float)
List of function values; default value is [].
ListFunction = List(FunctionType)
List of instance values; default value is [].
ListInstance = List(InstanceType)
List of integer values; default value is [].
ListInt = List(int)
List of method values; default value is [].
ListMethod = List(MethodType)
List of string values; default value is [].
ListStr = List(str)
List of container type values; default value is [].
ListThis = List(ThisClass)
List of Unicode string values; default value is [].
ListUnicode = List(unicode)
Method values only (i.e., types.MethodType); default value is None.
Method = Trait(MethodType)
Indicates that a parameter is missing from a type-checked method signature. Allows any value to be assigned; no type-checking is performed; default value is the singleton Missing object. See enthought.traits.has_traits.method().
missing = CTrait(0)
Module values only (i.e., types.ModuleType); default value is None.
Module = Trait(ModuleType)
MultilineTextEditor = None
MutableTypes = (list, dict)
PasswordEditor = None
This trait provides behavior identical to a standard Python attribute. That is, it allows any value to be assigned, and raises an ValueError if an attempt is made to get the value before one has been assigned. It has no default value. This trait is most often used in conjunction with wildcard naming. See the Traits User Manual for details on wildcards.
Python = TraitPython()
PythonShellEditor = None
PythonTypes = (StringType, UnicodeType, IntType, LongType, FloatType, ComplexType, ListType, TupleType, DictType, FunctionType, MethodType, ClassType, InstanceType, TypeType, NoneType)
This trait is write-once, and then read-only. The initial value of the attribute is the special, singleton object Undefined. The trait allows any value to be assigned to the attribute if the current value is the Undefined object. Once any other value is assigned, no further assignment is allowed. Normally, the initial assignment to the attribute is performed in the class constructor, based on information passed to the constructor. If the read-only value is known in advance of run time, use the Constant() function instead of ReadOnly to define the trait.
ReadOnly = CTrait(6)
Same as This; default value is the object containing the trait attribute defined using this trait..
self = Trait(Self, ThisClass)
SourceCodeEditor = None
SpecialNames = { Const('int') : CallFunc(Name('trait_factory'), [Name('Int')], None, None), Const('long') : CallFunc(Name('trait_factory'), [Name('Long')], None, None), Const('float') : CallFunc(Name('trait_factory'), [Name('Float')], None, None), Const('complex') : CallFunc(Name('trait_factory'), [Name('Complex')], None, None), Const('str') : CallFunc(Name('trait_factory'), [Name('Str')], None, None), Const('unicode') : CallFunc(Name('trait_factory'), [Name('Unicode')], None, None), Const('bool') : CallFunc(Name('trait_factory'), [Name('Bool')], None, None), Const('list') : CallFunc(Name('trait_factory'), [Name('List')], None, None), Const('tuple') : CallFunc(Name('trait_factory'), [Name('Tuple')], None, None), Const('dict') : CallFunc(Name('trait_factory'), [Name('Dict')], None, None) }
Allows only class values of the same class (or a subclass) as the object containing the trait attribute; default value is None.
This = Trait(ThisClass)
TraitTypes = (TraitHandler, CTrait)
Boolean values only; default value is True.
true = Bool(True)
Type values only (i.e., types.TypeType); default value is None.
Type = Trait(TypeType)
Allows any value to be assigned; no type-checking is performed. Default value is Undefined.
undefined = Any(Undefined)
Unpickles new-style objects.
Returns a trait that does no type-checking.
value or None
A trait attribute defined with this trait is different from a normal Python attribute, because if it is referenced before assignment, it returns value, rather than generating an error.
Returns a trait whose value must be a numpy array.
value or zeros(min(shape)), where min(shape) refers to the minimum shape allowed by the array. If shape is not specified, the minimum shape is (0,).
An Array trait allows only upcasting of assigned values that are already numpy arrays. It automatically casts tuples and lists of the right shape to the specified dtype (just like numpy's array does).
Returns a trait whose value must be a Boolean.
value or False
Returns a trait event whose editor is a button.
No default value because events do not store values.
Returns a trait whose value must be a numpy array, with casting allowed.
value or zeros(min(shape)), where min(shape) refers to the minimum shape allowed by the array. If shape is not specified, the minimum shape is (0,).
The trait returned by CArray() is similar to that returned by Array(), except that it allows both upcasting and downcasting of assigned values that are already numpy arrays. It automatically casts tuples and lists of the right shape to the specified dtype (just like numpy's array does).
Returns a trait whose value must be of a type that can be cast to a Boolean.
value or False
Returns a trait whose value must be able to be cast to a complex number.
value or 0.0+0.0j
Returns a trait whose value must be able to be cast to a floating point number.
value or 0.0
Returns a trait whose value must be able to be cast to an integer.
value or 0
Returns a trait whose value must be able to be cast to a long integer.
value or 0L
Returns a trait whose value must be a string, optionally of constrained length or matching a regular expression.
The trait returned by this function is indentical to that returned by String(), except that by default it uses a CodeEditor in TraitsUI views.
value or ''
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 complex number.
value or 0.0 + 0.0j
Returns a read-only trait whose value is value.
value
Traits of this type are very space efficient (and fast) because value is not stored in each instance using the trait, but only in the trait object itself. The value cannot be a list or dictionary, because those types have mutable values.
Returns a trait whose value must be able to be cast to a string.
value or ''
Returns a trait whose value must be able to be cast to a Unicode string.
value or u''
Creates a "delegator" trait, whose definition and default value are delegated to a delegate trait attribute on another object.
An object containing a delegator trait attribute must contain a second attribute that references the object containing the delegate trait attribute. The name of this second attribute is passed as the delegate argument to the Delegate() function.
The following rules govern the application of the prefix parameter:
If modify is True, then any changes to the delegator attribute are actually applied to the delegate attribute.
Returns a trait whose value must be a dictionary, optionally with specified types for keys and values.
value or {}
Returns a trait whose value must be the name of a directory.
value or ''
Returns a trait whose value must be one of an enumerated list.
values[0]
Returns a trait event whose assigned value must meet the specified criteria.
No default value because events do not store values.
Returns a trait whose value must be a valid Python expression.
value or '0'
The compiled form of a valid expression is stored as the mapped value of the trait.
Returns a trait whose value must be the name of a file.
value or ''
Returns a trait whose value must be a floating point number.
value or 0.0
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'
Returns a trait whose value must be a string.
The trait returned by this function is indentical to that returned by String(), except that by default it is parsed and displayed as HTML in TraitsUI views. The validation of the value does not enforce HTML syntax.
value or ''
Factory function for an "editor" that displays a multi-line string as interpreted HTML.
Returns a trait whose value must be an instance of a specified class, or one of its subclasses.
None if klass is an instance or if it is a class and args and kw are not specified. Otherwise, the default value is the instance obtained by calling klass(*args, **kw). Note that the constructor call is performed each time a default value is assigned, so each default value assigned is a unique instance.
Returns a trait whose value must be a plain integer.
value or 0
The length of the list assigned to the trait must be such that:
minlen <= len(list) <= maxlen
value or None
Returns a trait whose value must be a long integer.
value or 0L
Factory function that returns a text editor for multi-line strings.
Returns a trait whose value must be a string, optionally of constrained length or matching a regular expression.
The trait returned by this function is indentical to that returned by String(), except that by default it uses a PasswordEditor in TraitsUI views, which obscures text entered by the user.
value or ''
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, 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
Returns a trait whose value can be of any type, and whose editor is a Python shell.
value or None
Returns a trait whose numeric value must be in a specified range.
The low, high, and value arguments must be of the same type (integer or float).
value; if value is None or omitted, the default value is low, unless low is None or omitted, in which case the default value is high.
value or ''
Returns a trait whose value must be a GUI toolkit-specific RGB-based color.
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.
Returns a trait whose value must be a string.
value or ''
Returns a trait whose value must be a string, optionally of constrained length or matching a regular expression.
value or ''
Returns a trait even whose editor is a toolbar button.
No default value because events do not store 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 or TraitFactory to a CTrait but returns None if it is neither.
Returns a trait derived from its input.
Returns a trait that has standard Python behavior.
Returns a trait whose value must be a tuple of specified trait types.
- If no arguments are specified, the default value is ().
- If a tuple is specified as the first argument, it is the default value.
- If a tuple is not specified as the first argument, the default value is a tuple whose length is the length of the argument list, and whose values are the default values for the corresponding trait types.
Example for case #2:
mytuple = Tuple(('Fred', 'Betty', 5))
The trait's value must be a 3-element tuple whose first and second elements are strings, and whose third element is an integer. The default value is ('Fred', 'Betty', 5).
Example for case #3:
mytuple = Tuple('Fred', 'Betty', 5)
The trait's value must be a 3-element tuple whose first and second elements are strings, and whose third element is an integer. The default value is ('','',0).
Returns a trait event whose editor is a button that opens debugger window.
No default value because events do not store values.
Returns a trait whose value must be a Unicode string.
value or u''
Returns a trait whose values must be instances of the same type (or a subclass) of the specified klass, which can be a class or an instance.
Only a weak reference is maintained to any object assigned to a WeakRef trait. If no other references exist to the assigned value, the value may be garbage collected, in which case the value of the trait becomes None. In all other cases, the value returned by the trait is the original object.
None (even if allow_none==False)
Copyright © 2002-2008 Enthought, Inc.