Beispiel #1
0
 async def main():
     letter_ch = c.to_chan(['a', 'b', 'c', 'd', 'e'])
     number_ch = c.to_chan(['1', '2', '3'])
     result_ch = c.map(lambda x, y: x + y, [letter_ch, number_ch],
                       c.sliding_buffer(2))
     await asyncio.sleep(0.1)
     self.assertEqual(await a_list(result_ch), ['b2', 'c3'])
     self.assertEqual(await a_list(letter_ch), ['e'])
Beispiel #2
0
 async def main():
     to_ch = chan(5, xf.take(5))
     from_ch = c.to_chan(range(20))
     c.pipeline(5, to_ch, xf.identity, from_ch, mode=mode)
     await asyncio.sleep(0.1)
     self.assertEqual(await a_list(to_ch), [0, 1, 2, 3, 4])
     self.assertTrue(len(await a_list(from_ch)) > 5)
Beispiel #3
0
 async def main():
     in_ch = c.to_chan(range(4))
     with self.assertRaises(TypeError):
         c.transduce(
             xf.filter(lambda x: x % 2 == 0),
             xf.multi_arity(None, xf.identity, lambda x, y: x + y),
             in_ch)
Beispiel #4
0
 async def main():
     in_ch = c.to_chan(range(4))
     result_ch = c.transduce(
         xf.filter(lambda x: x % 2 == 0),
         xf.multi_arity(lambda: 100, xf.identity, lambda x, y: x + y),
         in_ch)
     self.assertEqual(await result_ch.get(), 102)
Beispiel #5
0
 async def main():
     t_ch, f_ch = c.split(lambda x: x % 2 == 0, c.to_chan([1, 2, 3, 4]))
     self.assertEqual(await f_ch.get(), 1)
     self.assertEqual(await t_ch.get(), 2)
     self.assertEqual(await f_ch.get(), 3)
     self.assertEqual(await t_ch.get(), 4)
     self.assertIsNone(await f_ch.get())
     self.assertIsNone(await t_ch.get())
Beispiel #6
0
 async def main():
     to_ch = chan(8)
     start_time = time.time()
     finished_ch = c.pipeline_async(2, to_ch, af,
                                    c.to_chan([1, 2, 3, 4]))
     self.assertIs(await finished_ch.get(), None)
     self.assertTrue(0.3 < time.time() - start_time < 0.5)
     self.assertEqual(await a_list(to_ch),
                      [1, '1', 2, '2', 3, '3', 4, '4'])
Beispiel #7
0
 async def main():
     to_ch = chan(2)
     c.pipeline(1,
                to_ch,
                xf.map(f),
                c.to_chan([1, 2]),
                ex_handler=ex_handler,
                mode=mode)
     self.assertEqual(await a_list(to_ch), ['ex_handler value', '2'])
Beispiel #8
0
        async def main():
            in_ch = c.to_chan(range(4))

            def rf(result, val):
                if val == 2:
                    return xf.reduced(result + 2)
                return result + val

            result_ch = c.reduce(rf, 100, in_ch)
            self.assertEqual(await result_ch.get(), 103)
Beispiel #9
0
 async def main():
     to_ch = chan(5)
     finished_ch = c.pipeline(5,
                              to_ch,
                              xf.map(str),
                              c.to_chan(range(5)),
                              mode=mode,
                              chunksize=2)
     self.assertIs(await finished_ch.get(), None)
     self.assertEqual(await a_list(to_ch), ['0', '1', '2', '3', '4'])
Beispiel #10
0
        async def main():
            ch = c.to_chan([1, 2, 3])

            def rf(result, val=None):
                if val is None:
                    return result
                result.append(val)
                return result

            result_ch = c.transduce(xf.take(2), rf, [], ch)
            self.assertEqual(await result_ch.get(), [1, 2])
Beispiel #11
0
 async def main():
     to_ch = chan(1)
     finished_ch = c.pipeline_async(2,
                                    to_ch,
                                    af,
                                    c.to_chan([1, 2, 3, 4]),
                                    close=False)
     self.assertIs(await finished_ch.get(), None)
     await to_ch.put('success')
     to_ch.close()
     self.assertEqual(await to_ch.get(), 'success')
     self.assertIs(await to_ch.get(), None)
Beispiel #12
0
 async def main():
     xform = xf.map(f)
     start_time = time.time()
     to_ch = chan(5)
     finished_ch = c.pipeline(5,
                              to_ch,
                              xform,
                              c.to_chan(range(5)),
                              mode=mode)
     self.assertIs(await finished_ch.get(), None)
     elapsed_time = time.time() - start_time
     self.assertTrue(0.1 < elapsed_time < 0.3)
     self.assertEqual(await a_list(to_ch), ['0', '1', '2', '3', '4'])
Beispiel #13
0
 async def main():
     to_ch = chan(5)
     c.pipeline(5,
                to_ch,
                xf.map(str),
                c.to_chan(range(5)),
                close=False,
                mode=mode)
     for i in range(5):
         self.assertEqual(await to_ch.get(), str(i))
     self.assertIs(await to_ch.put('success'), True)
     to_ch.close()
     self.assertEqual(await to_ch.get(), 'success')
     self.assertIs(await to_ch.get(), None)
Beispiel #14
0
 async def main():
     in_ch = c.to_chan(range(4))
     result_ch = c.reduce(lambda x, y: x + y, 100, in_ch)
     self.assertEqual(await result_ch.get(), 106)
Beispiel #15
0
 async def main():
     t_ch, f_ch = c.split(lambda x: x % 2 == 0,
                          c.to_chan([1, 2, 3, 4, 5]), 2, 3)
     self.assertEqual(await a_list(t_ch), [2, 4])
     self.assertEqual(await a_list(f_ch), [1, 3, 5])
Beispiel #16
0
 async def main():
     letter_ch = c.to_chan(['a', 'b', 'c', 'd', 'e'])
     number_ch = c.to_chan(['1', '2', '3'])
     result_ch = c.map(lambda x, y: x + y, [letter_ch, number_ch])
     self.assertEqual(await a_list(result_ch), ['a1', 'b2', 'c3'])
     self.assertEqual(await a_list(letter_ch), ['e'])