Add chunks function.
Implementing a generator to split Lists into chunks.
This commit is contained in:
		| @@ -6,7 +6,7 @@ import logging | |||||||
| import re | import re | ||||||
| from datetime import datetime | from datetime import datetime | ||||||
| from pathlib import Path | from pathlib import Path | ||||||
| from typing import Any | from typing import Any, Iterator, List | ||||||
| from typing.io import IO | from typing.io import IO | ||||||
|  |  | ||||||
| import rapidjson | import rapidjson | ||||||
| @@ -202,3 +202,14 @@ def render_template_with_fallback(templatefile: str, templatefallbackfile: str, | |||||||
|         return render_template(templatefile, arguments) |         return render_template(templatefile, arguments) | ||||||
|     except TemplateNotFound: |     except TemplateNotFound: | ||||||
|         return render_template(templatefallbackfile, arguments) |         return render_template(templatefallbackfile, arguments) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def chunks(lst: List[Any], n: int) -> Iterator[List[Any]]: | ||||||
|  |     """ | ||||||
|  |     Split lst into chunks of the size n. | ||||||
|  |     :param lst: list to split into chunks | ||||||
|  |     :param n: number of max elements per chunk | ||||||
|  |     :return: None | ||||||
|  |     """ | ||||||
|  |     for chunk in range(0, len(lst), n): | ||||||
|  |         yield (lst[chunk:chunk + n]) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user