diff --git a/swh/loader/tar/tests/test_utils.py b/swh/loader/tar/tests/test_utils.py index e2786a0..2b965e9 100644 --- a/swh/loader/tar/tests/test_utils.py +++ b/swh/loader/tar/tests/test_utils.py @@ -1,44 +1,43 @@ # Copyright (C) 2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import random import unittest from swh.loader.tar import utils class UtilsLib(unittest.TestCase): def assert_ok(self, actual_data, expected_data): """Check that actual_data and expected_data matched. Actual data is a random block of data. We want to check its contents match exactly but not the order within. """ out = [] random.shuffle(expected_data) for d in actual_data: self.assertIn(d, expected_data) out.append(d) self.assertEqual(len(out), len(expected_data)) def test_random_block(self): _input = list(range(0, 9)) # given actual_data = utils.random_blocks(_input, 2) self.assert_ok(actual_data, expected_data=_input) def test_random_block2(self): _input = list(range(9, 0, -1)) # given actual_data = utils.random_blocks(_input, 4) self.assert_ok(actual_data, expected_data=_input) def test_random_block_with_fillvalue(self): _input = [(i, i+1) for i in range(0, 9)] - actual_data = utils.random_blocks(_input, 2, - fillvalue=(None, None)) + actual_data = utils.random_blocks(_input, 2) self.assert_ok(actual_data, expected_data=_input) diff --git a/swh/loader/tar/utils.py b/swh/loader/tar/utils.py index 232832d..2b46989 100644 --- a/swh/loader/tar/utils.py +++ b/swh/loader/tar/utils.py @@ -1,37 +1,35 @@ # Copyright (C) 2015-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import random from swh.core.utils import grouper -def random_blocks(iterable, block=100, fillvalue=None): - """Given an iterable: +def random_blocks(iterable, block=100): + """Randomize iterable per block of size block. + + Given an iterable: - slice the iterable in data set of block-sized elements - randomized the block-sized elements - yield each element of that randomized block-sized - continue onto the next block-sized block Args: iterable (Iterable): an iterable block (int): number of elements per block - fillvalue (Optional[Something]): value to use as fill-in - values (typically for the last loop, the iterable might be - less than n elements). None by default but could be anything - relevant for the caller (e.g tuple of (None, None)) Yields: - random elements per size of block + random element of the iterable """ count = 0 - for iter_ in grouper(iterable, block, fillvalue=fillvalue): + for iter_ in grouper(iterable, block): count += 1 lst = list(iter_) random.shuffle(lst) for e in lst: yield e