Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd tests and type hints to hill_cipher.py #1862
Conversation
|
Awesome! Thanks for doing this. I changed the title so that reviewers know what this pull request does without having to find, open up, and read an issue. The commit |
| """ | ||
| self.encrypt_key = self.modulus(encrypt_key) # mod36 calc's on the encrypt key | ||
| self.check_determinant() # validate the determinant of the encryption key | ||
| self.decrypt_key = None | ||
| self.break_key = encrypt_key.shape[0] | ||
|
|
||
| def replace_letters(self, letter): |
This comment has been minimized.
This comment has been minimized.
cclauss
Apr 13, 2020
Member
Need type hints. This function does not really replace anything. Can we rename to something more self documenting? get_letter_by_letter or lookup_letter_by_letter. Alternatively could we have a single function instead of two functions? get_letter or lookup_letter could start with isinstance(key, int) and return the right result if key was a str or int.
| batch_vec = numpy.matrix([batch_vec]).T | ||
| batch_encrypted = self.modulus(self.encrypt_key.dot(batch_vec)).T.tolist()[ | ||
| 0 | ||
| ] | ||
| encrypted_batch = "".join(list(map(self.replaceNumbers, batch_encrypted))) | ||
| encrypted_batch = "".join( | ||
| list(map(HillCipher(self.encrypt_key).replace_numbers, batch_encrypted)) |
This comment has been minimized.
This comment has been minimized.
| """ | ||
| return self.key_string.index(letter) | ||
|
|
||
| def replace_numbers(self, num): |
This comment has been minimized.
This comment has been minimized.
cclauss
Apr 13, 2020
•
Member
Type hints? Combine with previous function? Or replace with __index__().
| >>> | ||
| """ | ||
| text: list = list(text.upper()) | ||
| text: list = [char for char in text if char in self.key_string] |
This comment has been minimized.
This comment has been minimized.
cclauss
Apr 13, 2020
Member
It is too confusing to use the same variable as both a str and as a list. Can we replace lines 113 and 114 with:
chars = [char for char in text.upper() if char in self.key_string]
and then change the variable name in the lines below? We do not need to declare the type because CPython is smart enough to know that a list comprehension returns a list.
| @@ -94,26 +120,37 @@ def process_text(self, text): | |||
| return "".join(text) | |||
|
|
|||
| def encrypt(self, text): | |||
This comment has been minimized.
This comment has been minimized.
cclauss
Apr 13, 2020
Member
| def encrypt(self, text): | |
| def encrypt(self, text: str) -> str: |
| batch_vec = numpy.matrix([batch_vec]).T | ||
| batch_decrypted = self.modulus(self.decrypt_key.dot(batch_vec)).T.tolist()[ | ||
| 0 | ||
| ] | ||
| decrypted_batch = "".join(list(map(self.replaceNumbers, batch_decrypted))) | ||
| decrypted_batch = "".join( | ||
| list(map(HillCipher(self.encrypt_key).replace_numbers, batch_decrypted)) |
This comment has been minimized.
This comment has been minimized.
| decrypted += decrypted_batch | ||
|
|
||
| return decrypted | ||
|
|
||
|
|
||
| def main(): | ||
| if __name__ == "__main__": |
This comment has been minimized.
This comment has been minimized.
cclauss
Apr 13, 2020
Member
Doctests do not work in if __name__ == "__main__": so please bring back main().
| N = int(input("Enter the order of the encryption key: ")) | ||
| hill_matrix = [] | ||
| hill_matrix: list = [] |
This comment has been minimized.
This comment has been minimized.
cclauss
Apr 13, 2020
Member
The type hint is not really required here because CPython is smart enough to know that [] is a list. A useful type hint would be hill_matrix: List[int] = [] assuming that from typing import List was added above.
| @@ -42,6 +42,10 @@ | |||
|
|
|||
|
|
|||
| def gcd(a, b): | |||
This comment has been minimized.
This comment has been minimized.
cclauss
Apr 13, 2020
Member
Please add type hints to function parameters and return values. This is where type hints are most valuable/helpful because in other locations CPython is often smart enough to determine the type from the context.
| def gcd(a, b): | |
| def gcd(a: int, b: int) -> int |
| @@ -84,8 +105,13 @@ def check_determinant(self): | |||
| ) | |||
|
|
|||
| def process_text(self, text): | |||
This comment has been minimized.
This comment has been minimized.
cclauss
Apr 13, 2020
Member
| def process_text(self, text): | |
| def process_text(self, text: str) -> str: |
|
Thanks @cclauss apologies for the delay. I will work to get these requested changes made |
mrvnmchm commentedApr 13, 2020
Describe your change:
Adding tests for issue Fixes: #1788
Checklist:
Fixes: #{$ISSUE_NO}.