Commit 6eb0b0db authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

add function to generate chunks iterator from list

parent 539cf54c
def generate_chunks(full_list, chunk_size):
"""Yield successive n-sized chunks from full_list."""
for i in range(0, len(full_list), chunk_size):
yield full_list[i:i + chunk_size]
from unittest import TestCase
from metagenedb.common.utils.chunks import generate_chunks
class TestChunks(TestCase):
def setUp(self):
self.full_list = range(0, 10)
def test_chunks(self):
chunk_size = 5
for i in generate_chunks(self.full_list, chunk_size):
self.assertEqual(len(i), 5)
def test_chunks_last_truncated(self):
chunk_size = 4
chunks = list(generate_chunks(self.full_list, chunk_size))
self.assertEqual(len(chunks), 3)
self.assertEqual(len(chunks[-1]), 2)
def test_chunks_no_chunks(self):
chunk_size = 14
chunks = list(generate_chunks(self.full_list, chunk_size))
self.assertEqual(len(chunks), 1)
self.assertEqual(len(chunks[-1]), 10)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment