diff options
| author | 2020-09-11 21:16:21 -0400 | |
|---|---|---|
| committer | 2020-09-11 21:16:21 -0400 | |
| commit | 2ebcdaed2a192fc8147dc17e046ada4e8983189d (patch) | |
| tree | cd2e6cddd9f41e195bf09b21a75d4a55ca0964bc /bot/utils/randomization.py | |
| parent | Merge pull request #429 from python-discord/wolfram-cog (diff) | |
| parent | Renamed from python-extensions to c-extensions, reordered within list. (diff) | |
Merge pull request #435 from python-discord/topic-embeds
Topic embeds
Diffstat (limited to '')
| -rw-r--r-- | bot/utils/randomization.py | 23 | 
1 files changed, 23 insertions, 0 deletions
| diff --git a/bot/utils/randomization.py b/bot/utils/randomization.py new file mode 100644 index 00000000..8f47679a --- /dev/null +++ b/bot/utils/randomization.py @@ -0,0 +1,23 @@ +import itertools +import random +import typing as t + + +class RandomCycle: +    """ +    Cycles through elements from a randomly shuffled iterable, repeating indefinitely. + +    The iterable is reshuffled after each full cycle. +    """ + +    def __init__(self, iterable: t.Iterable) -> None: +        self.iterable = list(iterable) +        self.index = itertools.cycle(range(len(iterable))) + +    def __next__(self) -> t.Any: +        idx = next(self.index) + +        if idx == 0: +            random.shuffle(self.iterable) + +        return self.iterable[idx] | 
