pyiter.shuffling

 1from typing import Iterable, Iterator, Optional, Union
 2from random import Random
 3from .transform import Transform, T
 4
 5
 6class ShufflingTransform(Transform[T, T]):
 7    """
 8    A transform that shuffles the elements of an iterable.
 9    """
10
11    def __init__(self, iter: Iterable[T], random: Optional[Union[Random, str, int]] = None):
12        super().__init__(iter)
13        if random is None or isinstance(random, (str, int)):
14            self.random = Random(random)
15        else:
16            self.random = random
17
18    def __do_iter__(self) -> Iterator[T]:
19        lst = list(self.iter)
20        self.random.shuffle(lst)
21        yield from lst
class ShufflingTransform(pyiter.transform.Transform[~T, ~T]):
 7class ShufflingTransform(Transform[T, T]):
 8    """
 9    A transform that shuffles the elements of an iterable.
10    """
11
12    def __init__(self, iter: Iterable[T], random: Optional[Union[Random, str, int]] = None):
13        super().__init__(iter)
14        if random is None or isinstance(random, (str, int)):
15            self.random = Random(random)
16        else:
17            self.random = random
18
19    def __do_iter__(self) -> Iterator[T]:
20        lst = list(self.iter)
21        self.random.shuffle(lst)
22        yield from lst

A transform that shuffles the elements of an iterable.