iterutils.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2020 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from itertools import islice
  17. from typing import Iterable, Iterator, Sequence, Tuple, TypeVar
  18. T = TypeVar("T")
  19. def batch_iter(iterable: Iterable[T], size: int) -> Iterator[Tuple[T]]:
  20. """batch an iterable up into tuples with a maximum size
  21. Args:
  22. iterable (iterable): the iterable to slice
  23. size (int): the maximum batch size
  24. Returns:
  25. an iterator over the chunks
  26. """
  27. # make sure we can deal with iterables like lists too
  28. sourceiter = iter(iterable)
  29. # call islice until it returns an empty tuple
  30. return iter(lambda: tuple(islice(sourceiter, size)), ())
  31. ISeq = TypeVar("ISeq", bound=Sequence, covariant=True)
  32. def chunk_seq(iseq: ISeq, maxlen: int) -> Iterable[ISeq]:
  33. """Split the given sequence into chunks of the given size
  34. The last chunk may be shorter than the given size.
  35. If the input is empty, no chunks are returned.
  36. """
  37. return (iseq[i : i + maxlen] for i in range(0, len(iseq), maxlen))