diff options
author | 2020-09-24 18:54:10 +0300 | |
---|---|---|
committer | 2020-09-24 18:54:10 +0300 | |
commit | 69d2292ec4b3fb2dc83f291ef1ed7bc86eabfd09 (patch) | |
tree | b0c3c0991eef3fc278a0977baf129a7205ff469c /bot/utils | |
parent | Merge remote-tracking branch 'origin/tic-tac-toe' into tic-tac-toe (diff) | |
parent | Merge pull request #456 from Anubhav1603/update_dpy (diff) |
Merge branch 'master' into tic-tac-toe
Diffstat (limited to 'bot/utils')
-rw-r--r-- | bot/utils/decorators.py | 2 | ||||
-rw-r--r-- | bot/utils/pagination.py | 4 | ||||
-rw-r--r-- | bot/utils/persist.py | 5 | ||||
-rw-r--r-- | bot/utils/randomization.py | 23 |
4 files changed, 30 insertions, 4 deletions
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") diff --git a/bot/utils/persist.py b/bot/utils/persist.py index d78e5420..1e178569 100644 --- a/bot/utils/persist.py +++ b/bot/utils/persist.py @@ -25,13 +25,16 @@ def make_persistent(file_path: Path) -> Path: as otherwise only one datafile can be persistent and will be returned for both cases. + Ensure that all open files are using explicit appropriate encoding to avoid + encoding errors from diffent OS systems. + Example Usage: >>> import json >>> template_datafile = Path("bot", "resources", "evergreen", "myfile.json") >>> path_to_persistent_file = make_persistent(template_datafile) >>> print(path_to_persistent_file) data/evergreen/myfile.json - >>> with path_to_persistent_file.open("w+") as f: + >>> with path_to_persistent_file.open("w+", encoding="utf8") as f: >>> data = json.load(f) """ # ensure the persistent data directory exists 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] |