From de6e9f51c7d465bcb39e3acd212b9f9ba3899a2c Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Tue, 23 Jan 2024 13:54:03 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- src/ds/graph.py | 59 +++++++++++++++++++++++++++++--------------- src/node_layering.py | 12 ++++----- src/resolver.py | 7 +++--- src/scanner.py | 19 ++++++++------ src/states/menu.py | 6 ++--- src/states/scan.py | 2 +- src/states/world.py | 2 +- src/ui/button.py | 4 +-- 8 files changed, 65 insertions(+), 46 deletions(-) diff --git a/src/ds/graph.py b/src/ds/graph.py index 9b80bed..3c27b32 100644 --- a/src/ds/graph.py +++ b/src/ds/graph.py @@ -41,9 +41,7 @@ def safe_add_vertex(self, name: str) -> int: Returns index of the new (or existing) vertex. """ v_id = self.get_vertex_id(name) - if v_id == -1: - return self.add_vertex(name) - return v_id + return self.add_vertex(name) if v_id == -1 else v_id def _base_add_vertex(self, name: str) -> int: """ Adds new vertex to the graph @@ -74,14 +72,26 @@ def get_matrix_str(self) -> str: return str(self._adj_nodes) def __str__(self) -> str: - return ("{\n" + - "\n".join( - [f'"{name}" | {node_id}' + ": [" + ",".join( - [str(vrtx) for vrtx in self.get_neighbors_out(node_id)] - ) + "]" for name,node_id in self._name2node.items()] - ) - + - "\n}") + return ( + "{\n" + + "\n".join( + [ + ( + ( + f'"{name}" | {node_id}: [' + + ",".join( + [ + str(vrtx) + for vrtx in self.get_neighbors_out(node_id) + ] + ) + ) + + "]" + ) + for name, node_id in self._name2node.items() + ] + ) + ) + "\n}" def get_neighbors_out(self, vertex:int) -> list[int]: return [vrtx for vrtx, is_neighbour in enumerate(self[vertex]) if is_neighbour!=0] @@ -115,14 +125,23 @@ def __init__(self) -> None: self._adj_nodes:list[list[int]] = [] def __str__(self) -> str: - return ("{\n" + - "\n".join( - [f'"{name}"' + ": [" + ",".join( - [str(edge) for edge in self._adj_nodes[node_id]] - ) + "]" for name,node_id in self._name2node.items()] - ) - + - "\n}") + return ( + "{\n" + + "\n".join( + [ + ( + ( + f'"{name}": [' + + ",".join( + [str(edge) for edge in self._adj_nodes[node_id]] + ) + ) + + "]" + ) + for name, node_id in self._name2node.items() + ] + ) + ) + "\n}" def add_vertex(self, name: str) -> int: super()._base_add_vertex(name) @@ -133,7 +152,7 @@ def _final_add_edge(self, u:int, v:int): self._adj_nodes[u].append(v) def reverse_edge(self, u:int, v:int) -> None: - if not u in self._adj_nodes[v]: + if u not in self._adj_nodes[v]: self._adj_nodes[v].append(u) self._adj_nodes[u].remove(v) else: diff --git a/src/node_layering.py b/src/node_layering.py index ad36c40..d645e1f 100644 --- a/src/node_layering.py +++ b/src/node_layering.py @@ -57,10 +57,10 @@ def proper_layering(self): for child_vtx in self.graph.get_neighbors_out(vtx): target_level = self.__node2layer[child_vtx] if abs(target_level - current_layer.level) > 1: - if not vtx in self.dummy_traversing_edges: + if vtx not in self.dummy_traversing_edges: self.dummy_traversing_edges[vtx] = {} - if not child_vtx in self.dummy_traversing_edges[vtx]: + if child_vtx not in self.dummy_traversing_edges[vtx]: self.dummy_traversing_edges[vtx][child_vtx] = [] for layer in range( @@ -72,10 +72,10 @@ def proper_layering(self): self.__layers[layer-1].nodes.append(-1) def __str__(self): - resp = "" - for layer in self.__layers: - resp += str(layer.level) + " => " + str(layer.nodes) + "\n" - return resp + return "".join( + f"{str(layer.level)} => {str(layer.nodes)}" + "\n" + for layer in self.__layers + ) def __repr__(self) -> str: return self.__str__() \ No newline at end of file diff --git a/src/resolver.py b/src/resolver.py index da699b3..d48e242 100644 --- a/src/resolver.py +++ b/src/resolver.py @@ -126,13 +126,14 @@ def __update_dt(self) -> None: self.get_fps() def get_fps(self): - fps = 0 - if self.dt: fps = 1/self.dt + fps = 1/self.dt if self.dt else 0 if len(self.fps_list) == 50: self.fps_list.pop(0) self.fps_list.append(fps) avg_fps = sum(self.fps_list) / len(self.fps_list) - pygame.display.set_caption('Dependency resolver - FPS: ' + str(round(avg_fps,2))) + pygame.display.set_caption( + f'Dependency resolver - FPS: {str(round(avg_fps, 2))}' + ) def __update(self) -> None: self.state_stack[-1].update(self.dt, self.actions) diff --git a/src/scanner.py b/src/scanner.py index 71425a3..279a45c 100644 --- a/src/scanner.py +++ b/src/scanner.py @@ -22,11 +22,14 @@ def is_valid_project_file(self, filename:str) -> int: """ Returns index that represents a file extension from SUPPORTED_FILES. """ - for i, ext in enumerate(self.SUPPORTED_FILES): - if filename.endswith(ext): - return i - - return -1 + return next( + ( + i + for i, ext in enumerate(self.SUPPORTED_FILES) + if filename.endswith(ext) + ), + -1, + ) def get_project_files(self, start_dir:str) -> list[Tuple[str, int]]: project_files = [] @@ -70,15 +73,15 @@ def scan_dir(self, path_dir:str) -> dict[str, list[str]]: for i, (file, file_type) in enumerate(project_files): file = DependencyScanner.normalize_path(file) - + # Report progress to the caller if self.__progress_cb: self.__progress_cb(i, len(project_files), file.replace(path_dir, "")) includes = self.get_includes(file) dep_map[file] = includes - - self.glob_log.info(f"Scan complete!") + + self.glob_log.info("Scan complete!") return dep_map \ No newline at end of file diff --git a/src/states/menu.py b/src/states/menu.py index 4ad038e..01d8744 100644 --- a/src/states/menu.py +++ b/src/states/menu.py @@ -50,14 +50,12 @@ def update(self, delta_time, actions): if self.buttons[self.CMD_CHOOSE].is_clicked(): - target_project_dir = self.resolver.find_directory() - - if target_project_dir: + if target_project_dir := self.resolver.find_directory(): new_state = Scan(self.resolver, target_project_dir) new_state.enter_state() else: self.resolver.show_error("Project directory", "You haven't selected a project directory.") - + self.resolver.focus() elif self.buttons[self.CMD_EXIT].is_clicked(): diff --git a/src/states/scan.py b/src/states/scan.py index b3e63e1..4eb45ae 100644 --- a/src/states/scan.py +++ b/src/states/scan.py @@ -71,7 +71,7 @@ def __prepare_and_sort_dag(self) -> None: self.digraph.safe_add_vertex(included_file) # prevent files with improper includes or "self includes" - if not included_file == source_file: + if included_file != source_file: self.digraph.add_edge(included_file, source_file) self.update_status("GRAPH", "Removing cycles and applying toposort...", colors.YELLOW) diff --git a/src/states/world.py b/src/states/world.py index 3b2cccf..4cc9d4b 100644 --- a/src/states/world.py +++ b/src/states/world.py @@ -251,7 +251,7 @@ def render_nodes(self, display): for vtx_out in self.digraph.get_neighbors_out(node_id): if (node_id, vtx_out) in self.reversed_edges: - if not node_id in self.digraph.get_neighbors_out(vtx_out): + if node_id not in self.digraph.get_neighbors_out(vtx_out): continue elif (vtx_out, node_id) in self.reversed_edges: continue diff --git a/src/ui/button.py b/src/ui/button.py index 0175d5e..b2fa877 100644 --- a/src/ui/button.py +++ b/src/ui/button.py @@ -105,9 +105,7 @@ def set_text(self, text:str): def update(self, dt, mouse_x, mouse_y, is_clicked: bool, offset_x=0, offset_y=0): if self.__rect.collidepoint(mouse_x, mouse_y): - self.state = ClickableState.HOVERED - if is_clicked: - self.state = ClickableState.CLICKED + self.state = ClickableState.CLICKED if is_clicked else ClickableState.HOVERED else: self.state = ClickableState.IDLE