Ejemplo n.º 1
0
def static_atom():
    """Class to test static observers."""
    class Extended(Atom):

        val = Int()

    obs_decorator = observe("val2", "ext.val")

    class ObserverTest(Atom):

        cls = Extended

        ext = Value()

        val2 = Int(0)

        changes = List()

        @obs_decorator
        def react(self, change):
            self.changes.append(change["name"])

        manual_obs = obs_decorator(react.func)

    return ObserverTest()
Ejemplo n.º 2
0
def test_observe_decorators():
    """Test checking observe decorator handling."""
    def react(self, change):
        pass

    handler = observe(("val", ))
    handler(react)
    handler_clone = handler.clone()
    assert handler is not handler_clone
    assert handler.pairs == handler_clone.pairs
    assert handler.func is handler_clone.func

    with pytest.raises(TypeError):
        observe(12)
    with pytest.raises(TypeError):
        observe(["a.b.c"])
Ejemplo n.º 3
0
def static_atom():
    """Class to test static observers.

    """
    class Extended(Atom):

        val = Int()

    obs_decorator = observe('val2', 'ext.val')

    class ObserverTest(Atom):

        cls = Extended

        ext = Value()

        val2 = Int(0)

        changes = List()

        @obs_decorator
        def react(self, change):
            self.changes.append(change['name'])

        manual_obs = obs_decorator(react.func)

    return ObserverTest()
Ejemplo n.º 4
0
def test_observe_decorators():
    """Test checking observe decorator handling.

    """
    def react(self, change):
        pass
    handler = observe(('val',))
    handler(react)
    handler_clone = handler.clone()
    assert handler is not handler_clone
    assert handler.pairs == handler_clone.pairs
    assert handler.func is handler_clone.func

    with pytest.raises(TypeError):
        observe(12)
    with pytest.raises(TypeError):
        observe(['a.b.c'])
Ejemplo n.º 5
0
 def __call__(self, func):
     def new_func(obj, change):
         if change["type"]=="update":
             func(obj, change)
             obj.update_plot(self.update_legend)
         elif change["type"]=="create":
             if self.immediate_update:
                 func(obj, change)
     return observe(*self.args)(new_func)
Ejemplo n.º 6
0
    def __call__(self, func):
        def new_func(obj, change):
            if change["type"] == "update":
                func(obj, change)
                obj.update_plot(self.update_legend)
            elif change["type"] == "create":
                if self.immediate_update:
                    func(obj, change)

        return observe(*self.args)(new_func)
Ejemplo n.º 7
0
def test_static_observers():
    """Test using static observers.

    """
    class Extended(Atom):

        val = Int()

    obs_decorator = observe('val2', 'ext.val')

    class ObserverTest(Atom):

        ext = Value()

        val2 = Int(0)

        changes = List()

        @obs_decorator
        def react(self, change):
            self.changes.append(change['name'])

        manual_obs = obs_decorator(react.func)

    ot = ObserverTest()
    ext1 = Extended()
    ext2 = Extended()

    # Test installing the extended observer
    ot.ext = ext1
    assert ext1.has_observer('val', ot.react)
    assert not ext2.has_observer('val', ot.react)

    ot.ext = ext2
    assert ext2.has_observer('val', ot.react)
    assert not ext1.has_observer('val', ot.react)

    # Test notifications on value setting
    ot.val2 = 1
    assert 'val2' in ot.changes
    ext1.val = 1
    assert 'val' not in ot.changes
    ext2.val = 1
    assert 'val' in ot.changes

    # Test manually managing static observers
    ot.changes = []
    # We have 2 static observers hence 2 removals
    ObserverTest.val2.remove_static_observer('react')
    ObserverTest.val2.remove_static_observer('manual_obs')
    ot.val2 += 1
    assert not ot.changes
    ObserverTest.val2.add_static_observer('react')
    ot.val2 += 1
    assert ot.changes

    # Test removing the extended observer upon deletion
    del ot.ext
    assert not ext2.has_observer('val', ot.react)

    with pytest.raises(TypeError):
        ot.ext = 12