Commit 19957e55 authored by David Mendez's avatar David Mendez

Add utils module with function to get list chunks

parent 0e649ba7
Pipeline #91911 passed with stages
in 2 minutes and 50 seconds
Module to test utils functions
import unittest
from app.es_subset_generator import utils
class TestUtilFunctions(unittest.TestCase):
Class to test utils function
def test_gets_list_chunks(self):
Tests it returns chunks from a list
list_size = 100
original_list = list(range(0, list_size))
original_list_size = len(original_list)
for chunk_size in range(1, list_size + 10):
chunks_obtained = list(utils.get_list_chunks(chunk_size, original_list))
total_size_obtained = sum([len(chunk) for chunk in chunks_obtained])
self.assertTrue(total_size_obtained == original_list_size,
msg='The total size of the chunks do not match the size of the original list!')
items_found = set()
for item in original_list:
for chunk in chunks_obtained:
if item in chunk:
for item in original_list:
self.assertIn(item, items_found, msg=f'The item {item} is missing in the chunks!')
Utils functions for handling the subset creation
def get_list_chunks(chunk_size, original_list):
:param chunk_size: size of chunk to obtain
:param original_list: list to split
:return: a generator object with the chunks of the original list with the given size
start_index = 0
end_index = chunk_size
original_list_size = len(original_list)
yield original_list[start_index:end_index]
while end_index < original_list_size:
start_index = end_index
end_index += chunk_size
yield original_list[start_index:end_index]
Markdown is supported
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