diff --git a/secretpy/cmdecorators/lowercase.py b/secretpy/cmdecorators/lowercase.py index 6af62e1..120df5d 100755 --- a/secretpy/cmdecorators/lowercase.py +++ b/secretpy/cmdecorators/lowercase.py @@ -7,7 +7,7 @@ class LowerCase(AbstractMachineDecorator): def encrypt(self, text): - return self._machine.encrypt(text.lower()) + return self._machine.encrypt(text).lower() def decrypt(self, text): - return self._machine.decrypt(text.lower()) + return self._machine.decrypt(text).lower() diff --git a/secretpy/cmdecorators/save_all.py b/secretpy/cmdecorators/save_all.py index c9900a0..7177a5b 100755 --- a/secretpy/cmdecorators/save_all.py +++ b/secretpy/cmdecorators/save_all.py @@ -14,21 +14,18 @@ def decrypt(self, text): def __crypt(self, text, func): # make lower case and save indexes - upcases = [i for i, char in enumerate(text) if char.isupper()] - txt = list(text) - for i in upcases: - txt[i] = txt[i].lower() + upcases = [i for i, c in enumerate(text) if c.isupper()] + txt = text.lower() - # remove non-alphabet characters and save indexes + # prepare alphabet alphabet = self._machine.get_alphabet() - coords = {c: i for i, letters in enumerate(alphabet) for c in letters} - chars = [i for i, char in enumerate(txt) if char not in coords] - for i in reversed(chars): - del txt[i] + alpha = {c: 1 for letters in alphabet for c in letters} + + # save indexes of non-alphabet characters + chars = [i for i, c in enumerate(txt) if c not in alpha] # execute function - res = func("".join(txt)) - res = list(res) + res = list(func(txt)) # restore non-alphabet characters for i in chars: diff --git a/secretpy/cmdecorators/uppercase.py b/secretpy/cmdecorators/uppercase.py index ed92f7e..bc3034a 100755 --- a/secretpy/cmdecorators/uppercase.py +++ b/secretpy/cmdecorators/uppercase.py @@ -7,9 +7,7 @@ class UpperCase(AbstractMachineDecorator): def encrypt(self, text): - res = self._machine.encrypt(text.lower()) - return res.upper() + return self._machine.encrypt(text).upper() def decrypt(self, text): - res = self._machine.decrypt(text.lower()) - return res.upper() + return self._machine.decrypt(text).upper() diff --git a/secretpy/cryptmachine.py b/secretpy/cryptmachine.py index 4ed007d..9be3185 100755 --- a/secretpy/cryptmachine.py +++ b/secretpy/cryptmachine.py @@ -34,5 +34,5 @@ def __crypt(self, text, func): # prepare alphabet alpha = {c: 1 for letters in self.__alphabet for c in letters} # filter text by alphabet - text = "".join(filter(lambda c: c in alpha, text.lower())) - return func(text, self.__key, self.__alphabet) + txt = "".join(filter(lambda c: c in alpha, text.lower())) + return func(txt, self.__key, self.__alphabet)