Example #1
0
def test_array_copy_listener():
    a = ndl([-0.5, 0.1, 1.1])

    class Watcher(object):
        def __init__(self):
            self.called_me = False

        def __call__(self):
            self.called_me = True

    you0 = Watcher()
    you1 = Watcher()

    a.talk_to(you0)

    b = ndl(a)
    assert_(not you0.called_me)

    b[0] = 1.0

    assert_(you0.called_me)

    b.talk_to(you1)

    assert_(not you1.called_me)

    a[0] = 1.0

    assert_(you1.called_me)
Example #2
0
def test_scalar_copy_listener():
    a = ndl(-0.5)

    class Watcher(object):
        def __init__(self):
            self.called_me = False

        def __call__(self):
            self.called_me = True

    you0 = Watcher()
    you1 = Watcher()

    a.talk_to(you0)

    b = ndl(a)
    assert_(not you0.called_me)

    b.itemset(1.0)

    assert_(you0.called_me)

    b.talk_to(you1)

    assert_(not you1.called_me)

    a.itemset(1.0)

    assert_(you1.called_me)
Example #3
0
def test_notification():
    a = np.array([-0.5, 0.1, 1.1])
    b = ndl(a)
    c = ndl(np.array([-0.5, 0.1, 1.1]))

    class Watcher(object):
        def __init__(self):
            self.called_me = False

        def __call__(self):
            self.called_me = True

    w = Watcher()
    b.talk_to(w)

    assert_(not w.called_me)
    b[0] = 1.2
    assert_(w.called_me)

    w = Watcher()
    b.talk_to(w)

    assert_(not w.called_me)

    b[:] = 1
    assert_(w.called_me)

    w = Watcher()
    c.talk_to(w)

    assert_(not w.called_me)
    c[:] = c + c
    assert_(w.called_me)
Example #4
0
def test_operations():
    a = np.array([-0.5, 0.1, 1.1])
    b = ndl(a)
    c = ndl(np.array([-0.5, 0.1, 1.1]))

    assert_array_almost_equal(a - b, [0, 0, 0])
    assert_array_almost_equal(a, b)
    assert_array_almost_equal(a, c)
    assert_array_almost_equal(b - c, [0, 0, 0])
Example #5
0
def test_printing(capsys):
    a = ndl(np.array([-0.5, 0.1, 1.1]))

    print(a)
    out, _ = capsys.readouterr()
    assert_equal(out, "[-0.5  0.1  1.1]\n")
    print([a])
    out, _ = capsys.readouterr()
    assert_equal(out, "[ndl([-0.5,  0.1,  1.1])]\n")

    a = ndl(np.float64(1.0))
    print(a)
    out, _ = capsys.readouterr()
    assert_equal(out, "1.0\n")
Example #6
0
    def __getattr__(self, name):
        from ndarray_listener import ndl

        if name == "value":
            v = ndl(Vector.__dict__["raw"].__get__(self))
            for l in self._listeners:
                v.talk_to(l)
            return v
        return Vector.__dict__[name].__get__(self)
Example #7
0
    def __init__(self, value):
        from ndarray_listener import ndl
        from numpy import float64, inf

        self._bounds = (-inf, +inf)
        self._fixed = False
        value = ndl(float64(value))
        self.raw = value
        self.__array_interface__ = value.__array_interface__
        self.__array_struct__ = value.__array_struct__
Example #8
0
    def __init__(self, value):
        from numpy import asarray, atleast_1d, inf
        from ndarray_listener import ndl

        self._bounds = [(-inf, +inf)] * len(value)
        self._fixed = False
        value = asarray(value, float)
        value = ndl(atleast_1d(value).ravel())
        self.raw = value
        self.__array_interface__ = value.__array_interface__
        self.__array_struct__ = value.__array_struct__
Example #9
0
def test_weakref_proxymethod():
    a = ndl(np.array([-0.5, 0.1, 1.1]))

    class Watcher(object):
        def __init__(self):
            self.called_me = False

        def call_me(self):
            self.called_me = True

    w = Watcher()
    a.talk_to(w.call_me)

    assert_(not w.called_me)
    a[0] = 1.2
    assert_(w.called_me)

    del w
    a[0] = 1.3
Example #10
0
def test_iterator():
    a = ndl([-0.5, 0.1, 1.1])
    assert_(isinstance(next(iter(a)), float64))
Example #11
0
def test_itemset():
    a = ndl(-0.5)
    a.itemset(1.0)
    assert_array_almost_equal(a, [1])