dev4py.utils.iterables
1# Copyright 2022 the original author or authors (i.e.: St4rG00se for Dev4py). 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from itertools import islice 16from typing import Iterator, Iterable 17 18from dev4py.utils.objects import require_non_none 19from dev4py.utils.types import V 20 21 22def get_chunks(values: Iterable[V], chunksize: int) -> Iterator[list[V]]: 23 """ 24 Returns a generator of chunk where values will be chopped into chunks of size chunksize 25 26 Args: 27 values: The values to chunk 28 chunksize: The chunk size 29 30 Returns: 31 Iterator[list[V]]: The chunk iterator 32 33 Raises: 34 TypeError: if values or chunksize is None 35 ValueError: if chunksize is negative 36 """ 37 require_non_none(chunksize) 38 it = iter(require_non_none(values)) 39 while True: 40 chunk = list(islice(it, chunksize)) 41 if not chunk: 42 return 43 yield chunk
def
get_chunks(values: Iterable[~V], chunksize: int) -> Iterator[list[~V]]:
23def get_chunks(values: Iterable[V], chunksize: int) -> Iterator[list[V]]: 24 """ 25 Returns a generator of chunk where values will be chopped into chunks of size chunksize 26 27 Args: 28 values: The values to chunk 29 chunksize: The chunk size 30 31 Returns: 32 Iterator[list[V]]: The chunk iterator 33 34 Raises: 35 TypeError: if values or chunksize is None 36 ValueError: if chunksize is negative 37 """ 38 require_non_none(chunksize) 39 it = iter(require_non_none(values)) 40 while True: 41 chunk = list(islice(it, chunksize)) 42 if not chunk: 43 return 44 yield chunk
Returns a generator of chunk where values will be chopped into chunks of size chunksize
Arguments:
- values: The values to chunk
- chunksize: The chunk size
Returns:
Iterator[list[V]]: The chunk iterator
Raises:
- TypeError: if values or chunksize is None
- ValueError: if chunksize is negative