traits.trait_converters Module

Utility routines that convert objects to CTrait instances

Most of these functions depend on objects having an informal as_ctrait interface if they know how to turn themselves into a CTrait instance.

We need to special-case TraitType subclasses so that we can use them in trait definitions without needing to call them, eg.:

x = Float

rather than:

x = Float()

To handle this, there is another informal interface with the deliberately unwieldy classmethod name instantiate_and_get_ctrait.

Functions

traits.trait_converters.as_ctrait(obj)[source]

Convert to CTrait if the object knows how, else raise TraitError.

Parameters

obj – An object that supports conversion to CTrait. Refer documentation for when the object supports this conversion.

Returns

A CTrait object.

Return type

ctrait.CTrait

Raises

TypeError – If the object does not support conversion to CTrait.

traits.trait_converters.check_trait(trait)[source]

Returns either the original value or a valid CTrait if the value can be converted to a CTrait.

traits.trait_converters.trait_cast(obj)[source]

Convert to a CTrait if the object knows how, else return None.

traits.trait_converters.trait_from(obj)[source]

Returns a trait derived from its input.

traits.trait_converters.trait_for(trait)[source]

Returns the trait corresponding to a specified value.

traits.trait_converters.mapped_trait_for(trait, name)[source]

Returns the ‘mapped trait’ definition for a mapped trait, the default value of which is a callable that maps the value of the original trait.

Parameters
  • trait (ctrait.CTrait) – A trait for which the ‘mapped trait’ definition is being created.

  • name (str) – The name of the trait for which the ‘mapped trait’ definition is being created.

Returns

A definition of the ‘mapped trait’

Return type

trait_types.Any