-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,9 +18,7 @@ | |
|
|
||
| @lru_cache(maxsize=None) | ||
| def fib4(n: int) -> int: # same definition as fib2() | ||
| 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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,22 +32,22 @@ def _compress(self, gene: str) -> None: | |
| elif nucleotide == "T": # change last two bits to 11 | ||
| self.bit_string |= 0b11 | ||
| else: | ||
| raise ValueError("Invalid Nucleotide:{}".format(nucleotide)) | ||
| raise ValueError(f"Invalid Nucleotide:{nucleotide}") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def decompress(self) -> str: | ||
| gene: str = "" | ||
| for i in range(0, self.bit_string.bit_length() - 1, 2): # - 1 to exclude sentinel | ||
| bits: int = self.bit_string >> i & 0b11 # get just 2 relevant bits | ||
| 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}") | ||
|
Comment on lines
-41
to
+50
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| return gene[::-1] # [::-1] reverses string by slicing backwards | ||
|
|
||
| def __str__(self) -> str: # string representation for pretty printing | ||
|
|
@@ -57,8 +57,10 @@ def __str__(self) -> str: # string representation for pretty printing | |
| if __name__ == "__main__": | ||
| from sys import getsizeof | ||
| original: str = "TAGGGATTAACCGTTATATATATATAGCCATGGATCGATTATATAGGGATTAACCGTTATATATATATAGCCATGGATCGATTATA" * 100 | ||
| 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())) | ||
| print( | ||
| f"original and decompressed are the same: {original == compressed.decompress()}" | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,10 +38,7 @@ def string_to_gene(s: str) -> Gene: | |
|
|
||
|
|
||
| def linear_contains(gene: Gene, key_codon: Codon) -> bool: | ||
| for codon in gene: | ||
| if codon == key_codon: | ||
| return True | ||
| return False | ||
| return key_codon in gene | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| acg: Codon = (Nucleotide.A, Nucleotide.C, Nucleotide.G) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,10 +22,7 @@ | |
|
|
||
|
|
||
| def linear_contains(iterable: Iterable[T], key: T) -> bool: | ||
| for item in iterable: | ||
| if item == key: | ||
| return True | ||
| return False | ||
| return key in iterable | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| C = TypeVar("C", bound="Comparable") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,10 @@ def __init__(self, rows: int = 10, columns: int = 10, sparseness: float = 0.2, s | |
| self.start: MazeLocation = start | ||
| self.goal: MazeLocation = goal | ||
| # fill the grid with empty cells | ||
| self._grid: List[List[Cell]] = [[Cell.EMPTY for c in range(columns)] for r in range(rows)] | ||
| self._grid: List[List[Cell]] = [ | ||
| [Cell.EMPTY for _ in range(columns)] for _ in range(rows) | ||
| ] | ||
|
|
||
|
Comment on lines
-44
to
+47
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| # populate the grid with blocked cells | ||
| self._randomly_fill(rows, columns, sparseness) | ||
| # fill the start and goal locations in | ||
|
|
@@ -56,10 +59,7 @@ def _randomly_fill(self, rows: int, columns: int, sparseness: float): | |
|
|
||
| # return a nicely formatted version of the maze for printing | ||
| def __str__(self) -> str: | ||
| output: str = "" | ||
| for row in self._grid: | ||
| output += "".join([c.value for c in row]) + "\n" | ||
| return output | ||
| return "".join("".join([c.value for c in row]) + "\n" for row in self._grid) | ||
|
Comment on lines
-59
to
+62
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def goal_test(self, ml: MazeLocation) -> bool: | ||
| return ml == self.goal | ||
|
|
@@ -68,11 +68,11 @@ def successors(self, ml: MazeLocation) -> List[MazeLocation]: | |
| locations: List[MazeLocation] = [] | ||
| if ml.row + 1 < self._rows and self._grid[ml.row + 1][ml.column] != Cell.BLOCKED: | ||
| locations.append(MazeLocation(ml.row + 1, ml.column)) | ||
| if ml.row - 1 >= 0 and self._grid[ml.row - 1][ml.column] != Cell.BLOCKED: | ||
| if ml.row >= 1 and self._grid[ml.row - 1][ml.column] != Cell.BLOCKED: | ||
| locations.append(MazeLocation(ml.row - 1, ml.column)) | ||
| if ml.column + 1 < self._columns and self._grid[ml.row][ml.column + 1] != Cell.BLOCKED: | ||
| locations.append(MazeLocation(ml.row, ml.column + 1)) | ||
| if ml.column - 1 >= 0 and self._grid[ml.row][ml.column - 1] != Cell.BLOCKED: | ||
| if ml.column >= 1 and self._grid[ml.row][ml.column - 1] != Cell.BLOCKED: | ||
|
Comment on lines
-71
to
+75
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| locations.append(MazeLocation(ml.row, ml.column - 1)) | ||
| return locations | ||
|
|
||
|
|
@@ -93,7 +93,7 @@ def euclidean_distance(goal: MazeLocation) -> Callable[[MazeLocation], float]: | |
| def distance(ml: MazeLocation) -> float: | ||
| xdist: int = ml.column - goal.column | ||
| ydist: int = ml.row - goal.row | ||
| return sqrt((xdist * xdist) + (ydist * ydist)) | ||
| return sqrt(xdist**2 + ydist**2) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return distance | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,7 +73,7 @@ def successors(self) -> List[MCState]: | |
|
|
||
|
|
||
| def display_solution(path: List[MCState]): | ||
| if len(path) == 0: # sanity check | ||
| if not path: # sanity check | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return | ||
| old_state: MCState = path[0] | ||
| print(old_state) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,10 +55,10 @@ def add_constraint(self, constraint: Constraint[V, D]) -> None: | |
| # Check if the value assignment is consistent by checking all constraints | ||
| # for the given variable against it | ||
| def consistent(self, variable: V, assignment: Dict[V, D]) -> bool: | ||
| for constraint in self.constraints[variable]: | ||
| if not constraint.satisfied(assignment): | ||
| return False | ||
| return True | ||
| return all( | ||
| constraint.satisfied(assignment) | ||
| for constraint in self.constraints[variable] | ||
| ) | ||
|
Comment on lines
-58
to
+61
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def backtracking_search(self, assignment: Dict[V, D] = {}) -> Optional[Dict[V, D]]: | ||
| # assignment is complete if every variable is assigned (our base case) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,9 +36,10 @@ def satisfied(self, assignment: Dict[str, str]) -> bool: | |
| if __name__ == "__main__": | ||
| variables: List[str] = ["Western Australia", "Northern Territory", "South Australia", | ||
| "Queensland", "New South Wales", "Victoria", "Tasmania"] | ||
| domains: Dict[str, List[str]] = {} | ||
| for variable in variables: | ||
| domains[variable] = ["red", "green", "blue"] | ||
| domains: Dict[str, List[str]] = { | ||
| variable: ["red", "green", "blue"] for variable in variables | ||
| } | ||
|
|
||
|
Comment on lines
-39
to
+42
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| csp: CSP[str, str] = CSP(variables, domains) | ||
| csp.add_constraint(MapColoringConstraint("Western Australia", "Northern Territory")) | ||
| csp.add_constraint(MapColoringConstraint("Western Australia", "South Australia")) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,9 +38,10 @@ def satisfied(self, assignment: Dict[int, int]) -> bool: | |
|
|
||
| if __name__ == "__main__": | ||
| columns: List[int] = [1, 2, 3, 4, 5, 6, 7, 8] | ||
| rows: Dict[int, List[int]] = {} | ||
| for column in columns: | ||
| rows[column] = [1, 2, 3, 4, 5, 6, 7, 8] | ||
| rows: Dict[int, List[int]] = { | ||
| column: [1, 2, 3, 4, 5, 6, 7, 8] for column in columns | ||
| } | ||
|
|
||
|
Comment on lines
-41
to
+44
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| csp: CSP[int, int] = CSP(columns, rows) | ||
| csp.add_constraint(QueensConstraint(columns)) | ||
| solution: Optional[Dict[int, int]] = csp.backtracking_search() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,9 +46,10 @@ def satisfied(self, assignment: Dict[str, int]) -> bool: | |
|
|
||
| if __name__ == "__main__": | ||
| letters: List[str] = ["S", "E", "N", "D", "M", "O", "R", "Y"] | ||
| 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 | ||
| } | ||
|
|
||
|
Comment on lines
-49
to
+52
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| possible_digits["M"] = [1] # so we don't get answers starting with a 0 | ||
| csp: CSP[str, int] = CSP(letters, possible_digits) | ||
| csp.add_constraint(SendMoreMoneyConstraint(letters)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ class GridLocation(NamedTuple): | |
|
|
||
| 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)] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def display_grid(grid: Grid) -> None: | ||
|
|
@@ -74,9 +74,10 @@ def satisfied(self, assignment: Dict[str, List[GridLocation]]) -> bool: | |
| if __name__ == "__main__": | ||
| grid: Grid = generate_grid(9, 9) | ||
| words: List[str] = ["MATTHEW", "JOE", "MARY", "SARAH", "SALLY"] | ||
| 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 | ||
| } | ||
|
|
||
|
Comment on lines
-77
to
+80
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| csp: CSP[str, List[GridLocation]] = CSP(words, locations) | ||
| csp.add_constraint(WordSearchConstraint(words)) | ||
| solution: Optional[Dict[str, List[GridLocation]]] = csp.backtracking_search() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,20 +66,16 @@ def dijkstra(wg: WeightedGraph[V], root: V) -> Tuple[List[Optional[float]], Dict | |
|
|
||
| # Helper function to get easier access to dijkstra results | ||
| def distance_array_to_vertex_dict(wg: WeightedGraph[V], distances: List[Optional[float]]) -> Dict[V, Optional[float]]: | ||
| distance_dict: Dict[V, Optional[float]] = {} | ||
| 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))} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| # Takes a dictionary of edges to reach each node and returns a list of | ||
| # edges that goes from `start` to `end` | ||
| def path_dict_to_path(start: int, end: int, path_dict: Dict[int, WeightedEdge]) -> WeightedPath: | ||
| if len(path_dict) == 0: | ||
| if not path_dict: | ||
| return [] | ||
| edge_path: WeightedPath = [] | ||
| e: WeightedEdge = path_dict[end] | ||
| edge_path.append(e) | ||
| edge_path: WeightedPath = [e] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| while e.u != start: | ||
| e = path_dict[e.u] | ||
| edge_path.append(e) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,10 +82,10 @@ def edges_for_vertex(self, vertex: V) -> List[Edge]: | |
|
|
||
| # Make it easy to pretty-print a Graph | ||
| def __str__(self) -> str: | ||
| 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) | ||
| ) | ||
|
Comment on lines
-85
to
+88
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ | |
|
|
||
|
|
||
| def total_weight(wp: WeightedPath) -> float: | ||
| return sum([e.weight for e in wp]) | ||
| return sum(e.weight for e in wp) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def mst(wg: WeightedGraph[V], start: int = 0) -> Optional[WeightedPath]: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,16 +35,16 @@ def add_edge_by_vertices(self, first: V, second: V, weight: float) -> None: | |
| self.add_edge_by_indices(u, v, weight) | ||
|
|
||
| def neighbors_for_index_with_weights(self, index: int) -> List[Tuple[V, float]]: | ||
| 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) | ||
| ] | ||
|
Comment on lines
-38
to
+41
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def __str__(self) -> str: | ||
| 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) | ||
| ) | ||
|
Comment on lines
-44
to
+47
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,11 +46,10 @@ def mutate(self) -> None: | |
| self.x += 1 | ||
| else: | ||
| self.x -= 1 | ||
| 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 | ||
|
Comment on lines
-49
to
+52
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| def __str__(self) -> str: | ||
| return f"X: {self.x} Y: {self.y} Fitness: {self.fitness()}" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,9 +33,11 @@ def distance(self, other: DataPoint) -> float: | |
| return sqrt(sum(differences)) | ||
|
|
||
| def __eq__(self, other: object) -> bool: | ||
| if not isinstance(other, DataPoint): | ||
| return NotImplemented | ||
| return self.dimensions == other.dimensions | ||
| return ( | ||
| self.dimensions == other.dimensions | ||
| if isinstance(other, DataPoint) | ||
| else NotImplemented | ||
| ) | ||
|
Comment on lines
-36
to
+40
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def __repr__(self) -> str: | ||
| return self._originals.__repr__() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,11 +25,13 @@ def __init__(self, previous_layer: Optional[Layer], num_neurons: int, learning_r | |
| self.previous_layer: Optional[Layer] = previous_layer | ||
| self.neurons: List[Neuron] = [] | ||
| # the following could all be one large list comprehension, but gets a bit long that way | ||
| for i in range(num_neurons): | ||
| if previous_layer is None: | ||
| random_weights: List[float] = [] | ||
| else: | ||
| random_weights = [random() for _ in range(len(previous_layer.neurons))] | ||
| for _ in range(num_neurons): | ||
| random_weights = ( | ||
| [] | ||
| if previous_layer is None | ||
| else [random() for _ in range(len(previous_layer.neurons))] | ||
| ) | ||
|
|
||
|
Comment on lines
-28
to
+34
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| neuron: Neuron = Neuron(random_weights, learning_rate, activation_function, derivative_activation_function) | ||
| self.neurons.append(neuron) | ||
| self.output_cache: List[float] = [0.0 for _ in range(num_neurons)] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,6 @@ def normalize_by_feature_scaling(dataset: List[List[float]]) -> None: | |
| column: List[float] = [row[col_num] for row in dataset] | ||
| maximum = max(column) | ||
| minimum = min(column) | ||
| for row_num in range(len(dataset)): | ||
| dataset[row_num][col_num] = (dataset[row_num][col_num] - minimum) / (maximum - minimum) | ||
| for item in dataset: | ||
| item[col_num] = (item[col_num] - minimum) / (maximum - minimum) | ||
|
Comment on lines
-42
to
+43
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
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
fib2refactored with the following changes:reintroduce-else)assign-if-exp)This removes the following comments ( why? ):