Exemple #1
0
    def test_init(self):
        w = self.wrapper()

        self.assertIsInstance(w.data_proxy(), dict)
        self.assertFalse(w.trace)
        self.assertEqual(len(w._attributes), 1)

        self.assertTrue(hasattr(w, "a"))
        self.assertFalse(hasattr(w, "b"))
        with self.assertRaises(AttributeError):
            w.b

        w = ph.Wrapper(trace=1, attrs=dict(
            foo=ph.Wrapper(trace=2),
            bar=3,
        ))
        self.assertEqual(w.trace.values, (1, ))
        self.assertEqual(len(w._attributes), 2)
        self.assertIsInstance(w.foo, ph.Wrapper)
        self.assertEqual(w.foo.trace, (1, 2))
        self.assertIsInstance(w.bar, ph.Wrapper)
        self.assertEqual(w.bar.trace, (1, 3))
Exemple #2
0
    def test_magic_methods(self):
        w = self.wrapper()

        # __call__
        data = deepcopy(TraceTest.struct)
        data["a"][1][0] = 99
        self.assertEqual(w.a.first()[0], 10)
        w(data)
        self.assertEqual(w.a.first()[0], 99)

        # __repr__
        self.assertEqual(repr(w), repr(w()))

        # __contains__
        self.assertIn(15, w.a.first)

        # __nonzero__
        self.assertTrue(w)
        self.assertFalse(ph.Wrapper())

        # __len__
        self.assertEqual(len(w.a), 1)
Exemple #3
0
 def W(start=-2, stop=2):
     return ph.Wrapper(tuple(range(start, stop)))
Exemple #4
0
    def test_math_ops(self):
        w = ph.Wrapper((1, 2))

        self.assertEqual(w, w)
        self.assertNotEqual(w, ph.Wrapper((2, 3)))

        if np:
            w_np1 = ph.Wrapper(np.arange(0, 20, 2))
            w_np2 = ph.Wrapper(np.arange(5, 15))

            self.assertEqual((w_np1 < w_np2).sum(), 5)
            self.assertEqual((w_np1 <= w_np2).sum(), 6)
            self.assertEqual((w_np1 > w_np2).sum(), 4)
            self.assertEqual((w_np1 >= w_np2).sum(), 5)

        def W(start=-2, stop=2):
            return ph.Wrapper(tuple(range(start, stop)))

        w = W(-5, 5)
        self.assertEqual((+w).data, tuple(range(-5, 5)))
        self.assertEqual((-w).data, tuple(range(-4, 6))[::-1])
        self.assertEqual(abs(w).data, (5, 4, 3, 2, 1, 0, 1, 2, 3, 4))
        self.assertEqual((~w).data, tuple(range(-5, 5))[::-1])

        w = W()
        self.assertEqual((w & 1).data, (0, 1, 0, 1))
        self.assertEqual((1 & w).data, (0, 1, 0, 1))
        w &= 1
        self.assertEqual(w.data, (0, 1, 0, 1))

        w = W()
        self.assertEqual((w | 1).data, (-1, -1, 1, 1))
        self.assertEqual((1 | w).data, (-1, -1, 1, 1))
        w |= 1
        self.assertEqual(w.data, (-1, -1, 1, 1))

        w = W()
        self.assertEqual((w ^ 1).data, (-1, -2, 1, 0))
        self.assertEqual((1 ^ w).data, (-1, -2, 1, 0))
        w ^= 1
        self.assertEqual(w.data, (-1, -2, 1, 0))

        w = W(0, 4)
        self.assertEqual((w << 1).data, (0, 2, 4, 6))
        self.assertEqual((1 << w).data, (1, 2, 4, 8))
        w <<= 1
        self.assertEqual(w.data, (0, 2, 4, 6))

        w = W(0, 4)
        self.assertEqual((w >> 1).data, (0, 0, 1, 1))
        self.assertEqual((1 >> w).data, (1, 0, 0, 0))
        w >>= 1
        self.assertEqual(w.data, (0, 0, 1, 1))

        w = W()
        self.assertEqual((w + 1).data, (-1, 0, 1, 2))
        self.assertEqual((1 + w).data, (-1, 0, 1, 2))
        w += 1
        self.assertEqual(w.data, (-1, 0, 1, 2))

        w = W()
        self.assertEqual((w - 1).data, (-3, -2, -1, 0))
        self.assertEqual((1 - w).data, (3, 2, 1, 0))
        w -= 1
        self.assertEqual(w.data, (-3, -2, -1, 0))

        w = W()
        self.assertEqual((w * 2).data, (-4, -2, 0, 2))
        self.assertEqual((2 * w).data, (-4, -2, 0, 2))
        w *= 2
        self.assertEqual(w.data, (-4, -2, 0, 2))

        w = ph.Wrapper((-2, -1, 0.2, 1))
        self.assertEqual((w / 2.).data, (-1, -0.5, 0.1, 0.5))
        self.assertEqual((2. / w).data, (-1, -2, 10, 2))
        w /= 2.
        self.assertEqual(w.data, (-1, -0.5, 0.1, 0.5))

        w = ph.Wrapper((-2, -1, 0.2, 1))
        self.assertEqual((w // 2).data, (-1, -1, 0, 0))
        self.assertEqual((2 // w).data, (-1, -2, 9.0, 2))
        w //= 2
        self.assertEqual(w.data, (-1, -1, 0, 0))

        w = W()
        self.assertEqual((w % 2).data, (0, 1, 0, 1))
        w %= 2
        self.assertEqual(w.data, (0, 1, 0, 1))

        w = W()
        self.assertEqual((w**2).data, (4, 1, 0, 1))
        self.assertEqual((2**w).data, (0.25, 0.5, 1, 2))
        w **= 2
        self.assertEqual(w.data, (4, 1, 0, 1))
Exemple #5
0
 def test_get_data(self):
     tpl = (1, 2)
     w = ph.Wrapper(tpl)
     self.assertEqual(ph.Wrapper._get_data(w), tpl)
     self.assertEqual(ph.Wrapper._get_data(tpl), tpl)
Exemple #6
0
 def wrapper(cls):
     return ph.Wrapper(
         data=deepcopy(TraceTest.struct),
         attrs=dict(a=ph.Wrapper(trace="a", attrs=dict(first=1))))