def test_object_has_named_trait_false(self): foo = Foo() self.assertFalse( helpers.object_has_named_trait(foo, "not_existing"), "Expected object_has_named_trait to return False for a" "nonexisting trait name." )
def iter_objects(self, object): """ Yield the value of the named trait from the given object, if the value is defined. The value will then be given to the next observer(s) following this one in an ObserverGraph. If the value has not been set as an instance attribute, i.e. absent in ``object.__dict__``, this observer will yield nothing. This is to avoid evaluating default initializers while adding observers. When the default is defined, a change event will trigger the maintainer to add/remove notifiers for the next observers. Note that ``Undefined``, ``Uninitialized`` and ``None`` values are also skipped, as they are inevitable filled values that contain no further attributes to be observed by any other observers. Parameters ---------- object: HasTraits Expected to be an instance of HasTraits Yields ------ value : any Raises ------ ValueError If the trait is not found and optional flag is set to false. """ if not object_has_named_trait(object, self.name): if self.optional: return raise ValueError("Trait named {!r} not found on {!r}.".format( self.name, object)) yield from iter_objects(object, self.name)
def test_object_has_named_trait_false_for_trait_list(self): foo = Foo() # TraitListObject that has `trait` attribute self.assertFalse( helpers.object_has_named_trait(foo.list_of_int, "bar"), "Expected object_has_named_trait to return false for {!r}".format( type(foo.list_of_int)))
def iter_observables(self, object): """ Yield the named instance trait from the given object. If the named trait cannot be found and optional is false, raise an error. Parameters ---------- object: object Object provided by another observers or by the user. Yields ------ CTrait Raises ------ ValueError If the trait is not found and optional flag is set to false. """ if not object_has_named_trait(object, self.name): if self.optional: return raise ValueError("Trait named {!r} not found on {!r}.".format( self.name, object)) yield object._trait(self.name, 2)
def iter_observables(self, object): """ Yield observables for notifiers to be attached to or detached from. Parameters ---------- object: object Object provided by the ``iter_objects`` methods from another observers or directly by the user. Yields ------ IObservable Raises ------ ValueError If the given object cannot be handled by this observer. """ if not object_has_named_trait(object, "trait_added"): if self.optional: return raise ValueError( "Unable to observe 'trait_added' event on {!r}".format(object)) yield object._trait("trait_added", 2)
def test_object_has_named_trait_does_not_trigger_property(self): foo = Foo() helpers.object_has_named_trait(foo, "property_value") self.assertEqual(foo.property_n_calculations, 0)
def test_object_has_named_trait_true_basic(self): foo = Foo() self.assertTrue(helpers.object_has_named_trait(foo, "list_of_int"), "The named trait should exist.")