-
Notifications
You must be signed in to change notification settings - Fork 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
Sourcery Starbot ⭐ refactored haolly/dependency-resolver #1
base: main
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 |
---|---|---|
|
@@ -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}" | ||
Comment on lines
-77
to
+94
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 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}" | ||
Comment on lines
-118
to
+144
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 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]: | ||
Comment on lines
-136
to
+155
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
|
||
self._adj_nodes[v].append(u) | ||
self._adj_nodes[u].remove(v) | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]: | ||
Comment on lines
-60
to
+63
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
|
||
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 | ||
) | ||
Comment on lines
-75
to
+78
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.__str__() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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))}' | ||
) | ||
Comment on lines
-129
to
+136
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 __update(self) -> None: | ||
self.state_stack[-1].update(self.dt, self.actions) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
) | ||
Comment on lines
-25
to
+32
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 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!") | ||
Comment on lines
-73
to
+84
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 dep_map | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.") | ||
|
||
Comment on lines
-53
to
+58
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
|
||
self.resolver.focus() | ||
|
||
elif self.buttons[self.CMD_EXIT].is_clicked(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
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
|
||
self.digraph.add_edge(included_file, source_file) | ||
|
||
self.update_status("GRAPH", "Removing cycles and applying toposort...", colors.YELLOW) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
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
|
||
continue | ||
elif (vtx_out, node_id) in self.reversed_edges: | ||
continue | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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
|
||
else: | ||
self.state = ClickableState.IDLE | ||
|
||
|
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
Graph.safe_add_vertex
refactored with the following changes:reintroduce-else
)assign-if-exp
)