traits.ctraits Module

Fast base classes for HasTraits and CTrait.

The ctraits module defines the CHasTraits and cTrait extension types that define the core performance-oriented portions of the Traits package. Users will rarely need to use this module directly. Instead, they should use the API-complete HasTraits and CTrait subclasses of CHasTraits and cTrait (respectively).

Classes

class traits.ctraits.CHasTraits

Base class for HasTraits.

The CHasTraits class is not intended to be instantiated directly. Instead, it serves as a base class for the HasTraits class.

Parameters

**traits (dict) – Dictionary mapping trait names to trait values.

_class_traits()

_instance_traits()

Return this object’s class traits dictionary.

Returns

class_traits – Dictionary mapping trait names to corresponding CTrait instances.

Return type

dict

_instance_traits()

Return this object’s instance traits dictionary.

The object’s instance traits dictionary is created if it doesn’t already exist.

Returns

instance_traits – Dictionary mapping trait names to corresponding CTrait instances.

Return type

dict

trait_items_event(name, event_object, event_trait)

Fire an items event for changes to a Traits collection.

Parameters
  • name (str) – Name of the item trait for which an event is being fired. (The name will usually end in ‘_items’.)

  • event_object (object) – Object of type TraitListEvent, TraitDictEvent or TraitSetEvent describing the changes to the underlying collection trait value.

  • event_trait (CTrait) – The items trait, of trait type Event.

trait_property_changed(name, old_value[, new_value])

Call notifiers when a trait property value is explicitly changed.

Calls trait and object notifiers for a property value change.

Parameters
  • name (str) – Name of the trait whose value has changed

  • old_value (any) – Old value for this trait.

  • new_value (any, optional) – New value for this trait. If the new value is not provided, it’s looked up on the object.

traits_init()

Perform any final object initialization needed.

For the CHasTraits base class, this method currently does nothing.

traits_inited()

Get the initialization state of this object.

Returns

initialized – True if the object is initialized, else False.

Return type

bool

class traits.ctraits.cTrait

Base class for CTrait.

The cTrait class is not intended to be instantiated directly. Instead, it serves as a base class for CTrait.

Parameters

kind (int, optional) – Integer between 0 and 8 representing the kind of this trait, with the default value being 0. The kind determines how attribute get and set operations behave for attributes using this trait. The values for kind correspond to the members of the TraitKind enumeration type.

clone(source)

Clone state of another trait into this one.

This method copies all of the state of the source trait into this trait, with the exception of the trait notifiers and the trait __dict__. The copy is a simple shallow copy: for example, after the copy, the handler for this trait will be the same object as the handler for the source trait.

Parameters

source (CTrait) – The source trait.

comparison_mode

Integer constant indicating when notifiers are executed.

The value of this constant is the integer corresponding to a member of the ComparisonMode enumeration.

default_value()

Return tuple giving default value information for this trait.

Returns

  • default_value_type (int) – An integer representing the kind of the default value

  • default_value (value) – A value or callable providing the default

default_value_for(object, name)

Return the default value of this CTrait instance for a specified object and trait name.

Parameters
  • object (HasTraits) – The object the trait is attached to.

  • name (str) – The name of the trait.

Returns

default_value – The default value for the given object and name.

Return type

value

delegate(delegate_name, prefix, prefix_type, modify_delegate)

Set another trait as the delegate of this trait.

Parameters
  • delegate_name (str) – Name of an attribute on the current object with references the object that is the trait’s delegate.

  • delegate_prefix (str) – A prefix or substitution applied to the original attribute when looking up the delegated attribute.

  • prefix_type (int) –

    An integer between 0 and 3, inclusive. This controls how the delegator attribute name is mapped to an attribute name on the delegate object. The meanings of the values are as follows:

    0

    The delegation is to an attribute on the delegate object with the same name as the delegator attribute. delegate_prefix is unused.

    1

    The delegation is to an attribute with name given directly by delegate_prefix.

    2

    The delegation is to an attribute whose name is the value of delegate_prefix, prepended to the delegator attribute name.

    3

    The delegation is to an attribute whose name is the value of the delegator object’s __prefix__ attribute, prepended to the delegator attribute name.

  • modify_delegate (bool) – Whether to modify the delegate when the value of this trait is modified.

get_validate()

Return the validator of a CTrait instance.

Returns the current validator for a CTrait instance, or None if the trait has no validator. See also the set_validate method.

Returns

validator

Return type

tuple, callable, or None

handler

The trait handler underlying this trait.

The value of this property should be an instance of BaseTraitHandler.

is_mapped

True if this is a mapped trait, else False.

is_property

True if this trait is a property trait, else False.

This property is read-only.

modify_delegate

Indicate whether modifications affect the delegate.

For delegated traits, this is a boolean indicating whether modifications to this trait also modify the delegated trait.

post_setattr

Callable called after a successful value assignment to this trait.

The value of this property is either a callable or None. If the value is a callable, this callable allows the trait to do additional processing after a value has successfully been assigned. The callable is called with arguments (object, name, value), and the return value of the callable is ignored.

post_setattr_original_value

Whether post_setattr receives the original or the validated value.

If true, the post_setattr callable for this trait (if defined) receives the original, unvalidated value assigned to the trait. If false, the validated value is provided to post_setattr.

set_default_value(default_value_type, default_value)

Set the default value information for this trait.

Parameters
  • default_value_type (int) – An integer representing the kind of the default value

  • default_value (value) – A value or callable providing the default

set_validate(validator)

Set the validator of a CTrait instance

Parameters

validator (callable or tuple) –

Either a callable used for validation, or a tuple representing validation information.

A callable used for validation should have signature validator(obj, name, value) -> value, and should return the validated (and possibly transformed) value. It should raise TraitError on failure to validate.

If the validator is a tuple, its first entry will be an integer specifying the type of validation, and the remaining entries in the tuple (if any) provide additional information specific to the validation type

Raises

ValueError – If the given tuple does not have any of the expected forms.

setattr_original_value

Whether setattr stores the original or the validated value.

If true, setattr will store the original, unvalidated, value set on the trait to the object’s dictionary. If false, the value returned from the validator will be stored.

validate(object, name, value)

Perform validation and appropriate conversions on a value for this trait.

Parameters
  • object (HasTraits) – The HasTraits object that validation is being performed for.

  • name (str) – The name of the trait.

  • value (object) – The value to be validated.

Returns

Return type

The validated, converted value.

Raises

TraitError – If the given value is invalid for this trait.