예제 #1
0
    def test_getattr(self):
        def func(x):
            return x * 2

        f = functions.composable(func)
        self.assertEqual((f . str)('blah'), 'blahblah')
        self.assertRaises(NameError, lambda: f . NonExistantName)
예제 #2
0
    def test_compose(self):
        f = functions.composable(lambda x: x ** 3)
        h1 = f << (lambda x: x + 2)
        self.assertEqual(h1(2), 64)

        h2 = f >> (lambda x: x + 10)
        self.assertEqual(h2(4), 74)
예제 #3
0
    def test_iterate(self):
        def func(x):
            return x * 2

        f = functions.composable(func)
        self.assertEqual((f ** 2)(1), 4)
        self.assertEqual((f ** 3)(1), 8)
예제 #4
0
    def test_ops(self):
        ops = operator.attrgetter(*self.ops)(operator)
        f = lambda x: x ** 2
        g = lambda y: y > 10 and y - 5 or y * 3

        f = functions.composable(f)
        g = functions.composable(g)

        for op in ops:
            h = op(f, g)
            self.assertTrue(callable(h))

            start = random.randint(100, 200)

            for n in range(start, start + 100):
                self.assertEqual(h(n), op(f(n), g(n)))

        unary_ops = operator.attrgetter(*self.unary_ops)(operator)
        for op in unary_ops:
            h = op(f)
            n = random.randint(100, 1000)
            self.assertEqual(op(f(n)), h(n))
예제 #5
0
 def test_repr(self):
     def func(x):
         return x + 100
     f = functions.composable(func)
     self.assertEqual(repr(f), repr(func))