From a1c1a9442730c20364d3765f3d4de64c4fd8ce36 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Tue, 14 Jul 2020 12:15:01 -0700 Subject: Remove redundant f-strings This fixes a new lint error (F541) introduced by flake8 3.8. --- bot/utils/decorators.py | 2 +- bot/utils/pagination.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'bot/utils') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index 519e61a9..9e6ef73d 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -285,7 +285,7 @@ def locked() -> t.Union[t.Callable, None]: embed = Embed() embed.colour = Colour.red() - log.debug(f"User tried to invoke a locked command.") + log.debug("User tried to invoke a locked command.") embed.description = ( "You're already using this command. Please wait until " "it is done before you use it again." diff --git a/bot/utils/pagination.py b/bot/utils/pagination.py index 9a7a0382..a4d0cc56 100644 --- a/bot/utils/pagination.py +++ b/bot/utils/pagination.py @@ -128,7 +128,7 @@ class LinePaginator(Paginator): if not lines: if exception_on_empty_embed: - log.exception(f"Pagination asked for empty lines iterable") + log.exception("Pagination asked for empty lines iterable") raise EmptyPaginatorEmbed("No lines to paginate") log.debug("No lines to add to paginator, adding '(nothing to display)' message") @@ -335,7 +335,7 @@ class ImagePaginator(Paginator): if not pages: if exception_on_empty_embed: - log.exception(f"Pagination asked for empty image list") + log.exception("Pagination asked for empty image list") raise EmptyPaginatorEmbed("No images to paginate") log.debug("No images to add to paginator, adding '(no images to display)' message") -- cgit v1.2.3 From 434e9a65529d77a0954255781c8792de60ab28d6 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Wed, 9 Sep 2020 15:49:05 -0700 Subject: Added RandomCycle utility to jump between set indexes repeatedly. --- bot/exts/evergreen/conversationstarters.py | 13 +++---------- bot/utils/randomization.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 bot/utils/randomization.py (limited to 'bot/utils') diff --git a/bot/exts/evergreen/conversationstarters.py b/bot/exts/evergreen/conversationstarters.py index 68790dca..ebefc53e 100644 --- a/bot/exts/evergreen/conversationstarters.py +++ b/bot/exts/evergreen/conversationstarters.py @@ -1,5 +1,3 @@ -import itertools -import random from pathlib import Path import yaml @@ -8,6 +6,7 @@ from discord.ext import commands from bot.constants import WHITELISTED_CHANNELS from bot.utils.decorators import override_in_channel +from bot.utils.randomization import RandomCycle SUGGESTION_FORM = 'https://forms.gle/zw6kkJqv8U43Nfjg9' @@ -25,14 +24,8 @@ with Path("bot/resources/evergreen/py_topics.yaml").open("r", encoding="utf8") a ALL_ALLOWED_CHANNELS = [channel_id for channel_id in PY_TOPICS.keys()] + list(WHITELISTED_CHANNELS) # Putting all topics into one dictionary and shuffling lists to reduce same-topic repetitions. -TOPICS = {} -for k, v in {**{'default': STARTERS}, **PY_TOPICS}.items(): - if len(v): - random.shuffle(v) - TOPICS[k] = itertools.cycle(v) - - else: - TOPICS[k] = False +all_topics = {'default': STARTERS, **PY_TOPICS} +TOPICS = {channel: RandomCycle(topics) if len(topics) else False for channel, topics in all_topics.items()} class ConvoStarters(commands.Cog): diff --git a/bot/utils/randomization.py b/bot/utils/randomization.py new file mode 100644 index 00000000..063d7e1f --- /dev/null +++ b/bot/utils/randomization.py @@ -0,0 +1,19 @@ +import itertools +import random +import typing as t + + +class RandomCycle: + """Cycling through jumping to random indexes in an iterable.""" + + 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] -- cgit v1.2.3 From 189d2e3714cb532f00921d11a9908b39ca1d7955 Mon Sep 17 00:00:00 2001 From: Xithrius <15021300+Xithrius@users.noreply.github.com> Date: Thu, 10 Sep 2020 08:43:47 -0700 Subject: Update bot/utils/randomization.py Co-authored-by: Dennis Pham --- bot/utils/randomization.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'bot/utils') diff --git a/bot/utils/randomization.py b/bot/utils/randomization.py index 063d7e1f..8f47679a 100644 --- a/bot/utils/randomization.py +++ b/bot/utils/randomization.py @@ -4,7 +4,11 @@ import typing as t class RandomCycle: - """Cycling through jumping to random indexes in an iterable.""" + """ + 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) -- cgit v1.2.3