diff options
author | 2020-09-18 20:30:56 +0800 | |
---|---|---|
committer | 2020-09-18 20:36:55 +0800 | |
commit | 98718712ab38ecb98970ade58fd1afd6796f8718 (patch) | |
tree | b9d42a0e389d38770c5a412a9efcf1a6586e1344 | |
parent | Explicitly pass left_shift as a keyword argument (diff) |
Make the encryption function top-level
This makes caesar_func a top-level function and renames it to
caesar_cipher.
-rw-r--r-- | bot/exts/evergreen/fun.py | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 5a499270..3fd0f275 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -28,6 +28,26 @@ UWU_WORDS = { } +def caesar_cipher(text: str, offset: int) -> Iterable[str]: + """ + Implements a lazy Caesar Cipher algorithm. + + Encrypts a `text` given a specific integer `offset`. The sign + of the `offset` dictates the direction in which it shifts to, + with a negative value shifting to the left, and a positive + value shifting to the right. + """ + for char in text: + if not char.isascii() or not char.isalpha() or char.isspace(): + yield char + continue + + case_start = 65 if char.isupper() else 97 + true_offset = (ord(char) - case_start + offset) % 26 + + yield chr(case_start + true_offset) + + class Fun(Cog): """A collection of general commands for fun.""" @@ -126,21 +146,9 @@ class Fun(Cog): if left_shift: offset = -offset - def caesar_func(text: str) -> Iterable[str]: - """Implements a lazy Caesar Cipher algorithm.""" - for char in text: - if not char.isascii() or not char.isalpha() or char.isspace(): - yield char - continue - - case_start = 65 if char.isupper() else 97 - true_offset = (ord(char) - case_start + offset) % 26 - - yield chr(case_start + true_offset) - def conversion_func(text: str) -> str: """Encrypts the given string using the Caesar Cipher.""" - return "".join(caesar_func(text)) + return "".join(caesar_cipher(text, offset)) is_message = isinstance(msg, Message) |