def _clone_trait(self, clone, metadata=None): """ Creates a clone of a specific trait (ie. the same trait type but different ids). Parameters ---------- clone: CTrait (mandatory) the input trait to clone. metadata: dict (opional, default None) some metadata than can be added to the trait __dict__. Returns ------- trait: CTrait the cloned input trait. """ # Create an empty trait trait = CTrait(0) # Clone the input trait in the empty trait structure trait.clone(clone) # Set the input trait __dict__ elements to the cloned trait # __dict__ if clone.__dict__ is not None: trait.__dict__ = clone.__dict__.copy() # Update the cloned trait __dict__ if necessary if metadata is not None: trait.__dict__.update(metadata) return trait
def test_trait_string_description(self): """ Method to test if we can build a string description for a trait. """ trait = CTrait(0) trait.handler = Float() trait.ouptut = False trait.optional = True trait.desc = "bla" manhelp = get_trait_desc("float_trait", trait, 5) self.assertEqual( manhelp[0], "float_trait: a float (['Float'] - optional, default value: 5)") self.assertEqual(manhelp[1], " bla")
def test_trait(self): """ Method to test trait characterisitics: value, type. """ self.assertTrue(is_trait_value_defined(5)) self.assertFalse(is_trait_value_defined("")) self.assertFalse(is_trait_value_defined(None)) self.assertFalse(is_trait_value_defined(Undefined)) trait = CTrait(0) trait.handler = Float() self.assertFalse(is_trait_pathname(trait)) for handler in [File(), Directory()]: trait.handler = handler self.assertTrue(is_trait_pathname(trait))
def relax_exists_constrain(trait): """ Relax the exist constrain of a trait Parameters ---------- trait: trait a trait that will be relaxed from the exist constrain """ # If we have a single trait, just modify the 'exists' contrain # if specified if hasattr(trait.handler, "exists"): trait.handler.exists = False # If we have a selector, call the 'relax_exists_constrain' on each # selector inner components. main_id = trait.handler.__class__.__name__ if main_id == "TraitCompound": for sub_trait in trait.handler.handlers: sub_c_trait = CTrait(0) sub_c_trait.handler = sub_trait relax_exists_constrain(sub_c_trait) elif len(trait.inner_traits) > 0: for sub_c_trait in trait.inner_traits: relax_exists_constrain(sub_c_trait)
def _wire_default(obj, name): """ Wire an expression trait for default value computation. This is a private function used by Declarative for allowing default values of attributes to be provided by bound expression objects without requiring an explicit initialization graph. """ # This is a low-level performance hack that bypasses a mountain # of traits cruft and performs the minimum work required to make # traits do what we want. The speedup of this over `add_trait` is # substantial. # A new 'event' trait type (defaults are overridden) trait = CTrait(4) # Override defaults with 2-arg getter, 3-arg setter, no validator trait.property(_wired_getter, 2, _wired_setter, 3, None, 0) # Provide a handler else dynamic creation kills performance trait.handler = Any shadow = obj._trait(name, 2) trait._shadowed = shadow trait._notifiers = shadow._notifiers obj._instance_traits()[name] = trait