Coverage for src/qollib/collections/chunking.py: 100%
11 statements
« prev ^ index » next coverage.py v7.5.4, created at 2024-06-24 08:41 +0000
« prev ^ index » next coverage.py v7.5.4, created at 2024-06-24 08:41 +0000
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4The `chunking` utility provides methods to break huge collections in chunks
6@author: hoelken
7"""
10def chunker(seq: list, size: int) -> list:
11 """
12 Generates chunks (slices) from a given sequence
14 ### Params
15 - seq: the list to chunk
16 - size: the size of the chunks
18 ### Returns
19 A list of lists where each list has the
20 length of the requested chunk size (maybe except the last one)
21 """
22 if size < 1:
23 return [seq]
24 return [seq[pos:pos + size] for pos in range(0, len(seq), size)]
27def indexed_chunks(seq: list, size: int) -> dict:
28 """
29 Generates indexed chunks (slices) from a given sequence
30 ### Params
31 - seq: List the list to chunk
32 - size: Integer the size of the chunks
34 ### Returns
35 A dictionary with the index as key and the corresponding chunk as value.
36 The length of the value arrays is the requested chunk size (maybe except the last one)
37 """
38 idx = 0
39 indexed = {}
40 for chunk in chunker(seq, size):
41 indexed[idx] = chunk
42 idx += 1
43 return indexed