-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored master branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to GitHub API limits, only the first 60 comments can be shown.
x = retroactive_resolution( | ||
return retroactive_resolution( | ||
augmented_mat[:, 0:columns], augmented_mat[:, columns : columns + 1] | ||
) | ||
|
||
return x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function gaussian_elimination
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
y = [] | ||
for i in range(n): | ||
y.append([]) | ||
y = [[] for _ in range(n)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function main
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
) - Replace unused for index with underscore (
for-index-underscore
) - Replace call to format with f-string (
use-fstring-for-formatting
)
for i in range(0, repeats): | ||
for _ in range(0, repeats): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function SecantMethod
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))]) | ||
create_state_space_tree(sequence, [], 0, [0 for _ in range(len(sequence))]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function generate_all_permutations
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
for i, j in zip(range(row, -1, -1), range(column, len(board))): | ||
if board[i][j] == 1: | ||
return False | ||
return True | ||
return all( | ||
board[i][j] != 1 | ||
for i, j in zip(range(row, -1, -1), range(column, len(board))) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function isSafe
refactored with the following changes:
- Use any() instead of for loop (
use-any
) - Invert any/all to simplify comparisons (
invert-any-all
)
i = 0 | ||
while i < len(s): | ||
for i in range(0, len(s), 3): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function encode_base64
refactored with the following changes:
- Replace while with for (
while-to-for
) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
)
else: | ||
if i == "=": | ||
c += "=" | ||
elif i == "=": | ||
c += "=" | ||
|
||
p = "" | ||
if c == "=": | ||
p = "A" | ||
else: | ||
if c == "==": | ||
p = "AA" | ||
elif c == "==": | ||
p = "AA" | ||
|
||
r = b"" | ||
s = s + p | ||
|
||
i = 0 | ||
while i < len(s): | ||
for i in range(0, len(s), 4): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function decode_base64
refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Simplify conditional into switch-like form [×4] (
switch
) - Replace while with for (
while-to-for
) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
)
num = num - key | ||
num -= key | ||
if num < 0: | ||
num = num + len(LETTERS) | ||
translated = translated + LETTERS[num] | ||
else: | ||
translated = translated + symbol | ||
print("Decryption using Key #%s: %s" % (key, translated)) | ||
print(f"Decryption using Key #{key}: {translated}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function decrypt
refactored with the following changes:
- Replace assignment with augmented assignment (
aug-assign
) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
key = 1 | ||
result = "" | ||
while key <= 94: | ||
for key in range(1, 95): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function brute_force
refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block
) - Replace while with for (
while-to-for
)
if os.path.exists("%s_pubkey.txt" % name) or os.path.exists( | ||
"%s_privkey.txt" % name | ||
if os.path.exists(f"{name}_pubkey.txt") or os.path.exists( | ||
f"{name}_privkey.txt" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function makeKeyFiles
refactored with the following changes:
- Replace interpolated string formatting with f-string [×5] (
replace-interpolation-with-fstring
)
if a == 0: | ||
return b | ||
return gcd(b % a, a) | ||
return b if a == 0 else gcd(b % a, a) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function gcd
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
det_inv = None | ||
for i in range(len(self.key_string)): | ||
if (det * i) % len(self.key_string) == 1: | ||
det_inv = i | ||
break | ||
|
||
det_inv = next( | ||
( | ||
i | ||
for i in range(len(self.key_string)) | ||
if (det * i) % len(self.key_string) == 1 | ||
), | ||
None, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function HillCipher.makeDecryptKey
refactored with the following changes:
- Use the built-in function
next
instead of a for-loop (use-next
)
for i in range(N): | ||
for _ in range(N): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function main
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
dic = dict() | ||
dic = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function mixed_keyword
refactored with the following changes:
- Replace
dict()
with{}
[×2] (dict-literal
) - Simplify division expressions (
simplify-division
) - Replace unused for index with underscore [×2] (
for-index-underscore
) - Use str.join() instead of for loop (
use-join
) - Simplify logical expression using De Morgan identities [×2] (
de-morgan
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
cipher = "" | ||
for letter in message: | ||
if letter != " ": | ||
|
||
cipher += MORSE_CODE_DICT[letter] + " " | ||
else: | ||
|
||
cipher += " " | ||
|
||
return cipher | ||
return "".join( | ||
f"{MORSE_CODE_DICT[letter]} " if letter != " " else " " | ||
for letter in message | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function encrypt
refactored with the following changes:
- Use str.join() instead of for loop (
use-join
) - Replace if statement with if expression (
assign-if-exp
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
password = [random.choice(choices) for i in range(random.randint(10, 20))] | ||
return password | ||
return [random.choice(choices) for _ in range(random.randint(10, 20))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ShuffledShiftCipher.__passcode_creator
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
) - Replace unused for index with underscore (
for-index-underscore
)
keyList = list(key) | ||
lettersList = list(LETTERS) | ||
keyList.sort() | ||
keyList = sorted(key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function checkValidKey
refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block
) - Remove an unnecessary list construction call prior to sorting (
skip-sorted-list-construction
)
tmp = [] | ||
|
||
for character in messagePart: | ||
tmp.append(character2Number[character]) | ||
|
||
tmp = [character2Number[character] for character in messagePart] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function __encryptPart
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
)
decrypted = "" | ||
|
||
for i in range(0, len(message) + 1, period): | ||
a, b, c = __decryptPart(message[i : i + period], character2Number) | ||
|
||
for j in range(0, len(a)): | ||
decrypted_numeric.append(a[j] + b[j] + c[j]) | ||
|
||
for each in decrypted_numeric: | ||
decrypted += number2Character[each] | ||
|
||
return decrypted | ||
decrypted_numeric.extend(a[j] + b[j] + c[j] for j in range(0, len(a))) | ||
return "".join(number2Character[each] for each in decrypted_numeric) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function decryptMessage
refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block
) - Replace a for append loop with list extend (
for-append-to-extend
) - Use str.join() instead of for loop (
use-join
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
print("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted)) | ||
print(f"Encrypted: {encrypted}\nDecrypted: {decrypted}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 120-120
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
key = int(input("Enter key [2-%s]: " % (len(message) - 1))) | ||
key = int(input(f"Enter key [2-{len(message) - 1}]: ")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function main
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
print("File %s does not exist. Quitting..." % inputFile) | ||
print(f"File {inputFile} does not exist. Quitting...") | ||
sys.exit() | ||
if os.path.exists(outputFile): | ||
print("Overwrite %s? [y/n]" % outputFile) | ||
print(f"Overwrite {outputFile}? [y/n]") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function main
refactored with the following changes:
- Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring
)
# This will be returned | ||
ans = [] | ||
|
||
for ch in content: | ||
ans.append(chr(ord(ch) ^ key)) | ||
|
||
return ans | ||
return [chr(ord(ch) ^ key) for ch in content] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function XORCipher.encrypt
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
This removes the following comments ( why? ):
# This will be returned
# This will be returned | ||
ans = [] | ||
|
||
for ch in content: | ||
ans.append(chr(ord(ch) ^ key)) | ||
|
||
return ans | ||
return [chr(ord(ch) ^ key) for ch in content] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function XORCipher.decrypt
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
This removes the following comments ( why? ):
# This will be returned
# This will be returned | ||
ans = "" | ||
|
||
for ch in content: | ||
ans += chr(ord(ch) ^ key) | ||
|
||
return ans | ||
return "".join(chr(ord(ch) ^ key) for ch in content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function XORCipher.encrypt_string
refactored with the following changes:
- Use str.join() instead of for loop (
use-join
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
This removes the following comments ( why? ):
# This will be returned
else: | ||
depth_l_tree = depth_of_tree(tree.left) | ||
depth_r_tree = depth_of_tree(tree.right) | ||
if depth_l_tree > depth_r_tree: | ||
return 1 + depth_l_tree | ||
else: | ||
return 1 + depth_r_tree | ||
depth_l_tree = depth_of_tree(tree.left) | ||
depth_r_tree = depth_of_tree(tree.right) | ||
return 1 + depth_l_tree if depth_l_tree > depth_r_tree else 1 + depth_r_tree |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function depth_of_tree
refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else
) - Replace if statement with if expression (
assign-if-exp
)
if not self.empty(): | ||
# Look for the node with that label | ||
node = self.getNode(label) | ||
# If the node exists | ||
if node is not None: | ||
# If it has no children | ||
if node.getLeft() is None and node.getRight() is None: | ||
self.__reassignNodes(node, None) | ||
node = None | ||
# Has only right children | ||
elif node.getLeft() is None and node.getRight() is not None: | ||
self.__reassignNodes(node, node.getRight()) | ||
# Has only left children | ||
elif node.getLeft() is not None and node.getRight() is None: | ||
self.__reassignNodes(node, node.getLeft()) | ||
# Has two children | ||
else: | ||
# Gets the max value of the left branch | ||
tmpNode = self.getMax(node.getLeft()) | ||
# Deletes the tmpNode | ||
self.delete(tmpNode.getLabel()) | ||
# Assigns the value to the node to delete and keesp tree structure | ||
node.setLabel(tmpNode.getLabel()) | ||
if self.empty(): | ||
return | ||
# Look for the node with that label | ||
node = self.getNode(label) | ||
# If the node exists | ||
if node is not None: | ||
# If it has no children | ||
if node.getLeft() is None and node.getRight() is None: | ||
self.__reassignNodes(node, None) | ||
node = None | ||
# Has only right children | ||
elif node.getLeft() is None and node.getRight() is not None: | ||
self.__reassignNodes(node, node.getRight()) | ||
# Has only left children | ||
elif node.getLeft() is not None and node.getRight() is None: | ||
self.__reassignNodes(node, node.getLeft()) | ||
# Has two children | ||
else: | ||
# Gets the max value of the left branch | ||
tmpNode = self.getMax(node.getLeft()) | ||
# Deletes the tmpNode | ||
self.delete(tmpNode.getLabel()) | ||
# Assigns the value to the node to delete and keesp tree structure | ||
node.setLabel(tmpNode.getLabel()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function BinarySearchTree.delete
refactored with the following changes:
- Add guard clause (
last-if-guard
)
if root is not None: | ||
curr_node = root | ||
else: | ||
# We go deep on the right branch | ||
curr_node = self.getRoot() | ||
curr_node = root if root is not None else self.getRoot() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function BinarySearchTree.getMax
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
This removes the following comments ( why? ):
# We go deep on the right branch
if root is not None: | ||
curr_node = root | ||
else: | ||
# We go deep on the left branch | ||
curr_node = self.getRoot() | ||
curr_node = root if root is not None else self.getRoot() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function BinarySearchTree.getMin
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
This removes the following comments ( why? ):
# We go deep on the left branch
if self.root is None: | ||
return True | ||
return False | ||
return self.root is None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function BinarySearchTree.empty
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
Branch
master
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
master
branch, then run:Help us improve this pull request!