Conversation
| if n < 2: # base case | ||
| return n | ||
| return fib2(n - 2) + fib2(n - 1) # recursive case | ||
| return n if n < 2 else fib2(n - 2) + fib2(n - 1) |
There was a problem hiding this comment.
Function fib2 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)
This removes the following comments ( why? ):
# base case
# recursive case
| if n < 2: # base case | ||
| return n | ||
| return fib4(n - 2) + fib4(n - 1) # recursive case | ||
| return n if n < 2 else fib4(n - 2) + fib4(n - 1) |
There was a problem hiding this comment.
Function fib4 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)
This removes the following comments ( why? ):
# base case
# recursive case
| self.bit_string |= 0b11 | ||
| else: | ||
| raise ValueError("Invalid Nucleotide:{}".format(nucleotide)) | ||
| raise ValueError(f"Invalid Nucleotide:{nucleotide}") |
There was a problem hiding this comment.
Function CompressedGene._compress refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting)
| if bits == 0b00: # A | ||
| if bits == 0b00: | ||
| gene += "A" | ||
| elif bits == 0b01: # C | ||
| elif bits == 0b01: | ||
| gene += "C" | ||
| elif bits == 0b10: # G | ||
| elif bits == 0b10: | ||
| gene += "G" | ||
| elif bits == 0b11: # T | ||
| elif bits == 0b11: | ||
| gene += "T" | ||
| else: | ||
| raise ValueError("Invalid bits:{}".format(bits)) | ||
| raise ValueError(f"Invalid bits:{bits}") |
There was a problem hiding this comment.
Function CompressedGene.decompress refactored with the following changes:
- Simplify conditional into switch-like form (
switch) - Replace call to format with f-string. (
use-fstring-for-formatting)
This removes the following comments ( why? ):
# C
# T
# G
# A
| print("original is {} bytes".format(getsizeof(original))) | ||
| print(f"original is {getsizeof(original)} bytes") | ||
| compressed: CompressedGene = CompressedGene(original) # compress | ||
| print("compressed is {} bytes".format(getsizeof(compressed.bit_string))) | ||
| print(f"compressed is {getsizeof(compressed.bit_string)} bytes") | ||
| print(compressed) # decompress | ||
| print("original and decompressed are the same: {}".format(original == compressed.decompress())) No newline at end of file | ||
| print( | ||
| f"original and decompressed are the same: {original == compressed.decompress()}" | ||
| ) No newline at end of file |
There was a problem hiding this comment.
Lines 60-64 refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting)
| possible_digits: Dict[str, List[int]] = {} | ||
| for letter in letters: | ||
| possible_digits[letter] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
| possible_digits: Dict[str, List[int]] = { | ||
| letter: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for letter in letters | ||
| } | ||
|
|
There was a problem hiding this comment.
Lines 49-51 refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension)
| def generate_grid(rows: int, columns: int) -> Grid: | ||
| # initialize grid with random letters | ||
| return [[choice(ascii_uppercase) for c in range(columns)] for r in range(rows)] | ||
| return [[choice(ascii_uppercase) for _ in range(columns)] for _ in range(rows)] |
There was a problem hiding this comment.
Function generate_grid refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| locations: Dict[str, List[List[GridLocation]]] = {} | ||
| for word in words: | ||
| locations[word] = generate_domain(word, grid) | ||
| locations: Dict[str, List[List[GridLocation]]] = { | ||
| word: generate_domain(word, grid) for word in words | ||
| } | ||
|
|
There was a problem hiding this comment.
Lines 77-79 refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension)
| for i in range(len(distances)): | ||
| distance_dict[wg.vertex_at(i)] = distances[i] | ||
| return distance_dict | ||
| return {wg.vertex_at(i): distances[i] for i in range(len(distances))} |
There was a problem hiding this comment.
Function distance_array_to_vertex_dict refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable) - Convert for loop into dictionary comprehension (
dict-comprehension)
| edge_path: WeightedPath = [] | ||
| e: WeightedEdge = path_dict[end] | ||
| edge_path.append(e) | ||
| edge_path: WeightedPath = [e] |
There was a problem hiding this comment.
Function path_dict_to_path refactored with the following changes:
- Simplify sequence comparison (
simplify-len-comparison) - Merge append into list declaration (
merge-list-append) - Move assignment closer to its usage within a block (
move-assign-in-block)
| desc: str = "" | ||
| for i in range(self.vertex_count): | ||
| desc += f"{self.vertex_at(i)} -> {self.neighbors_for_index(i)}\n" | ||
| return desc | ||
| return "".join( | ||
| f"{self.vertex_at(i)} -> {self.neighbors_for_index(i)}\n" | ||
| for i in range(self.vertex_count) | ||
| ) |
There was a problem hiding this comment.
Function Graph.__str__ refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable) - Use str.join() instead of for loop (
use-join)
|
|
||
| def total_weight(wp: WeightedPath) -> float: | ||
| return sum([e.weight for e in wp]) | ||
| return sum(e.weight for e in wp) |
There was a problem hiding this comment.
Function total_weight refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator)
| distance_tuples: List[Tuple[V, float]] = [] | ||
| for edge in self.edges_for_index(index): | ||
| distance_tuples.append((self.vertex_at(edge.v), edge.weight)) | ||
| return distance_tuples | ||
| return [ | ||
| (self.vertex_at(edge.v), edge.weight) | ||
| for edge in self.edges_for_index(index) | ||
| ] |
There was a problem hiding this comment.
Function WeightedGraph.neighbors_for_index_with_weights refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable) - Convert for loop into list comprehension (
list-comprehension)
| desc: str = "" | ||
| for i in range(self.vertex_count): | ||
| desc += f"{self.vertex_at(i)} -> {self.neighbors_for_index_with_weights(i)}\n" | ||
| return desc | ||
| return "".join( | ||
| f"{self.vertex_at(i)} -> {self.neighbors_for_index_with_weights(i)}\n" | ||
| for i in range(self.vertex_count) | ||
| ) |
There was a problem hiding this comment.
Function WeightedGraph.__str__ refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable) - Use str.join() instead of for loop (
use-join)
| else: # otherwise mutate y | ||
| if random() > 0.5: | ||
| self.y += 1 | ||
| else: | ||
| self.y -= 1 | ||
| elif random() > 0.5: | ||
| self.y += 1 | ||
| else: | ||
| self.y -= 1 |
There was a problem hiding this comment.
Function SimpleEquation.mutate refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif)
This removes the following comments ( why? ):
# otherwise mutate y
| segment: List[Tuple[int, int]] = [] | ||
| for t in range(segment_length): | ||
| segment.append((c, r + t)) | ||
| segment: List[Tuple[int, int]] = [(c, r + t) for t in range(segment_length)] | ||
| segments.append(segment) | ||
|
|
||
| # generate the horizontal segments | ||
| for c in range(num_columns - segment_length + 1): | ||
| for r in range(num_rows): | ||
| segment = [] | ||
| for t in range(segment_length): | ||
| segment.append((c + t, r)) | ||
| segment = [(c + t, r) for t in range(segment_length)] | ||
| segments.append(segment) | ||
|
|
||
| # generate the bottom left to top right diagonal segments | ||
| for c in range(num_columns - segment_length + 1): | ||
| for r in range(num_rows - segment_length + 1): | ||
| segment = [] | ||
| for t in range(segment_length): | ||
| segment.append((c + t, r + t)) | ||
| segment = [(c + t, r + t) for t in range(segment_length)] | ||
| segments.append(segment) | ||
|
|
||
| # generate the top left to bottom right diagonal segments | ||
| for c in range(num_columns - segment_length + 1): | ||
| for r in range(segment_length - 1, num_rows): | ||
| segment = [] | ||
| for t in range(segment_length): | ||
| segment.append((c + t, r - t)) | ||
| segment = [(c + t, r - t) for t in range(segment_length)] |
There was a problem hiding this comment.
Function generate_segments refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
| total: float = 0 | ||
| for segment in C4Board.SEGMENTS: | ||
| total += self._evaluate_segment(segment, player) | ||
| return total | ||
| return sum( | ||
| self._evaluate_segment(segment, player) for segment in C4Board.SEGMENTS | ||
| ) |
There was a problem hiding this comment.
Function C4Board.evaluate refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable) - Convert for loop into call to sum() (
sum-comprehension)
| display += f"{self.position[c][r]}" + "|" | ||
| display += f"{self.position[c][r]}|" |
There was a problem hiding this comment.
Function C4Board.__repr__ refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| if self.is_win and self.turn == player: | ||
| return -1 | ||
| elif self.is_win and self.turn != player: | ||
| elif self.is_win: |
There was a problem hiding this comment.
Function TTTBoard.evaluate refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| letter_tuples: List[Tuple[str, ...]] = [] | ||
| for digit in phone_number: | ||
| letter_tuples.append(phone_mapping.get(digit, (digit,))) | ||
| letter_tuples: List[Tuple[str, ...]] = [ | ||
| phone_mapping.get(digit, (digit,)) for digit in phone_number | ||
| ] | ||
|
|
There was a problem hiding this comment.
Function possible_mnemonics refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.29%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
Branch
masterrefactored 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
masterbranch, then run:Help us improve this pull request!