def test_chunks_input_validation(self): with self.assertRaises(AssertionError): list(chunks(1, 2)) with self.assertRaises(AssertionError): list(chunks(range(10), 'hi')) with self.assertRaises(AssertionError): list(chunks(range(10), 2, 'fishsticks'))
def test_chunks_basic_8(self): ''' test the basic usage of chunks ''' l = list(range(30)) self.assertEqual(list(chunks(l, lambda i: i % 5 == 0)), [(0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, 11, 12, 13, 14), (15, 16, 17, 18, 19), (20, 21, 22, 23, 24), (25, 26, 27, 28, 29)])
def test_chunks_basic_6(self): ''' test the basic usage of chunks ''' l = list(range(30)) self.assertEqual(list(chunks(l, 20, output_type=list)), [[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])
def test_chunks_basic_4(self): ''' test the basic usage of chunks ''' l = list(range(30)) self.assertEqual(list(chunks(l, 2, output_type=list)), [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13], [14, 15], [16, 17], [18, 19], [20, 21], [22, 23], [24, 25], [26, 27], [28, 29]])
def test_chunks_basic_3(self): ''' test the basic usage of chunks ''' l = list(range(30)) self.assertEqual(list(chunks(l, 20)), [(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19), (20, 21, 22, 23, 24, 25, 26, 27, 28, 29)])
def test_chunks_basic_9(self): ''' test the basic usage of chunks ''' l = list(range(30)) self.assertEqual(list(chunks(l, lambda i: str(i).startswith('1'))), [(0, ), (1, 2, 3, 4, 5, 6, 7, 8, 9), (10, ), (11, ), (12, ), (13, ), (14, ), (15, ), (16, ), (17, ), (18, ), (19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29)])
def test_chunks_basic_1(self): ''' test the basic usage of chunks ''' l = list(range(30)) self.assertEqual(list(chunks(l, 2)), [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11), (12, 13), (14, 15), (16, 17), (18, 19), (20, 21), (22, 23), (24, 25), (26, 27), (28, 29)])
def test_chunks_ranged(self, test_range=10, chunk_size=2): pipe = chunks(range(test_range), chunk_size) for first_digit in range(0, test_range, chunk_size): expected_chunk = tuple( (i for i in range(first_digit, first_digit + chunk_size) if i < test_range)) chunk = next(pipe) self.assertTrue(isinstance(chunk, tuple), msg='incorrect chunk type') self.assertEqual(len(expected_chunk), len(chunk), msg='incorrect chunk size') self.assertEqual(expected_chunk, chunk, msg='invalid chunk')
def dicts(): strategies = easy_street.strings(), easy_street.ints( ), easy_street.floats(), easy_street.bools() strategies = list( gen.chain(product(strategies, repeat=len(strategies)))) lengths = cycle(list(range(1, 21))) for _ in gen.loop(): for length in lengths: for strat in strategies: yield { k: v for k, v in gen.chunks(islice(strat, length * 2), 2) }
def test_chunks_3_dimensional(self): self.assertEqual(list(chunks(range(16), 2, 2, 2)), [ (((0, 1), (2, 3)), ((4, 5), (6, 7))), (((8, 9), (10, 11)), ((12, 13), (14, 15))), ])
def test_chunks_2_dimensional(self): self.assertEqual(list(chunks(range(18), 3, 3)), [((0, 1, 2), (3, 4, 5), (6, 7, 8)), ((9, 10, 11), (12, 13, 14), (15, 16, 17))])
def test_chunks_output_type(self): for t in (tuple, list, str): self.assertEqual(type(next(chunks(range(10), 2, output_type=t))), t, 'invalid custom output type')
def floats(): non_zero_ints = (i for i in easy_street.ints() if i != 0) stream1 = gen.chain(i[:8] for i in gen.chunks(non_zero_ints, 10)) stream2 = gen.chain(i[:8] for i in gen.chunks(non_zero_ints, 12)) for i in stream1: yield next(stream2) / (1.0 * i)