Skip to content

Commit

Permalink
Extend lists for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
geographika committed Apr 23, 2024
1 parent aeb0ee7 commit 5021b3a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
4 changes: 2 additions & 2 deletions wayfarer/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def get_edges_from_nodes(
**edge._asdict()
) # unpack namedtuple to **kwargs

edge_list += node_edges
edge_list.extend(node_edges)

if return_unique:
return list({v.key: v for v in edge_list}.values())
Expand Down Expand Up @@ -495,7 +495,7 @@ def get_nodes_from_edges(edges):
if edge_atts[WITH_DIRECTION_FIELD] is False:
nodes = reversed(nodes)

all_nodes += nodes
all_nodes.extend(nodes)

# return a unique ordered list
return list(OrderedDict.fromkeys(all_nodes))
Expand Down
8 changes: 3 additions & 5 deletions wayfarer/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def solve_shortest_path_from_edges(

if edge.start_node == edge.end_node:
# self-loop
edges += functions.get_edges_from_nodes(net, end_nodes)
edges.extend(functions.get_edges_from_nodes(net, end_nodes))
else:
try:
nodes = solve_shortest_path_from_nodes(net, node_list)
Expand All @@ -118,7 +118,7 @@ def solve_shortest_path_from_edges(
log.warning(ex)
raise

edges += functions.get_edges_from_nodes(net, nodes)
edges.extend(functions.get_edges_from_nodes(net, nodes))

# reset original length - note not in original implementation
# ensure this is only reset after get_edges_from_nodes has been called
Expand All @@ -130,7 +130,7 @@ def solve_shortest_path_from_edges(
net, [edge.start_node, edge.end_node]
)
if loop_edges:
edges += loop_edges
edges.extend(loop_edges)

previous_edge_nodes = end_nodes

Expand Down Expand Up @@ -196,13 +196,11 @@ def solve_matching_path(
all_shortest_paths.append([start_node])

for path_nodes in all_shortest_paths:

all_paths = functions.get_all_paths_from_nodes(
net, path_nodes, with_direction_flag=True
)

for path_edges in all_paths:

if include_key and include_key not in [e.key for e in path_edges]:
continue
path_length = functions.get_path_length(path_edges)
Expand Down
2 changes: 1 addition & 1 deletion wayfarer/splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def get_split_nodes(split_edges: list[Edge]) -> list[int | str]:
split_nodes = []

for split_edge in split_edges:
nodes += [split_edge.start_node, split_edge.end_node]
nodes.extend([split_edge.start_node, split_edge.end_node])

for n in set(nodes):
if SPLIT_KEY_SEPARATOR in str(n):
Expand Down

0 comments on commit 5021b3a

Please sign in to comment.