From 0dba9f3d331a8d9996380fccf83c4bcf57e62191 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sun, 16 Oct 2022 20:55:09 +0100 Subject: [PATCH 1/6] ci: Add ``flake8-pep3101`` plugin to ``pre-commit`` --- .pre-commit-config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3455135653cf..b5133ee70613 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -40,6 +40,7 @@ repos: - flake8-bugbear - flake8-builtins - flake8-comprehensions + - flake8-pep3101 - pep8-naming - repo: https://github.com/pre-commit/mirrors-mypy From 95e960642125a2cecc0cc5b268a6b5f4bfc4d352 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sun, 16 Oct 2022 21:14:09 +0100 Subject: [PATCH 2/6] refactor: Remove all modulo string formatting --- ciphers/elgamal_key_generator.py | 7 +++---- ciphers/rsa_cipher.py | 6 ++---- ciphers/rsa_key_generator.py | 3 +-- dynamic_programming/edit_distance.py | 4 ++-- genetic_algorithm/basic_string.py | 4 ++-- graphs/minimum_spanning_tree_boruvka.py | 2 +- machine_learning/linear_regression.py | 2 +- matrix/sherman_morrison.py | 6 +++--- neural_network/back_propagation_neural_network.py | 2 +- neural_network/convolution_neural_network.py | 2 +- 10 files changed, 17 insertions(+), 21 deletions(-) diff --git a/ciphers/elgamal_key_generator.py b/ciphers/elgamal_key_generator.py index 4d72128aed52..abb86c1a0dc5 100644 --- a/ciphers/elgamal_key_generator.py +++ b/ciphers/elgamal_key_generator.py @@ -41,9 +41,8 @@ def make_key_files(name: str, key_size: int) -> None: if os.path.exists(f"{name}_pubkey.txt") or os.path.exists(f"{name}_privkey.txt"): print("\nWARNING:") print( - '"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n' + f'"{name}_pubkey.txt" or "{name}_privkey.txt" already exists. \n' "Use a different name or delete these files and re-run this program." - % (name, name) ) sys.exit() @@ -51,12 +50,12 @@ def make_key_files(name: str, key_size: int) -> None: print(f"\nWriting public key to file {name}_pubkey.txt...") with open(f"{name}_pubkey.txt", "w") as fo: fo.write( - "%d,%d,%d,%d" % (public_key[0], public_key[1], public_key[2], public_key[3]) + ",".join((public_key[0], public_key[1], public_key[2], public_key[3])) ) print(f"Writing private key to file {name}_privkey.txt...") with open(f"{name}_privkey.txt", "w") as fo: - fo.write("%d,%d" % (private_key[0], private_key[1])) + fo.write(",".join((private_key[0], private_key[1]))) def main() -> None: diff --git a/ciphers/rsa_cipher.py b/ciphers/rsa_cipher.py index de26992f5eeb..4d9e03e6e299 100644 --- a/ciphers/rsa_cipher.py +++ b/ciphers/rsa_cipher.py @@ -76,10 +76,9 @@ def encrypt_and_write_to_file( key_size, n, e = read_key_file(key_filename) if key_size < block_size * 8: sys.exit( - "ERROR: Block size is %s bits and key size is %s bits. The RSA cipher " + f"ERROR: Block size is {block_size * 8} bits and key size is {key_size} bits. The RSA cipher " "requires the block size to be equal to or greater than the key size. " "Either decrease the block size or use different keys." - % (block_size * 8, key_size) ) encrypted_blocks = [str(i) for i in encrypt_message(message, (n, e), block_size)] @@ -101,10 +100,9 @@ def read_from_file_and_decrypt(message_filename: str, key_filename: str) -> str: if key_size < block_size * 8: sys.exit( - "ERROR: Block size is %s bits and key size is %s bits. The RSA cipher " + f"ERROR: Block size is {block_size * 8} bits and key size is {key_size} bits. The RSA cipher " "requires the block size to be equal to or greater than the key size. " "Did you specify the correct key file and encrypted file?" - % (block_size * 8, key_size) ) encrypted_blocks = [] diff --git a/ciphers/rsa_key_generator.py b/ciphers/rsa_key_generator.py index f64bc7dd0557..2573ed01387b 100644 --- a/ciphers/rsa_key_generator.py +++ b/ciphers/rsa_key_generator.py @@ -37,9 +37,8 @@ def make_key_files(name: str, key_size: int) -> None: if os.path.exists(f"{name}_pubkey.txt") or os.path.exists(f"{name}_privkey.txt"): print("\nWARNING:") print( - '"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n' + f'"{name}_pubkey.txt" or "{name}_privkey.txt" already exists. \n' "Use a different name or delete these files and re-run this program." - % (name, name) ) sys.exit() diff --git a/dynamic_programming/edit_distance.py b/dynamic_programming/edit_distance.py index d63e559e30da..fe23431a7ea6 100644 --- a/dynamic_programming/edit_distance.py +++ b/dynamic_programming/edit_distance.py @@ -99,7 +99,7 @@ def min_distance_bottom_up(word1: str, word2: str) -> int: S2 = input("Enter the second string: ").strip() print() - print("The minimum Edit Distance is: %d" % (solver.solve(S1, S2))) - print("The minimum Edit Distance is: %d" % (min_distance_bottom_up(S1, S2))) + print(f"The minimum Edit Distance is: {solver.solve(S1, S2)}") + print(f"The minimum Edit Distance is: {min_distance_bottom_up(S1, S2)}") print() print("*************** End of Testing Edit Distance DP Algorithm ***************") diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index bd7d8026866c..d2d305189983 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -172,7 +172,7 @@ def mutate(child: str) -> str: " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" "nopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\" ) + generation, population, target = basic(target_str, genes_list) print( - "\nGeneration: %s\nTotal Population: %s\nTarget: %s" - % basic(target_str, genes_list) + f"\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}" ) diff --git a/graphs/minimum_spanning_tree_boruvka.py b/graphs/minimum_spanning_tree_boruvka.py index 32548b2ecb6c..6c72615cc729 100644 --- a/graphs/minimum_spanning_tree_boruvka.py +++ b/graphs/minimum_spanning_tree_boruvka.py @@ -63,7 +63,7 @@ def __str__(self): for tail in self.adjacency: for head in self.adjacency[tail]: weight = self.adjacency[head][tail] - string += "%d -> %d == %d\n" % (head, tail, weight) + string += f"{head} -> {tail} == {weight}\n" return string.rstrip("\n") def get_edges(self): diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index 85fdfb0005ac..92ab91c01b95 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -82,7 +82,7 @@ def run_linear_regression(data_x, data_y): for i in range(0, iterations): theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta) error = sum_of_square_error(data_x, data_y, len_data, theta) - print("At Iteration %d - Error is %.5f " % (i + 1, error)) + print(f"At Iteration {i + 1} - Error is {error:.5f}") return theta diff --git a/matrix/sherman_morrison.py b/matrix/sherman_morrison.py index 29c9b3381b55..39eddfed81f3 100644 --- a/matrix/sherman_morrison.py +++ b/matrix/sherman_morrison.py @@ -31,14 +31,14 @@ def __str__(self) -> str: """ # Prefix - s = "Matrix consist of %d rows and %d columns\n" % (self.row, self.column) + s = f"Matrix consist of {self.row} rows and {self.column} columns\n" # Make string identifier max_element_length = 0 for row_vector in self.array: for obj in row_vector: max_element_length = max(max_element_length, len(str(obj))) - string_format_identifier = "%%%ds" % (max_element_length,) + string_format_identifier = f"%{max_element_length}s" # Make string and return def single_line(row_vector: list[float]) -> str: @@ -252,7 +252,7 @@ def test1() -> None: v[0, 0], v[1, 0], v[2, 0] = 4, -2, 5 print(f"u is {u}") print(f"v is {v}") - print("uv^T is %s" % (u * v.transpose())) + print(f"uv^T is {u * v.transpose()}") # Sherman Morrison print(f"(a + uv^T)^(-1) is {ainv.sherman_morrison(u, v)}") diff --git a/neural_network/back_propagation_neural_network.py b/neural_network/back_propagation_neural_network.py index 23b818b0f3cf..cb47b829010c 100644 --- a/neural_network/back_propagation_neural_network.py +++ b/neural_network/back_propagation_neural_network.py @@ -117,7 +117,7 @@ def build(self): def summary(self): for i, layer in enumerate(self.layers[:]): - print("------- layer %d -------" % i) + print(f"------- layer {i} -------") print("weight.shape ", np.shape(layer.weight)) print("bias.shape ", np.shape(layer.bias)) diff --git a/neural_network/convolution_neural_network.py b/neural_network/convolution_neural_network.py index 9dfb6d091412..bd0550212157 100644 --- a/neural_network/convolution_neural_network.py +++ b/neural_network/convolution_neural_network.py @@ -219,7 +219,7 @@ def train( mse = 10000 while rp < n_repeat and mse >= error_accuracy: error_count = 0 - print("-------------Learning Time %d--------------" % rp) + print(f"-------------Learning Time {rp}--------------") for p in range(len(datas_train)): # print('------------Learning Image: %d--------------'%p) data_train = np.asmatrix(datas_train[p]) From aa515e59e52062fa8106c4343ef815c6bf12a8d4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Oct 2022 20:17:20 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/elgamal_key_generator.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ciphers/elgamal_key_generator.py b/ciphers/elgamal_key_generator.py index abb86c1a0dc5..b7fc6181fe44 100644 --- a/ciphers/elgamal_key_generator.py +++ b/ciphers/elgamal_key_generator.py @@ -49,9 +49,7 @@ def make_key_files(name: str, key_size: int) -> None: public_key, private_key = generate_key(key_size) print(f"\nWriting public key to file {name}_pubkey.txt...") with open(f"{name}_pubkey.txt", "w") as fo: - fo.write( - ",".join((public_key[0], public_key[1], public_key[2], public_key[3])) - ) + fo.write(",".join((public_key[0], public_key[1], public_key[2], public_key[3]))) print(f"Writing private key to file {name}_privkey.txt...") with open(f"{name}_privkey.txt", "w") as fo: From eeac4f9729e7c1d5734def7d0ea2d53005f24172 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sun, 16 Oct 2022 21:20:21 +0100 Subject: [PATCH 4/6] refactor: Remove ``flake8-pep3101`` plugin from ``pre-commit`` --- .pre-commit-config.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a75acf9d2651..39af0f3b4370 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -41,7 +41,6 @@ repos: - flake8-builtins - flake8-broken-line - flake8-comprehensions - - flake8-pep3101 - pep8-naming - repo: https://github.com/pre-commit/mirrors-mypy From 573b323f1b4219e66ee90fd21cbf75bc7523cc23 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sun, 16 Oct 2022 21:31:44 +0100 Subject: [PATCH 5/6] revert: Revert to modulo formatting --- ciphers/rsa_cipher.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ciphers/rsa_cipher.py b/ciphers/rsa_cipher.py index 4d9e03e6e299..de26992f5eeb 100644 --- a/ciphers/rsa_cipher.py +++ b/ciphers/rsa_cipher.py @@ -76,9 +76,10 @@ def encrypt_and_write_to_file( key_size, n, e = read_key_file(key_filename) if key_size < block_size * 8: sys.exit( - f"ERROR: Block size is {block_size * 8} bits and key size is {key_size} bits. The RSA cipher " + "ERROR: Block size is %s bits and key size is %s bits. The RSA cipher " "requires the block size to be equal to or greater than the key size. " "Either decrease the block size or use different keys." + % (block_size * 8, key_size) ) encrypted_blocks = [str(i) for i in encrypt_message(message, (n, e), block_size)] @@ -100,9 +101,10 @@ def read_from_file_and_decrypt(message_filename: str, key_filename: str) -> str: if key_size < block_size * 8: sys.exit( - f"ERROR: Block size is {block_size * 8} bits and key size is {key_size} bits. The RSA cipher " + "ERROR: Block size is %s bits and key size is %s bits. The RSA cipher " "requires the block size to be equal to or greater than the key size. " "Did you specify the correct key file and encrypted file?" + % (block_size * 8, key_size) ) encrypted_blocks = [] From 2366e38aed5d13cb12579e931b60f8115c9fce3a Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sun, 16 Oct 2022 21:32:08 +0100 Subject: [PATCH 6/6] refactor: Use f-string instead of `join` --- ciphers/elgamal_key_generator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ciphers/elgamal_key_generator.py b/ciphers/elgamal_key_generator.py index b7fc6181fe44..17ba55c0d013 100644 --- a/ciphers/elgamal_key_generator.py +++ b/ciphers/elgamal_key_generator.py @@ -49,11 +49,11 @@ def make_key_files(name: str, key_size: int) -> None: public_key, private_key = generate_key(key_size) print(f"\nWriting public key to file {name}_pubkey.txt...") with open(f"{name}_pubkey.txt", "w") as fo: - fo.write(",".join((public_key[0], public_key[1], public_key[2], public_key[3]))) + fo.write(f"{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}") print(f"Writing private key to file {name}_privkey.txt...") with open(f"{name}_privkey.txt", "w") as fo: - fo.write(",".join((private_key[0], private_key[1]))) + fo.write(f"{private_key[0]},{private_key[1]}") def main() -> None: