Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions src/ds/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

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:


def _base_add_vertex(self, name: str) -> int:
""" Adds new vertex to the graph
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GraphMatrix.__str__ refactored with the following changes:


def get_neighbors_out(self, vertex:int) -> list[int]:
return [vrtx for vrtx, is_neighbour in enumerate(self[vertex]) if is_neighbour!=0]
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GraphAdjList.__str__ refactored with the following changes:


def add_vertex(self, name: str) -> int:
super()._base_add_vertex(name)
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GraphAdjList.reverse_edge refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

self._adj_nodes[v].append(u)
self._adj_nodes[u].remove(v)
else:
Expand Down
12 changes: 6 additions & 6 deletions src/node_layering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Layering.proper_layering refactored with the following changes:

  • Simplify logical expression using De Morgan identities [×2] (de-morgan)

self.dummy_traversing_edges[vtx][child_vtx] = []

for layer in range(
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Layering.__str__ refactored with the following changes:


def __repr__(self) -> str:
return self.__str__()
7 changes: 4 additions & 3 deletions src/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Resolver.get_fps refactored with the following changes:


def __update(self) -> None:
self.state_stack[-1].update(self.dt, self.actions)
Expand Down
19 changes: 11 additions & 8 deletions src/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DependencyScanner.is_valid_project_file refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)


def get_project_files(self, start_dir:str) -> list[Tuple[str, int]]:
project_files = []
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DependencyScanner.scan_dir refactored with the following changes:


return dep_map

6 changes: 2 additions & 4 deletions src/states/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Menu.update refactored with the following changes:

self.resolver.focus()

elif self.buttons[self.CMD_EXIT].is_clicked():
Expand Down
2 changes: 1 addition & 1 deletion src/states/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Scan.__prepare_and_sort_dag refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

self.digraph.add_edge(included_file, source_file)

self.update_status("GRAPH", "Removing cycles and applying toposort...", colors.YELLOW)
Expand Down
2 changes: 1 addition & 1 deletion src/states/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function World.render_nodes refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

continue
elif (vtx_out, node_id) in self.reversed_edges:
continue
Expand Down
4 changes: 1 addition & 3 deletions src/ui/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Button.update refactored with the following changes:

else:
self.state = ClickableState.IDLE

Expand Down