traits.trait_types
Module¶
Core Trait definitions.
Traits¶
-
class
traits.trait_types.
Any
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, *, factory=None, args=(), kw={}, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value can be anything.
- Parameters
default_value (object, optional) –
The default value for the trait. If this is an instance of either
list
ordict
then a copy of the default value is made for each instance. Otherwise, the default is shared between all instances.Deprecated since version 6.3.0: In a future version of Traits, a
list
ordict
default value will no longer be copied. If you need a per-instance default, use a_trait_name_default
method to supply that default.factory (callable, optional) – A callable, that when called with args and kw, returns the default value for the trait.
args (tuple, optional) – Positional arguments (if any) for generating the default value.
kw (dictionary, optional) – Keyword arguments (if any) for generating the default value.
**metadata – Metadata for the trait.
-
default_value
= None¶ The default value for the trait:
-
info_text
= 'any value'¶ A description of the type of value this trait accepts:
-
default_value_type
= 0¶ The default value type to use.
-
class
traits.trait_types.
BaseInt
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be an int.
Values which support the Python index protocol will validate and will be converted to the corresponding int value.
-
default_value_type
= 0¶ The default value type to use.
-
default_value
= 0¶ The default value for the trait:
-
info_text
= 'an integer'¶ A description of the type of value this trait accepts:
-
-
class
traits.trait_types.
Int
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseInt
A fast-validating trait type whose value must be an integer.
Values which support the Python index protocol will validate and will be converted to the corresponding int value.
-
fast_validate
= (<ValidateTrait.int: 20>,)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
BaseFloat
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a float.
Values which support automatic conversion to floats via the Python __float__ method will validate and will be converted to the corresponding float value.
-
default_value_type
= 0¶ The default value type to use.
-
default_value
= 0.0¶ The default value for the trait:
-
info_text
= 'a float'¶ A description of the type of value this trait accepts:
-
-
class
traits.trait_types.
Float
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseFloat
A fast-validating trait type whose value must be a float.
Values which support automatic conversion to floats via the Python __float__ method will validate and will be converted to the corresponding float value.
-
fast_validate
= (<ValidateTrait.float: 21>,)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
BaseComplex
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a complex number.
Integers and floating-point numbers will be converted to the corresponding complex value.
-
default_value_type
= 0¶ The default value type to use.
-
default_value
= 0j¶ The default value for the trait:
-
info_text
= 'a complex number'¶ A description of the type of value this trait accepts:
-
-
class
traits.trait_types.
Complex
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseComplex
A fast-validating trait type whose value must be a complex number.
Integers and floating-point numbers will be converted to the corresponding complex value.
-
fast_validate
= (<ValidateTrait.complex_number: 23>,)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
BaseStr
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a string.
-
default_value_type
= 0¶ The default value type to use.
-
default_value
= ''¶ The default value for the trait:
-
info_text
= 'a string'¶ A description of the type of value this trait accepts:
-
-
class
traits.trait_types.
Str
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseStr
A fast-validating trait type whose value must be a string.
-
fast_validate
= (<ValidateTrait.coerce: 11>, <class 'str'>)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
Title
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.Str
A Str trait which by default uses a TraitsUI TitleEditor.
-
class
traits.trait_types.
BaseBytes
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a bytestring.
-
default_value_type
= 0¶ The default value type to use.
-
default_value
= b''¶ The default value for the trait:
-
info_text
= 'a bytes string'¶ A description of the type of value this trait accepts:
-
encoding
= None¶ An encoding to use with TraitsUI editors
-
-
class
traits.trait_types.
Bytes
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseBytes
A fast-validating trait type whose value must be a bytestring.
-
fast_validate
= (<ValidateTrait.coerce: 11>, <class 'bytes'>)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
BaseBool
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a bool.
-
default_value_type
= 0¶ The default value type to use.
-
default_value
= False¶ The default value for the trait:
-
info_text
= 'a boolean'¶ A description of the type of value this trait accepts:
-
-
class
traits.trait_types.
Bool
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseBool
A fast-validating trait type whose value must be a bool.
-
fast_validate
= (<ValidateTrait.coerce: 11>, <class 'bool'>, None, <class 'numpy.bool_'>)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
BaseCInt
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseInt
A coercing trait type whose value is an integer.
-
class
traits.trait_types.
CInt
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseCInt
A fast-validating, coercing trait type whose value is an int.
-
fast_validate
= (<ValidateTrait.cast: 12>, <class 'int'>)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
BaseCFloat
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseFloat
A coercing trait type whose value is a float.
-
class
traits.trait_types.
CFloat
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseCFloat
A fast-validating, coercing trait type whose value is a float.
-
fast_validate
= (<ValidateTrait.cast: 12>, <class 'float'>)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
BaseCComplex
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseComplex
A coercing trait type whose value is a complex number.
-
class
traits.trait_types.
CComplex
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseCComplex
A fast-validating, coercing trait type whose value is a complex number.
-
fast_validate
= (<ValidateTrait.cast: 12>, <class 'complex'>)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
BaseCStr
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseStr
A coercing trait type whose value is a string.
-
class
traits.trait_types.
CStr
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseCStr
A fast-validating, coercing trait type whose value is a string.
-
fast_validate
= (<ValidateTrait.cast: 12>, <class 'str'>)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
BaseCBytes
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseBytes
A coercing trait type whose value is a bytestring.
-
class
traits.trait_types.
CBytes
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseCBytes
A fast-validating, coercing trait type whose value is a bytestring.
-
fast_validate
= (<ValidateTrait.cast: 12>, <class 'bytes'>)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
BaseCBool
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseBool
A coercing trait type whose value is a bool.
-
class
traits.trait_types.
CBool
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_types.BaseCBool
A fast-validating, coercing trait type whose value is a bool.
-
fast_validate
= (<ValidateTrait.cast: 12>, <class 'bool'>)¶ The C-level fast validator to use:
-
-
class
traits.trait_types.
String
(value='', minlen=0, maxlen=9223372036854775807, regex='', **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a string with optional constraints.
The value is a string whose length is in a specified range, and which optionally matches a specified regular expression.
- Parameters
-
minlen
¶ The minimum length allowed for the string.
- Type
integer
-
maxlen
¶ The maximum length allowed for the string.
- Type
integer
-
default_value_type
= 0¶ The default value type to use.
-
validate_all
(object, name, value)[source]¶ Validates that the value is a valid string in the specified length range which matches the specified regular expression.
-
validate_len
(object, name, value)[source]¶ Validates that the value is a valid string in the specified length range.
-
class
traits.trait_types.
Regex
(value='', regex='.*', **metadata)[source]¶ Bases:
traits.trait_types.String
A trait type whose value must match a regular expression.
-
class
traits.trait_types.
Code
(value='', minlen=0, maxlen=9223372036854775807, regex='', **metadata)[source]¶ Bases:
traits.trait_types.String
A trait type whose value holds a string of source code.
Validation does not perform any sort of syntax checking. The default TraitsUI editor is a CodeEditor.
-
metadata
= {'editor': <function code_editor>}¶ The standard metadata for the trait:
-
-
class
traits.trait_types.
HTML
(value='', minlen=0, maxlen=9223372036854775807, regex='', **metadata)[source]¶ Bases:
traits.trait_types.String
A trait type whose value holds an HTML string.
The validation of the value does not enforce HTML syntax. The default TraitsUI editor is an HTMLEditor.
-
metadata
= {'editor': <function html_editor>}¶ The standard metadata for the trait:
-
-
class
traits.trait_types.
Password
(value='', minlen=0, maxlen=9223372036854775807, regex='', **metadata)[source]¶ Bases:
traits.trait_types.String
A trait type whose value holds a password string.
The default TraitsUI editor is an PasswordEditor, which obscures text entered by the user.
-
metadata
= {'editor': <function password_editor>}¶ The standard metadata for the trait:
-
-
class
traits.trait_types.
BaseCallable
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a Python callable.
-
metadata
= {'copy': 'ref'}¶ The standard metadata for the trait:
-
default_value_type
= 0¶ The default value type to use.
-
default_value
= None¶ The default value for the trait:
-
info_text
= 'a callable value'¶ A description of the type of value this trait accepts:
-
-
class
traits.trait_types.
Callable
(value=None, allow_none=True, **metadata)[source]¶ Bases:
traits.trait_types.BaseCallable
A fast-validating trait type whose value must be a Python callable.
-
class
traits.trait_types.
BaseType
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be an instance of a Python type.
This is an abstract class and should not be directly instantiated.
-
default_value_type
= 0¶ The default value type to use.
-
-
class
traits.trait_types.
This
(value=None, allow_none=True, **metadata)[source]¶ Bases:
traits.trait_types.BaseType
A trait type whose value must be an instance of the defining class.
-
default_value_type
= 0¶ The default value type to use.
-
info_text
= 'an instance of the same type as the receiver'¶ A description of the type of value this trait accepts:
-
fast_validate
= (<ValidateTrait.self_type: 2>,)¶ The C-level fast validator to use:
-
info
()[source]¶ Must return a string describing the type of value accepted by the trait handler.
The string should be a phrase describing the type defined by the TraitHandler subclass, rather than a complete sentence. For example, use the phrase, “a square sprocket” instead of the sentence, “The value must be a square sprocket.” The value returned by info() is combined with other information whenever an error occurs and therefore makes more sense to the user if the result is a phrase. The info() method is similar in purpose and use to the info attribute of a validator function.
Note that the result can include information specific to the particular trait handler instance. If the info() method is not overridden, the default method returns the value of the ‘info_text’ attribute.
-
-
class
traits.trait_types.
self
(value=None, allow_none=True, **metadata)[source]¶ Bases:
traits.trait_types.This
A trait type whose default value is the object containing the trait.
The trait can be assigned to, but any new value must be an instance of the defining class.
-
default_value_type
= 2¶ The default value type to use (i.e. ‘self’):
-
-
class
traits.trait_types.
Function
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a function.
Deprecated since version 6.2.0: This trait type explicitly checks for an instance of
types.FunctionType
. For the majority of use cases, the more generalCallable
trait type should be used instead. If an instance specifically oftypes.FunctionType
really is needed, one can useInstance(types.FunctionType)
.-
default_value_type
= 0¶ The default value type to use.
-
default_value
= <undefined>¶ The default value for the trait type.
-
fast_validate
= (<ValidateTrait.coerce: 11>, <class 'function'>)¶ The C-level fast validator to use:
-
info_text
= 'a function'¶ A description of the type of value this trait accepts:
-
-
class
traits.trait_types.
Method
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a method.
Deprecated since version 6.2.0: This trait type explicitly checks for an instance of
types.MethodType
. For the majority of use cases, the more generalCallable
trait type should be used instead. If an instance specifically oftypes.MethodType
really is needed, one can useInstance(types.MethodType)
.-
default_value_type
= 0¶ The default value type to use.
-
default_value
= <undefined>¶ The default value for the trait type.
-
fast_validate
= (<ValidateTrait.coerce: 11>, <class 'method'>)¶ The C-level fast validator to use:
-
info_text
= 'a method'¶ A description of the type of value this trait accepts:
-
-
class
traits.trait_types.
Module
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a module.
-
default_value_type
= 0¶ The default value type to use.
-
default_value
= <undefined>¶ The default value for the trait type.
-
fast_validate
= (<ValidateTrait.coerce: 11>, <class 'module'>)¶ The C-level fast validator to use:
-
info_text
= 'a module'¶ A description of the type of value this trait accepts:
-
-
class
traits.trait_types.
Python
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type that behaves as a standard Python attribute.
This trait type 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.
-
metadata
= {'type': 'python'}¶ The standard metadata for the trait:
-
default_value_type
= 0¶ The default value type to use.
-
default_value
= <undefined>¶ The default value for the trait:
-
-
class
traits.trait_types.
Constant
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value is a constant.
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.
- Parameters
value (any) – The constant value for the trait.
**metadata – Trait metadata for the trait.
-
default_value_type
= 0¶ The default value type to use.
-
ctrait_type
= 7¶ Defines the CTrait type to use for this trait:
-
metadata
= {'transient': True, 'type': 'constant'}¶ The standard metadata for the trait:
-
class
traits.trait_types.
Delegate
(delegate, prefix='', modify=False, listenable=True, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value is delegated to a trait on another object.
This is a base class that shouldn’t be used directly, rather use one of the subclasses DelegatesTo or PrototypesFrom, depending on desired behaviour.
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.
The following rules govern the application of the prefix parameter:
If prefix is empty or omitted, the delegation is to an attribute of the delegate object with the same name as the delegator attribute.
If prefix is a valid Python attribute name, then the delegation is to an attribute whose name is the value of prefix.
If prefix ends with an asterisk (‘*’) and is longer than one character, then the delegation is to an attribute whose name is the value of prefix, minus the trailing asterisk, prepended to the delegator attribute name.
If prefix is equal to a single asterisk, the delegation is to an attribute whose name is the value of the delegator object’s __prefix__ attribute prepended to delegator attribute name.
- Parameters
delegate (str) – The name of the trait that holds the HasTraits instance that the value is delegated to.
prefix (str) – The name of the trait on the delegate that holds the delegated value. If empty, then the name of this trait will be used.
modify (bool) – Whether modifications of this trait are applied to the delegated object. This differentiates the behaviour of DelegatesTo and PrototypedFrom.
listenable (bool) – Whether changes to the delegated trait will fire listeners to this trait.
-
delegate
¶ The name of the trait that holds the HasTraits instance that the value is delegated to.
- Type
-
prefix
¶ The name of the trait on the delegate that holds the delegated value. If empty, then the name of this trait will be used.
- Type
-
modify
¶ Whether modifications of this trait are applied to the delegated object. This differentiates the behaviour of DelegatesTo and PrototypedFrom.
- Type
-
ctrait_type
= 3¶ Defines the CTrait type to use for this trait:
-
default_value_type
= 0¶ The default value type to use.
-
metadata
= {'transient': False, 'type': 'delegate'}¶ The standard metadata for the trait:
-
class
traits.trait_types.
DelegatesTo
(delegate, prefix='', listenable=True, **metadata)[source]¶ Bases:
traits.trait_types.Delegate
A trait type that matches the ‘delegate’ design pattern.
This defines a trait whose value and definition is “delegated” to another trait on a different 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 DelegatesTo() function.
The following rules govern the application of the prefix parameter:
If prefix is empty or omitted, the delegation is to an attribute of the delegate object with the same name as the delegator attribute.
If prefix is a valid Python attribute name, then the delegation is to an attribute whose name is the value of prefix.
If prefix ends with an asterisk (‘*’) and is longer than one character, then the delegation is to an attribute whose name is the value of prefix, minus the trailing asterisk, prepended to the delegator attribute name.
If prefix is equal to a single asterisk, the delegation is to an attribute whose name is the value of the delegator object’s __prefix__ attribute prepended to delegator attribute name.
Note that any changes to the delegator attribute are actually applied to the corresponding attribute on the delegate object. The original object containing the delegator trait is not modified.
- Parameters
delegate (str) – Name of the attribute on the current object which references the object that is the trait’s delegate.
prefix (str) – A prefix or substitution applied to the original attribute when looking up the delegated attribute.
listenable (bool) – Indicates whether a listener can be attached to this attribute such that changes to the delegated attribute will trigger it.
**metadata – Trait metadata for the trait.
-
class
traits.trait_types.
PrototypedFrom
(prototype, prefix='', listenable=True, **metadata)[source]¶ Bases:
traits.trait_types.Delegate
A trait type that matches the ‘prototype’ design pattern.
This defines a trait whose default value and definition is “prototyped” from another trait on a different object.
An object containing a prototyped trait attribute must contain a second attribute that references the object containing the prototype trait attribute. The name of this second attribute is passed as the prototype argument to the PrototypedFrom() function.
The following rules govern the application of the prefix parameter:
If prefix is empty or omitted, the prototype delegation is to an attribute of the prototype object with the same name as the prototyped attribute.
If prefix is a valid Python attribute name, then the prototype delegation is to an attribute whose name is the value of prefix.
If prefix ends with an asterisk (‘*’) and is longer than one character, then the prototype delegation is to an attribute whose name is the value of prefix, minus the trailing asterisk, prepended to the prototyped attribute name.
If prefix is equal to a single asterisk, the prototype delegation is to an attribute whose name is the value of the prototype object’s __prefix__ attribute prepended to the prototyped attribute name.
Note that any changes to the prototyped attribute are made to the original object, not the prototype object. The prototype object is only used to define to trait type and default value.
- Parameters
prototype (str) – Name of the attribute on the current object which references the object that is the trait’s prototype.
prefix (str) – A prefix or substitution applied to the original attribute when looking up the prototyped attribute.
listenable (bool) – Indicates whether a listener can be attached to this attribute such that changes to the corresponding attribute on the prototype object will trigger it.
**metadata – Trait metadata for the trait.
-
class
traits.trait_types.
Expression
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a valid Python expression.
The compiled form of a valid expression is stored as the mapped value of the trait.
-
default_value_type
= 0¶ The default value type to use.
-
default_value
= '0'¶ The default value for the trait:
-
info_text
= 'a valid Python expression'¶ A description of the type of value this trait accepts:
-
is_mapped
= True¶ Indicate that this is a mapped trait:
-
-
class
traits.trait_types.
PythonValue
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, *, factory=None, args=(), kw={}, **metadata)[source]¶ Bases:
traits.trait_types.Any
A trait type whose value can be of any type.
The default editor is a ShellEditor.
-
metadata
= {'editor': <function shell_editor>}¶ The standard metadata for the trait:
-
-
class
traits.trait_types.
BaseFile
(value='', filter=None, auto_set=False, entries=0, exists=False, **metadata)[source]¶ Bases:
traits.trait_types.BaseStr
A trait type whose value must be a file path string.
This will accept both strings and os.pathlib Path objects, converting the latter to the corresponding string value.
- Parameters
value (str) – The default value for the trait.
filter (str) – A wildcard string to filter filenames in the file dialog box used by the attribute trait editor.
auto_set (bool) – Indicates whether the file editor updates the trait value after every key stroke.
entries (int) – A hint to the TraitsUI editor about how many values to display in the editor.
exists (bool) – Indicates whether the trait value must be an existing file or not.
-
filter
¶ A wildcard string to filter filenames in the file dialog box used by the attribute trait editor.
- Type
-
auto_set
¶ Indicates whether the file editor updates the trait value after every key stroke.
- Type
-
class
traits.trait_types.
File
(value='', filter=None, auto_set=False, entries=0, exists=False, **metadata)[source]¶ Bases:
traits.trait_types.BaseFile
A fast-validating trait type whose value must be a file path string.
This will accept both strings and os.pathlib Path objects, converting the latter to the corresponding string value.
- Parameters
value (str) – The default value for the trait.
filter (str) – A wildcard string to filter filenames in the file dialog box used by the attribute trait editor.
auto_set (bool) – Indicates whether the file editor updates the trait value after every key stroke.
entries (int) – A hint to the TraitsUI editor about how many values to display in the editor.
exists (bool) – Indicates whether the trait value must be an existing file or not.
-
filter
¶ A wildcard string to filter filenames in the file dialog box used by the attribute trait editor.
- Type
-
auto_set
¶ Indicates whether the file editor updates the trait value after every key stroke.
- Type
-
class
traits.trait_types.
BaseDirectory
(value='', auto_set=False, entries=0, exists=False, **metadata)[source]¶ Bases:
traits.trait_types.BaseStr
A trait type whose value must be a directory path string.
This also accepts objects implementing the
os.PathLike
interface, converting them to the corresponding string.- Parameters
value (str) – The default value for the trait.
auto_set (bool) – Indicates whether the directory editor updates the trait value after every key stroke.
entries (int) – A hint to the TraitsUI editor about how many values to display in the editor.
exists (bool) – Indicates whether the trait value must be an existing directory or not.
-
auto_set
¶ Indicates whether the directory editor updates the trait value after every key stroke.
- Type
-
class
traits.trait_types.
Directory
(value='', auto_set=False, entries=0, exists=False, **metadata)[source]¶ Bases:
traits.trait_types.BaseDirectory
A fast-validating trait type whose value is a directory path string.
This also accepts objects implementing the
os.PathLike
interface, converting them to the corresponding string.- Parameters
value (str) – The default value for the trait.
auto_set (bool) – Indicates whether the directory editor updates the trait value after every key stroke.
entries (int) – A hint to the TraitsUI editor about how many values to display in the editor.
exists (bool) – Indicates whether the trait value must be an existing directory or not.
-
auto_set
¶ Indicates whether the directory editor updates the trait value after every key stroke.
- Type
-
class
traits.trait_types.
BaseRange
(low=None, high=None, value=None, exclude_low=False, exclude_high=False, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose numeric value lies inside a range.
The value held will be either an integer or a float, which type is determined by whether the low, high and value arguments are integers or floats.
The low, high, and value arguments must be of the same type (integer or float), except in the case where either low or high is a string (i.e. extended trait name).
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.
- Parameters
low (integer, float or string (i.e. extended trait name)) – The low end of the range.
high (integer, float or string (i.e. extended trait name)) – The high end of the range.
value (integer, float or string (i.e. extended trait name)) – The default value of the trait.
exclude_low (bool) – Indicates whether the low end of the range is exclusive.
exclude_high (bool) – Indicates whether the high end of the range is exclusive.
-
default_value_type
= 0¶ The default value type to use.
-
init_fast_validate
(*args)[source]¶ Does nothing for the BaseRange class. Used in the Range class to set up the fast validator.
-
float_validate
(object, name, value)[source]¶ Validate that the value is a float value in the specified range.
-
class
traits.trait_types.
Range
(low=None, high=None, value=None, exclude_low=False, exclude_high=False, **metadata)[source]¶ Bases:
traits.trait_types.BaseRange
A fast-validating trait type whose numeric value lies inside a range.
-
class
traits.trait_types.
BaseEnum
(*args, values=None, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value is an element of a finite collection.
This trait type can be either static, with the collection of valid values specified directly in the constructor, or dynamic, with the collection provided by the value of another trait attribute.
For both static and dynamic enumerations, a default value can be provided as a positional argument. If no default is provided, the default is the first item (in iteration order) of the underlying collection.
Notes
If the enumeration is based on an unordered collection like a
set
, and no explicit default is given, the default used will effectively be arbitrary (the first element of the set in iteration order). It’s recommended that a default be given explicitly in this case.Instances of
str
,bytes
andbytearray
are not treated as collections for the purposes of this trait type, both for pragmatic reasons (it’s more likely that a user wants to use a string as an element in a collection than as a collection in its own right), and because the behavior of thein
operator for those types does not express the usual membership semantics (for example,"bc" in "abc"
isTrue
).
- Parameters
*args –
The enumeration of all valid values for the trait. For a static enumeration trait (where the values keyword argument is not given) the supported signatures for
args
are as follows:- (collection,)
A nonempty collection of valid values. The default is the first element of the collection, in iteration order.
- (default, collection)
The default value, followed by a nonempty collection of valid values. The default should be an element of the collection, but this is not checked.
- (item1, item2, …, itemn)
One or more items giving the valid values for the collection. The default is item1.
For a dynamic enumeration trait, where the values keyword argument is given, the supported signatures for
args
are:- ()
No arguments given. In this case the default is the first item of the collection, in iteration order.
- (default,)
The default value for the collection.
For the static case, the ambiguity in the signatures is resolved as follows: if
args
has length1
or2
,args[-1]
can be iterated over, andargs[-1]
is not an instance ofstr
,bytes
orbytearray
, thenargs[-1]
is assumed to give the collection of values. Otherwise, all elements ofargs
are assumed to be items in the collection. Thus the first two signatures are safe from ambiguity, and it’s recommended to use one of these two signatures in preference to the third form.values (str, optional) – The name of a trait holding the valid values. If given, this is a dynamic enumeration, otherwise it’s a static numeration.
**metadata – Metadata for the trait.
-
values
¶ For a static enumeration, this is a tuple holding the valid values. For a dynamic enumeration, this is None.
-
name
¶ For a dynamic enumeration, this is the name of a trait holding the collection of valid values. For a static enumeration, this is None.
-
default_value_type
= 0¶ The default value type to use.
-
init_fast_validate
(*args)[source]¶ Does nothing for the BaseEnum class. Used in the Enum class to set up the fast validator.
-
class
traits.trait_types.
Enum
(*args, values=None, **metadata)[source]¶ Bases:
traits.trait_types.BaseEnum
A fast-validating trait type whose value is an element of a finite collection.
This trait type can be either static, with the collection of valid values specified directly in the constructor, or dynamic, with the collection provided by the value of another trait attribute.
For both static and dynamic enumerations, a default value can be provided as a positional argument. If no default is provided, the default is the first item (in iteration order) of the underlying collection.
Notes
If the enumeration is based on an unordered collection like a
set
, and no explicit default is given, the default used will effectively be arbitrary (the first element of the set in iteration order). It’s recommended that a default be given explicitly in this case.Instances of
str
,bytes
andbytearray
are not treated as collections for the purposes of this trait type, both for pragmatic reasons (it’s more likely that a user wants to use a string as an element in a collection than as a collection in its own right), and because the behavior of thein
operator for those types does not express the usual membership semantics (for example,"bc" in "abc"
isTrue
).
- Parameters
*args –
The enumeration of all valid values for the trait. For a static enumeration trait (where the values keyword argument is not given) the supported signatures for
args
are as follows:- (collection,)
A nonempty collection of valid values. The default is the first element of the collection, in iteration order.
- (default, collection)
The default value, followed by a nonempty collection of valid values. The default should be an element of the collection, but this is not checked.
- (item1, item2, …, itemn)
One or more items giving the valid values for the collection. The default is item1.
For a dynamic enumeration trait, where the values keyword argument is given, the supported signatures for
args
are:- ()
No arguments given. In this case the default is the first item of the collection, in iteration order.
- (default,)
The default value for the collection.
For the static case, the ambiguity in the signatures is resolved as follows: if
args
has length1
or2
,args[-1]
can be iterated over, andargs[-1]
is not an instance ofstr
,bytes
orbytearray
, thenargs[-1]
is assumed to give the collection of values. Otherwise, all elements ofargs
are assumed to be items in the collection. Thus the first two signatures are safe from ambiguity, and it’s recommended to use one of these two signatures in preference to the third form.values (str, optional) – The name of a trait holding the valid values. If given, this is a dynamic enumeration, otherwise it’s a static numeration.
**metadata – Metadata for the trait.
-
values
¶ For a static enumeration, this is a tuple holding the valid values. For a dynamic enumeration, this is None.
-
name
¶ For a dynamic enumeration, this is the name of a trait holding the collection of valid values. For a static enumeration, this is None.
-
class
traits.trait_types.
BaseTuple
(*types, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type holding a tuple with typed elements.
The default value is determined as follows:
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)
.- Parameters
*types – Definition of the default and allowed tuples. If the first item of types is a tuple, it is used as the default value. The remaining argument list is used to form a tuple that constrains the values assigned to the returned trait. The trait’s value must be a tuple of the same length as the remaining argument list, whose elements must match the types specified by the corresponding items of the remaining argument list.
**metadata – Trait metadata for the trait.
-
default_value_type
= 0¶ The default value type to use.
-
class
traits.trait_types.
Tuple
(*types, **metadata)[source]¶ Bases:
traits.trait_types.BaseTuple
A fast-validating trait type holding a tuple with typed elements.
-
class
traits.trait_types.
ValidatedTuple
(*types, **metadata)[source]¶ Bases:
traits.trait_types.BaseTuple
A trait type holding a tuple with customized validation.
- Parameters
*types – Definition of the default and allowed tuples. (see
BaseTuple
for more details)fvalidate (callable, optional) – A callable to provide the additional custom validation for the tuple. The callable will be passed the tuple value and should return True or False.
fvalidate_info (string, optional) – A string describing the custom validation to use for the error messages.
**metadata – Trait metadata for the trait.
Example
The definition:
value_range = ValidatedTuple( Int(0), Int(1), fvalidate=lambda x: x[0] < x[1])
will accept only tuples
(a, b)
containing two integers that satisfya < b
.
-
class
traits.trait_types.
List
(trait=None, value=None, minlen=0, maxlen=9223372036854775807, items=True, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type for a list of values of the specified type.
The length of the list assigned to the trait must be such that:
minlen <= len(list) <= maxlen
Note that this trait type creates copies of values on assignment, rather than assigning the exact instance. For example, consider:
>>> class A(HasTraits): ... x = List() ... >>> b = [1, 2, 3] >>> a = A(x=b) >>> a.x [1, 2, 3] >>> b.append(4) >>> a.x [1, 2, 3]
- Parameters
trait (a trait or value that can be converted using trait_from()) – The type of item that the list contains. If not specified, the list can contain items of any type.
value (list) – Default value for the list.
minlen (integer) – The minimum length of a list that can be assigned to the trait.
maxlen (integer) – The maximum length of a list that can be assigned to the trait.
items (bool) – Whether there is a corresponding <name>_items trait.
**metadata – Trait metadata for the trait.
-
item_trait
¶ The type of item that the list contains.
- Type
trait
-
minlen
¶ The minimum length of a list that can be assigned to the trait.
- Type
integer
-
maxlen
¶ The maximum length of a list that can be assigned to the trait.
- Type
integer
-
class
traits.trait_types.
CList
(trait=None, value=None, minlen=0, maxlen=9223372036854775807, items=True, **metadata)[source]¶ Bases:
traits.trait_types.List
A coercing trait type for a list of values of the specified type.
-
class
traits.trait_types.
PrefixList
(values, *, default_value=None, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
- Ensures that a value assigned to the attribute is a member of a list of
specified string values, or is a unique prefix of one of those values.
The values that can be assigned to a trait attribute of type PrefixList type is the set of all strings supplied to the PrefixList constructor, as well as any unique prefix of those strings. That is, if the set of strings supplied to the constructor is described by [s1, s2, …, sn], then the string v is a valid value for the trait if v == si[:j] for one and only one pair of values (i, j). If v is a valid value, then the actual value assigned to the trait attribute is the corresponding si value that v matched.
The legal values can be provided as an iterable of values.
Example
- ::
- class Person(HasTraits):
married = PrefixList([‘yes’, ‘no’])
The Person class has a married trait that accepts any of the strings ‘y’, ‘ye’, ‘yes’, ‘n’, or ‘no’ as valid values. However, the actual values assigned as the value of the trait attribute are limited to either ‘yes’ or ‘no’. That is, if the value ‘y’ is assigned to the married attribute, the actual value assigned will be ‘yes’.
- Parameters
values – A list or other iterable of legal string values for this trait.
-
values
¶ The list of legal values for this trait.
- Type
list of str
-
default_value_type
= 0¶ The default value type to use.
-
info
()[source]¶ Must return a string describing the type of value accepted by the trait handler.
The string should be a phrase describing the type defined by the TraitHandler subclass, rather than a complete sentence. For example, use the phrase, “a square sprocket” instead of the sentence, “The value must be a square sprocket.” The value returned by info() is combined with other information whenever an error occurs and therefore makes more sense to the user if the result is a phrase. The info() method is similar in purpose and use to the info attribute of a validator function.
Note that the result can include information specific to the particular trait handler instance. If the info() method is not overridden, the default method returns the value of the ‘info_text’ attribute.
-
class
traits.trait_types.
Set
(trait=None, value=None, items=True, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type for a set of values of the specified type.
Note that this trait type creates copies of values on assignment, rather than assigning the exact instance. For example, consider:
>>> class A(HasTraits): ... x = Set() ... >>> b = set() >>> a = A(x=b) >>> a.x TraitSetObject() >>> b.add(1) >>> a.x TraitSetObject()
- Parameters
trait (a trait or value that can be converted using trait_from()) – The type of item that the set contains. If not specified, the set can contain items of any type.
value (set) – Default value for the set.
items (bool) – Whether there is a corresponding <name>_items trait.
**metadata – Trait metadata for the trait.
-
item_trait
¶ The type of item that the set contains. If not specified, the set can contain items of any type.
- Type
a trait or value that can be converted to a trait
-
class
traits.trait_types.
CSet
(trait=None, value=None, items=True, **metadata)[source]¶ Bases:
traits.trait_types.Set
A coercing trait type for a set of values of the specified type.
-
class
traits.trait_types.
Dict
(key_trait=None, value_trait=None, value=None, items=True, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type for a dictionary with specified key and value types.
Note that this trait type creates copies of values on assignment, rather than assigning the exact instance. For example, consider:
>>> class A(HasTraits): ... x = Dict() ... >>> b = {} >>> a = A(x=b) >>> a.x {} >>> b['one'] = 1 >>> a.x {}
- Parameters
key_trait (a trait or value that can be converted using trait_from()) – The trait type for keys in the dictionary; if not specified, any values can be used as keys.
value_trait (a trait or value that can be converted using trait_from()) – The trait type for values in the dictionary; if not specified, any values can be used as dictionary values.
value (dict) – The default value for the returned trait.
items (bool) – Indicates whether the value contains items.
-
key_trait
¶ The trait type for keys in the dictionary; if not specified, any values can be used as keys.
- Type
a trait
-
value_trait
¶ The trait type for values in the dictionary; if not specified, any values can be used as dictionary values.
- Type
a trait
-
value_trait_handler
¶ The TraitHandler for the value_trait.
- Type
-
class
traits.trait_types.
Map
(map, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
Checks that the value assigned to a trait attribute is a key of a specified dictionary, and also assigns the dictionary value corresponding to that key to a shadow attribute.
A trait attribute of type Map is called a mapped trait attribute. In practice, this means that the resulting object actually contains two attributes: one whose value is a key of the Map dictionary, and the other whose value is the corresponding value of the Map dictionary. The name of the shadow attribute is simply the base attribute name with an underscore (‘_’) appended. Mapped trait attributes can be used to allow a variety of user-friendly input values to be mapped to a set of internal, program-friendly values.
Example
The following example defines a
Person
class:>>> class Person(HasTraits): ... married = Map({'yes': 1, 'no': 0 }, default_value="yes") ... >>> bob = Person() >>> print(bob.married) yes >>> print(bob.married_) 1
In this example, the default value of the
married
attribute of the Person class is ‘yes’. Because this attribute is defined using Map, instances of Person have another attribute,married_
, whose default value is 1, the dictionary value corresponding to the key ‘yes’.- Parameters
map (dict) – A dictionary whose keys are valid values for the trait attribute, and whose corresponding values are the values for the shadow trait attribute.
default_value (object, optional) – The default value for the trait. If given, this should be a key from the mapping. If not given, the first key from the mapping (in normal dictionary iteration order) will be used as the default.
-
map
¶ A dictionary whose keys are valid values for the trait attribute, and whose corresponding values are the values for the shadow trait attribute.
- Type
-
default_value_type
= 0¶ The default value type to use.
-
info
()[source]¶ Must return a string describing the type of value accepted by the trait handler.
The string should be a phrase describing the type defined by the TraitHandler subclass, rather than a complete sentence. For example, use the phrase, “a square sprocket” instead of the sentence, “The value must be a square sprocket.” The value returned by info() is combined with other information whenever an error occurs and therefore makes more sense to the user if the result is a phrase. The info() method is similar in purpose and use to the info attribute of a validator function.
Note that the result can include information specific to the particular trait handler instance. If the info() method is not overridden, the default method returns the value of the ‘info_text’ attribute.
-
get_editor
(trait)[source]¶ Returns a trait editor that allows the user to modify the trait trait. This method only needs to be specified if traits defined using this trait handler require a non-default trait editor in trait user interfaces. The default implementation of this method returns a trait editor that allows the user to type an arbitrary string as the value.
For more information on trait user interfaces, refer to the Traits UI User Guide.
- Parameters
trait (Trait) – The trait to be edited.
-
class
traits.trait_types.
PrefixMap
(map, *, default_value=None, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A cross between the PrefixList and Map classes.
Like Map, PrefixMap is created using a dictionary, but in this case, the keys of the dictionary must be strings. Like PrefixList, a string v is a valid value for the trait attribute if it is a prefix of one and only one key k in the dictionary. The actual values assigned to the trait attribute is k, and its corresponding mapped attribute is map*[*k].
Example
mapping = {'true': 1, 'yes': 1, 'false': 0, 'no': 0 } boolean_map = PrefixMap(mapping)
This example defines a Boolean trait that accepts any prefix of ‘true’, ‘yes’, ‘false’, or ‘no’, and maps them to 1 or 0.
- Parameters
map (dict) – A dictionary whose keys are strings that are valid values for the trait attribute, and whose corresponding values are the values for the shadow trait attribute.
default_value (object, optional) – The default value for the trait. If given, this should be either a key from the mapping or a unique prefix of a key from the mapping. If not given, the first key from the mapping (in normal dictionary iteration order) will be used as the default.
-
map
¶ A dictionary whose keys are strings that are valid values for the trait attribute, and whose corresponding values are the values for the shadow trait attribute.
- Type
-
default_value_type
= 0¶ The default value type to use.
-
info
()[source]¶ Must return a string describing the type of value accepted by the trait handler.
The string should be a phrase describing the type defined by the TraitHandler subclass, rather than a complete sentence. For example, use the phrase, “a square sprocket” instead of the sentence, “The value must be a square sprocket.” The value returned by info() is combined with other information whenever an error occurs and therefore makes more sense to the user if the result is a phrase. The info() method is similar in purpose and use to the info attribute of a validator function.
Note that the result can include information specific to the particular trait handler instance. If the info() method is not overridden, the default method returns the value of the ‘info_text’ attribute.
-
get_editor
(trait)[source]¶ Returns a trait editor that allows the user to modify the trait trait. This method only needs to be specified if traits defined using this trait handler require a non-default trait editor in trait user interfaces. The default implementation of this method returns a trait editor that allows the user to type an arbitrary string as the value.
For more information on trait user interfaces, refer to the Traits UI User Guide.
- Parameters
trait (Trait) – The trait to be edited.
-
class
traits.trait_types.
BaseClass
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A base trait type for trait types which have an associated class.
Traits sometimes need to be able to access classes which have not yet been defined, or which are from a module that we want to defer importing from. To support this, classes can be determined dynamically by specifying a string name for the class (e.g.
'package1.package2.module.class'
). This base class provides the machinery for this sort of deferred access to classes.Any subclass must define instances with ‘klass’ and ‘module’ attributes that contain the string name of the class (or actual class object) and the module name that contained the original trait definition (used for resolving local class names (e.g. ‘LocalClass’)).
This is an abstract class that only provides helper methods used to resolve the class name into an actual class object.
-
default_value_type
= 0¶ The default value type to use.
-
-
class
traits.trait_types.
BaseInstance
(klass=None, factory=None, args=None, kw=None, allow_none=True, adapt=None, module=None, **metadata)[source]¶ Bases:
traits.trait_types.BaseClass
A trait type whose value is an instance of a class or its subclasses.
The default value is 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.- Parameters
klass (class, str or instance) – The object that forms the basis for the trait; if it is an instance, then trait values must be instances of the same class or a subclass. This object is not the default value, even if it is an instance. If the provided value is a string, it is expected to be a reference to a class that will be resolved at run-time.
factory (callable) – A callable, typically a class, that when called with args and kw, returns the default value for the trait. If not specified, or None, klass is used as the factory.
args (tuple) – Positional arguments for generating the default value.
kw (dictionary) – Keyword arguments for generating the default value.
allow_none (bool) – Indicates whether None is allowed as a value.
adapt (str) –
A string specifying how adaptation should be applied. The possible values are:
’no’: Adaptation is not allowed.
’yes’: Adaptation is allowed. If adaptation fails, an exception should be raised.
’default’: Adaptation is allowed. If adaptation fails, the default value for the trait should be used.
-
factory
¶ A callable, typically a class, that when called with args and kw, returns the default value for the trait. If not specified, or None, klass is used as the factory.
- Type
callable
-
kw
¶ Keyword arguments for generating the default value.
- Type
dictionary
-
adapt
¶ A string specifying how adaptation should be applied. The possible values are:
‘no’: Adaptation is not allowed.
‘yes’: Adaptation is allowed. If adaptation fails, an exception should be raised.
‘default’: Adaptation is allowed. If adaptation fails, the default value for the trait should be used.
- Type
-
adapt_default
= 'no'¶ Default adaptation behavior.
-
clone
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Copy, optionally modifying default value and metadata.
-
class
traits.trait_types.
Instance
(klass=None, factory=None, args=None, kw=None, allow_none=True, adapt=None, module=None, **metadata)[source]¶ Bases:
traits.trait_types.BaseInstance
A fast-validated trait type whose value is an instance of a class.
-
class
traits.trait_types.
Supports
(klass=None, factory=None, args=None, kw=None, allow_none=True, adapt=None, module=None, **metadata)[source]¶ Bases:
traits.trait_types.Instance
A trait type whose value is adapted to a specified protocol.
In other words, the value of the trait directly provide, or can be adapted to, the given protocol (Interface or type).
The value of the trait after assignment is the possibly adapted value (i.e., it is the original assigned value if that provides the protocol, or is an adapter otherwise).
The original, unadapted value is stored in a “shadow” attribute with the same name followed by an underscore (e.g.,
foo
andfoo_
).
-
class
traits.trait_types.
AdaptsTo
(klass=None, factory=None, args=None, kw=None, allow_none=True, adapt=None, module=None, **metadata)[source]¶ Bases:
traits.trait_types.Supports
A trait type whose value must support a specified protocol.
In other words, the value of the trait directly provide, or can be adapted to, the given protocol (Interface or type).
The value of the trait after assignment is the original, unadapted value.
A possibly adapted value is stored in a “shadow” attribute with the same name followed by an underscore (e.g.,
foo
andfoo_
).
-
class
traits.trait_types.
Type
(value=None, klass=None, allow_none=True, **metadata)[source]¶ Bases:
traits.trait_types.BaseClass
A trait type whose value must be a subclass of a specified class.
- Parameters
value (class or None) – The default value of the trait.
klass (class, str or None) – The class that trait values must be subclasses of. If None, then the default value is used instead. If both are None, then the
object
type is used. If it is a string, the first time that the validate method is called, the class will be imported and the value replaced with the class object.allow_none (bool) – Indicates whether None is allowed as an assignable value. Even if False, the default value may be None.
**metadata – Trait metadata for the trait.
-
klass
¶ The class that trait values must be subclasses of. If this is a string, the first time that the validate method is called, the class will be imported and the value replaced with the class object.
- Type
class or str
-
module
¶ The name of the module where local class names (ie. class names with no module components) are presumed to be importable from. This is the caller’s caller’s module, as determined by the
get_module_method
.- Type
-
resolve
(object, name, value)[source]¶ Resolves a class originally specified as a string into an actual class, then resets the trait so that future calls will be handled by the normal validate method.
-
traits.trait_types.
Subclass
¶ An alias for the Type trait
alias of
traits.trait_types.Type
-
class
traits.trait_types.
Event
(trait=None, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type that holds no value but can be set and listened to.
Event traits are write-only traits. They do not hold any value, but they can be assigned to, and listeners to the trait will be notified of the assignment. Since no value is held, trait change functions that ask for the
old
value of the trait will be given the Undefined special value.Event traits can be given an optional trait type that is used to validate values assigned to the trait. If the assigned value does not validate, then a TraitError will occur.
- Parameters
trait (a trait) – The type of value that can be assigned to the event.
-
default_value_type
= 0¶ The default value type to use.
-
class
traits.trait_types.
Button
(label='', image=None, values_trait=None, style='button', orientation='vertical', width_padding=7, height_padding=5, view=None, **metadata)[source]¶ Bases:
traits.trait_types.Event
An Event trait type whose UI editor is a button.
- Parameters
label (str) – The label for the button.
image (pyface.ImageResource) – An image to display on the button.
style ('button', 'radio', 'toolbar' or 'checkbox') – The style of button to display.
values_trait (str) – For a “button” or “toolbar” style, the name of an enum trait whose values will populate a drop-down menu on the button. The selected value will replace the label on the button.
orientation ('horizontal' or 'vertical') – The orientation of the label relative to the image.
width_padding (integer between 0 and 31) – Extra padding (in pixels) added to the left and right sides of the button.
height_padding (integer between 0 and 31) – Extra padding (in pixels) added to the top and bottom of the button.
view (traitsui View, optional) – An optional View to display when the button is clicked.
**metadata – Trait metadata for the trait.
-
image
¶ An image to display on the button.
- Type
pyface.ImageResource
-
style
¶ The style of button to display.
- Type
‘button’, ‘radio’, ‘toolbar’ or ‘checkbox’
-
values_trait
¶ For a “button” or “toolbar” style, the name of an enum trait whose values will populate a drop-down menu on the button. The selected value will replace the label on the button.
- Type
-
orientation
¶ The orientation of the label relative to the image.
- Type
‘horizontal’ or ‘vertical’
-
width_padding
¶ Extra padding (in pixels) added to the left and right sides of the button.
- Type
integer between 0 and 31
-
height_padding
¶ Extra padding (in pixels) added to the top and bottom of the button.
- Type
integer between 0 and 31
-
view
¶ An optional View to display when the button is clicked.
- Type
traitsui View, optional
-
class
traits.trait_types.
ToolbarButton
(label='', image=None, style='toolbar', orientation='vertical', width_padding=2, height_padding=2, **metadata)[source]¶ Bases:
traits.trait_types.Button
A Button trait type whose UI editor is a toolbar button.
This is just a Button trait with different defaults to style it like a toolbar button.
- Parameters
label (str) – The label for the button.
image (pyface.ImageResource) – An image to display on the button.
style ('button', 'radio', 'toolbar' or 'checkbox') – The style of button to display.
orientation ('horizontal' or 'vertical') – The orientation of the label relative to the image.
width_padding (integer between 0 and 31) – Extra padding (in pixels) added to the left and right sides of the button.
height_padding (integer between 0 and 31) – Extra padding (in pixels) added to the top and bottom of the button.
**metadata – Trait metadata for the trait.
-
image
¶ An image to display on the button.
- Type
pyface.ImageResource
-
style
¶ The style of button to display.
- Type
‘button’, ‘radio’, ‘toolbar’ or ‘checkbox’
-
values_trait
¶ For a “button” or “toolbar” style, the name of an enum trait whose values will populate a drop-down menu on the button. The selected value will replace the label on the button.
- Type
-
orientation
¶ The orientation of the label relative to the image.
- Type
‘horizontal’ or ‘vertical’
-
width_padding
¶ Extra padding (in pixels) added to the left and right sides of the button.
- Type
integer between 0 and 31
-
height_padding
¶ Extra padding (in pixels) added to the top and bottom of the button.
- Type
integer between 0 and 31
-
view
¶ An optional View to display when the button is clicked.
- Type
traitsui View, optional
-
class
traits.trait_types.
Union
(*traits, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
Defines a trait whose value can be any of of a specified list of trait types or list of trait type instances or None
If the default value is not defined on Union, the default value from the first trait will be used.
-
default_value_type
= 0¶ The default value type to use.
-
validate
(obj, name, value)[source]¶ Return the value by the first trait in the list that can validate the assigned value, raise an error if none of them can.
-
info
()[source]¶ Must return a string describing the type of value accepted by the trait handler.
The string should be a phrase describing the type defined by the TraitHandler subclass, rather than a complete sentence. For example, use the phrase, “a square sprocket” instead of the sentence, “The value must be a square sprocket.” The value returned by info() is combined with other information whenever an error occurs and therefore makes more sense to the user if the result is a phrase. The info() method is similar in purpose and use to the info attribute of a validator function.
Note that the result can include information specific to the particular trait handler instance. If the info() method is not overridden, the default method returns the value of the ‘info_text’ attribute.
-
inner_traits
()[source]¶ Returns a tuple containing the inner traits for this trait. Most trait handlers do not have any inner traits, and so will return an empty tuple. The exceptions are List and Dict trait types, which have inner traits used to validate the values assigned to the trait. For example, in List( Int ), the inner traits for List are ( Int, ).
-
get_editor
(trait)[source]¶ Returns a trait editor that allows the user to modify the trait trait. This method only needs to be specified if traits defined using this trait handler require a non-default trait editor in trait user interfaces. The default implementation of this method returns a trait editor that allows the user to type an arbitrary string as the value.
For more information on trait user interfaces, refer to the Traits UI User Guide.
- Parameters
trait (Trait) – The trait to be edited.
-
-
class
traits.trait_types.
Either
(*traits, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value can be any of of a specified list of traits.
Note
This class has some unusual corner-case behaviours and is not recommended for use in new code. It may eventually be deprecated and removed. For new code, consider using the
Union
trait type instead.- Parameters
*traits – Arguments that define allowable trait values.
**metadata – Trait metadata for the trait.
-
trait_maker
¶ A TraitHandler generated by _TraitMaker from the arguments.
- Type
-
class
traits.trait_types.
Symbol
(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A property trait type that refers to a Python object by name.
The value set to the trait must be a value of the form
'[package.package...package.]module[:symbol[([arg1,...,argn])]]'
which is imported and evaluated to get underlying value.The value returned by the trait is the actual object that this string refers to. The value is cached, so any calls are only evaluated once.
Deprecated since version 6.3.0: This trait type is deprecated, and will be removed in a future version of Traits.
-
info_text
= "an object or a string of the form '[package.package...package.]module[:symbol[([arg1,...,argn])]]' specifying where to locate the object"¶ A description of the type of value this trait accepts:
-
-
class
traits.trait_types.
UUID
(can_init=False, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A read-only trait type whose value is a globally unique UUID (type 4).
- Parameters
can_init (bool) – Whether the value can be set during object instantiation. Otherwise the UUID is generated automatically.
Example
Passing can_init=True allows the UUID value to be set during object instantiation, e.g.:
class A(HasTraits): id = UUID class B(HasTraits): id = UUID(can_init=True) # TraitError! A(id=uuid.uuid4()) # Okay! B(id=uuid.uuid4())
Note however that in both cases, the UUID trait is set automatically to a uuid.UUID instance (assuming none is provided during initialization in the latter case).
-
info_text
= 'a read-only UUID'¶ A description of the type of value this trait accepts:
-
class
traits.trait_types.
WeakRef
(klass='traits.has_traits.HasTraits', allow_none=False, adapt='yes', **metadata)[source]¶ Bases:
traits.trait_types.Instance
A trait type holding a weak reference to an instance of a class.
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.
- Parameters
klass (class, str or instance) – The object that forms the basis for the trait. If klass is omitted, then values must be an instance of HasTraits. If a string, the value will be resolved to a class object at runtime.
allow_none (boolean) – Indicates whether None can be _assigned_. The trait attribute may give a None value if the object referred to has been garbage collected even if allow_none is False.
adapt (str) – How to use the adaptation infrastructure when setting the value.
-
class
traits.trait_types.
Date
(default_value=None, *, allow_datetime=None, allow_none=None, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a date.
The value must be an instance of
datetime.date
. Note thatdatetime.datetime
is a subclass ofdatetime.date
, so by default instances ofdatetime.datetime
are also permitted. UseDate(allow_datetime=False)
to exclude this possibility.Deprecated since version 6.3.0: In the future,
datetime.datetime
instances will not be valid values for this trait type unless “allow_datetime=True” is explicitly given.Deprecated since version 6.3.0: In the future,
None
will not be a valid value for this trait type unless “allow_none=True” is explicitly given.- Parameters
default_value (datetime.date, optional) – The default value for this trait. If no default is provided, the default is
None
.allow_datetime (bool, optional) – If
False
, instances ofdatetime.datetime
are not valid values for this Trait. IfTrue
,datetime.datetime
instances are explicitly permitted. If this argument is not given,datetime.datetime
instances will be accepted, but aDeprecationWarning
will be issued; in some future version of Traits,datetime.datetime
instances will not be permitted.allow_none (bool, optional) – If
False
, it’s not permitted to assignNone
to this trait. IfTrue
,None
instances are permitted. If this argument is not given,None
instances will be accepted but aDeprecationWarning
will be issued; in some future verison of Traits,None
may no longer be permitted.**metadata (dict) – Additional metadata.
-
default_value_type
= 0¶ The default value type to use.
-
class
traits.trait_types.
Datetime
(default_value=None, *, allow_none=None, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a datetime.
The value must be an instance of
datetime.datetime
.Deprecated since version 6.3.0: In the future,
None
will not be a valid value for this trait type unless “allow_none=True” is explicitly given.- Parameters
default_value (datetime.datetime, optional) – The default value for this trait. If no default is provided, the default is
None
.allow_none (bool, optional) – If
False
, it’s not permitted to assignNone
to this trait. IfTrue
,None
instances are permitted. If this argument is not given,None
instances will be accepted but aDeprecationWarning
will be issued; in some future verison of Traits,None
may no longer be permitted.**metadata (dict) – Additional metadata.
-
default_value_type
= 0¶ The default value type to use.
-
class
traits.trait_types.
Time
(default_value=None, *, allow_none=None, **metadata)[source]¶ Bases:
traits.trait_type.TraitType
A trait type whose value must be a time.
The value must be an instance of
datetime.time
.Deprecated since version 6.3.0: In the future,
None
will not be a valid value for this trait type unless “allow_none=True” is explicitly given.- Parameters
default_value (datetime.time, optional) – The default value for this trait. If no default is provided, the default is
None
.allow_none (bool, optional) – If
False
, it’s not permitted to assignNone
to this trait. IfTrue
,None
instances are permitted. If this argument is not given,None
instances will be accepted but aDeprecationWarning
will be issued; in some future verison of Traits,None
may no longer be permitted.**metadata (dict) – Additional metadata.
-
default_value_type
= 0¶ The default value type to use.
Deprecated Traits¶
The following TraitType
classes and instances are deprecated,
and may be removed in a future version of Traits.
Deprecated since version 6.0.0.
-
traits.trait_types.
AdaptedTo
= <class 'traits.trait_types.Supports'>¶ A trait whose value must support a specified protocol. This is an alias for
Supports
. UseSupports
instead.
-
traits.trait_types.
BaseUnicode
= <class 'traits.trait_types.BaseStr'>¶ A trait whose value must be a (Unicode) string. This is an alias for
BaseStr
. UseBaseStr
instead.
-
traits.trait_types.
Unicode
= <class 'traits.trait_types.Str'>¶ A trait whose value must be a (Unicode) string, using a C-level fast validator. This is an alias for
Str
. UseStr
instead.
-
traits.trait_types.
BaseCUnicode
= <class 'traits.trait_types.BaseCStr'>¶ A trait whose value must be a (Unicode) string and which supports coercions of non-string values to string. This is an alias for
BaseCStr
. UseBaseCStr
instead.
-
traits.trait_types.
CUnicode
= <class 'traits.trait_types.CStr'>¶ A trait whose value must be a (Unicode) string and which supports coercions of non-string values to string, using a C-level fast validator. This is an alias for
CStr
. UseCStr
instead.
-
traits.trait_types.
BaseLong
= <class 'traits.trait_types.BaseInt'>¶ A trait whose value must be an integer. This is an alias for
BaseInt
. UseBaseInt
instead.
-
traits.trait_types.
Long
= <class 'traits.trait_types.Int'>¶ A trait whose value must be an integer, using a C-level fast validator. This is an alias for
Int
. UseInt
instead.
-
traits.trait_types.
BaseCLong
= <class 'traits.trait_types.BaseCInt'>¶ A trait whose value must be an integer and which supports coercions of non-integer values to integer. This is an alias for
BaseCInt
. UseBaseCInt
instead.
-
traits.trait_types.
CLong
= <class 'traits.trait_types.CInt'>¶ A trait whose value must be an integer and which supports coercions of non-integer values to integer, using a C-level fast validator. This is an alias for
CInt
. UseCInt
instead.
-
traits.trait_types.
false
= <class 'traits.trait_types.Bool'>¶ Synonym for Bool; default value is
False
. This trait type is deprecated. UseBool(False)
orBool()
instead.
-
traits.trait_types.
true
= <traits.trait_types.Bool object>¶ Boolean values only; default value is
True
. This trait type is deprecated. UseBool(True)
instead.
-
traits.trait_types.
undefined
= <traits.trait_types.Any object>¶ Allows any value to be assigned; no type-checking is performed. Default value is
Undefined
. This trait type is deprecated. UseAny(Undefined)
instead.
-
traits.trait_types.
ListInt
= <traits.trait_types.List object>¶ List of integer values; default value is
[]
. This trait type is deprecated. UseList(Int)
instead.
-
traits.trait_types.
ListFloat
= <traits.trait_types.List object>¶ List of float values; default value is
[]
. This trait type is deprecated. UseList(Float)
instead.
-
traits.trait_types.
ListStr
= <traits.trait_types.List object>¶ List of string values; default value is
[]
. This trait type is deprecated. UseList(Str)
instead.
-
traits.trait_types.
ListUnicode
= <traits.trait_types.List object>¶ List of string values; default value is
[]
. This trait type is deprecated. UseList(Str)
instead.
-
traits.trait_types.
ListComplex
= <traits.trait_types.List object>¶ List of complex values; default value is
[]
. This trait type is deprecated. UseList(Complex)
instead.
-
traits.trait_types.
ListBool
= <traits.trait_types.List object>¶ List of Boolean values; default value is
[]
. This trait type is deprecated. UseList(Bool)
instead.
-
traits.trait_types.
ListFunction
= <traits.trait_types.List object>¶ List of function values; default value is
[]
. This trait type is deprecated. UseList(Instance(types.FunctionType, allow_none=False))
instead.
-
traits.trait_types.
ListMethod
= <traits.trait_types.List object>¶ List of method values; default value is
[]
. This trait type is deprecated. UseList(Instance(types.MethodType, allow_none=False))
instead.
-
traits.trait_types.
ListThis
= <traits.trait_types.List object>¶ List of container type values; default value is
[]
. This trait type is deprecated. UseList(This(allow_none=False))
instead.
-
traits.trait_types.
DictStrAny
= <traits.trait_types.Dict object>¶ Only a dictionary with strings as keys can be assigned; only string keys can be inserted. The default value is {}. This trait type is deprecated. Use
Dict(Str, Any)
instead.
-
traits.trait_types.
DictStrStr
= <traits.trait_types.Dict object>¶ Only a dictionary mapping strings to strings can be assigned; only string keys with string values can be inserted. The default value is {}. This trait type is deprecated. Use
Dict(Str, Str)
instead.
-
traits.trait_types.
DictStrInt
= <traits.trait_types.Dict object>¶ Only a dictionary mapping strings to integers can be assigned; only string keys with integer values can be inserted. The default value is {}. This trait type is deprecated. Use
Dict(Str, Int)
instead.
-
traits.trait_types.
DictStrFloat
= <traits.trait_types.Dict object>¶ Only a dictionary mapping strings to floats can be assigned; only string keys with float values can be inserted. The default value is {}. This trait type is deprecated. Use
Dict(Str, Float)
instead.
-
traits.trait_types.
DictStrBool
= <traits.trait_types.Dict object>¶ Only a dictionary mapping strings to booleans can be assigned; only string keys with boolean values can be inserted. The default value is {}. This trait type is deprecated. Use
Dict(Str, Bool)
instead.
-
traits.trait_types.
DictStrList
= <traits.trait_types.Dict object>¶ Only a dictionary mapping strings to lists can be assigned; only string keys with list values can be inserted. The default value is {}. This trait type is deprecated. Use
Dict(Str, List)
instead.
Private Classes¶
Functions¶
-
traits.trait_types.
default_text_editor
(trait, type=None)[source]¶ Return a default text editor for a trait.
- Parameters
trait (TraitType) – The trait we are constructing the editor for.
type (callable, optional) – A callable (usually a Python type) to use to evaluate the text content of the editor and return the correct type of value for the trait.
- Returns
A TraitsUI TextEditor instance for the trait.
- Return type
TextEditor