From 224785e1f2c86fe3250a41876ad96e138bc3dbf4 Mon Sep 17 00:00:00 2001 From: Dimitri Yatsenko Date: Mon, 16 Sep 2024 00:03:37 +0000 Subject: [PATCH] optimize topographical sort --- datajoint/dependencies.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/datajoint/dependencies.py b/datajoint/dependencies.py index 4f78ad4f..aefb1bd2 100644 --- a/datajoint/dependencies.py +++ b/datajoint/dependencies.py @@ -51,20 +51,15 @@ def topo_sort(graph): placed = set() while pos > 1: part = sorted_nodes[pos] - if not (master := extract_master) or part in placed: + if (master := extract_master(part)) not in graph or part in placed: pos -= 1 else: placed.add(part) - try: - j = sorted_nodes.index(master) - except ValueError: - # master not found - pass - else: - if pos > j + 1: - # move the part to its master - del sorted_nodes[pos] - sorted_nodes.insert(j + 1, part) + j = sorted_nodes.index(master) + if pos > j + 1: + # move the part to its master + del sorted_nodes[pos] + sorted_nodes.insert(j + 1, part) return sorted_nodes