def test_xform_ex_handler_none_return(self): ch = self.chan(3, xf.map(lambda x: 12 // x), lambda _: None) ch.b_put(-1) ch.b_put(0) ch.b_put(2) ch.close() self.assertEqual(b_list(ch), [-12, 6])
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'])
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'])
def test_xform_ex_handler_non_none_return(self): def handler(e): if isinstance(e, ZeroDivisionError): return 'zero' ch = self.chan(3, xf.map(lambda x: 12 // x), handler) ch.b_put(-1) ch.b_put(0) ch.b_put(2) ch.close() self.assertEqual(b_list(ch), [-12, 'zero', 6])
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'])
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)
def test_unsuccessful_transformation_to_none(self): ch = self.chan(1, xf.map(lambda _: None)) with self.assertRaises(AssertionError): ch.b_put('failure')
def test_into(self): appendable = [1, 2] xform = xf.map(lambda x: x + 1) self.assertIs(xf.into(appendable, xform, [3, 4]), appendable) self.assertEqual(appendable, [1, 2, 4, 5])
async def main(): ch = self.chan(1, xf.map(lambda x: x + 1)) c.onto_chan(ch, [0, 1, 2]) self.assertEqual(await a_list(ch), [1, 2, 3])
def test_complete(self): xform = xf.comp(xf.map(lambda x: x * 2), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [1, 2, 3])), [(2, 4), (6,)])
def test_arity_zero(self): self.assertEqual(xf.map(None)(lambda: 'success')(), 'success')
def test_reduced(self): xform = xf.comp(xf.map(lambda x: x * 2), xf.take(2)) self.assertEqual(list(xf.xiter(xform, [1, 2, 3, 4])), [2, 4])
def test_map_none(self): xform = xf.map(None) self.assertEqual(list(xf.xiter(xform, [])), [])
def test_map_some(self): xform = xf.map(lambda x: x * 2) self.assertEqual(list(xf.xiter(xform, [1, 2, 3])), [2, 4, 6])