Add chunks function.

Implementing a generator to split Lists into chunks.
This commit is contained in:
Bernd Zeimetz 2021-04-24 13:26:40 +02:00
parent 191a31db30
commit 4d1613a432

View File

@ -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])