def test_xform_complete_flush(self): ch = c.promise_chan(xf.partition_all(3)) self.assertIs(ch.b_put(1), True) self.assertIs(ch.b_put(2), True) self.assertIsNone(ch.poll()) ch.close() self.assertEqual(ch.b_get(), (1, 2)) self.assertEqual(ch.b_get(), (1, 2)) self.assertIs(ch.b_put('drop me'), False)
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.partition_all(2), rf, [], ch) self.assertEqual(await result_ch.get(), [(1, 2), (3, )])
def test_partition_n_zero(self): with self.assertRaises(ValueError): xf.partition_all(0)
def test_close_flushes_xform_buffer(self): ch = self.chan(3, xf.partition_all(2)) for i in range(3): ch.b_put(i) ch.close() self.assertEqual(b_list(ch), [(0, 1), (2, )])
def test_partition_with_larger_step(self): xform = xf.partition_all(2, 4) self.assertEqual(list(xf.xiter(xform, range(1, 10))), [(1, 2), (5, 6), (9,)])
def test_complete(self): xform = xf.comp(xf.random_sample(1), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [1, 2, 3])), [(1, 2), (3,)])
def test_complete(self): xform = xf.comp(xf.replace({1: 'one'}), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [1, 2, 3])), [('one', 2), (3,)])
def test_complete(self): xform = xf.partition_all(3) self.assertEqual(list(xf.xiter(xform, range(5))), [(0, 1, 2), (3, 4)])
def test_complete(self): xform = xf.comp(xf.keep_indexed(self.even_set), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [2, 4, 5, 6])), [({2}, {4}), ({6},)])
def test_complete(self): xform = xf.comp(xf.keep(lambda x: x if x % 2 == 0 else None), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [2, 4, 5, 6])), [(2, 4), (6,)])
def test_complete(self): xform = xf.comp(xf.remove_indexed(self.even_i_pos_v), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [1, 2, -3, 4, 5])), [(2, -3), (4,)])
def test_complete(self): xform = xf.comp(xf.remove(lambda x: x % 2 == 0), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [1, 2, 3, 5])), [(1, 3), (5,)])
def test_partition_step_fraction(self): with self.assertRaises(ValueError): xf.partition_all(1, 1.5)
def test_complete(self): xform = xf.comp(xf.filter_indexed(self.even_i_pos_v), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [-1, 2, 3, 4, 5, 6, 7])), [(3, 5), (7,)])
def test_complete(self): xform = xf.comp(xf.map_indexed(lambda i, x: {i: x}), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, ['zero', 'one', 'two'])), [({0: 'zero'}, {1: 'one'}), ({2: 'two'},)])
def test_arity_zero(self): self.assertEqual(xf.partition_all(1)(lambda: 'success')(), 'success')
def test_complete(self): xform = xf.comp(xf.reductions(lambda x, y: x + y, 1), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [2, 3])), [(1, 3), (6,)])
def test_partition_step_neg(self): with self.assertRaises(ValueError): xf.partition_all(1, -1)
def test_complete(self): xform = xf.comp(xf.interpose('s'), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [1, 2])), [(1, 's'), (2,)])
def test_complete(self): xform = xf.comp(xf.cat, xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [[1, 2], [3]])), [(1, 2), (3,)])
def test_partition_with_smaller_step(self): xform = xf.partition_all(3, 1) self.assertEqual(list(xf.xiter(xform, [1, 2, 3, 4, 5])), [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5), (5,)])
def test_complete(self): xform = xf.comp(xf.mapcat(lambda x: [x, x * 2, x * 3]), xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [1])), [(1, 2), (3,)])
def test_partition_with_smaller_step_reduced_during_complete(self): xform = xf.comp(xf.partition_all(3, 1), xf.take(4)) self.assertEqual(list(xf.xiter(xform, [1, 2, 3, 4, 5])), [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5)])
def test_reduced_without_step(self): xform = xf.comp(xf.partition_all(1), xf.take(2)) self.assertEqual(list(xf.xiter(xform, range(12))), [(0,), (1,)])
def test_complete(self): result = xf.itransduce(xf.partition_all(2), xf.append, [1, 2, 3]) self.assertEqual(result, [(1, 2), (3,)])
def test_complete(self): xform = xf.comp(xf.distinct, xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [1, 2, 1, 2, 3, 3])), [(1, 2), (3,)])
def test_close_does_not_flush_xform_with_pending_puts(self): ch = self.chan(1, xf.partition_all(2)) for i in range(3): ch.f_put(i) ch.close() self.assertEqual(b_list(ch), [(0, 1), (2, )])
def test_reduced_with_step(self): xform = xf.comp(xf.partition_all(2, 1), xf.take(1)) self.assertEqual(list(xf.xiter(xform, [1, 2, 3])), [(1, 2)])
def test_reductions_init_only_complete(self): xform = xf.comp(xf.reductions(lambda x, y: x + y, [1, 2, 3]), xf.cat, xf.partition_all(2)) self.assertEqual(list(xf.xiter(xform, [])), [(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,)])