From 8b773790298cb50226704e0900a36ee71c99c0ec Mon Sep 17 00:00:00 2001
From: Lionel GUEZ <guez@lmd.ens.fr>
Date: Fri, 8 Sep 2023 18:06:38 +0200
Subject: [PATCH] Use `if` instead of `try`

Preparing for more complex script.
---
 Trajectories/trajectories.py | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/Trajectories/trajectories.py b/Trajectories/trajectories.py
index d77b833c..f9a7aa47 100755
--- a/Trajectories/trajectories.py
+++ b/Trajectories/trajectories.py
@@ -94,21 +94,20 @@ traj_segm = [] # an element of traj_segm is a trajectory, represented
 expanded_traj = [] # an element of expanded_traj is a trajectory,
                    # represented by a list of instantaneous eddies
 
-try:
+if "cost_function" in g.edge_properties:
     all_cost = g.get_edges([g.edge_properties.cost_function])
     # numpy array with dtype float64
-except AttributeError:
-    # This happens if there are only isolated segments.
-    print("No cost values in the input file.")
-    all_cost = np.empty((0, 3))
 
-# Array of indices that sort on cost:
-ind_cost = np.argsort(all_cost[:, 2])
+    # Array of indices that sort on cost:
+    ind_cost = np.argsort(all_cost[:, 2])
 
-# Loop over edges in ascending order of cost:
-for i in ind_cost:
-    ind_traj = assign_traj_edge(all_cost[i, 0], all_cost[i, 1], traj_prop,
-                                ind_traj, g, traj_segm, expanded_traj)
+    # Loop over edges in ascending order of cost:
+    for i in ind_cost:
+        ind_traj = assign_traj_edge(all_cost[i, 0], all_cost[i, 1], traj_prop,
+                                    ind_traj, g, traj_segm, expanded_traj)
+else:
+    # This happens if there are only isolated segments.
+    print("No cost values in the input file.")
 
 # Create trajectories for isolated segments:
 for v in g.vertices():
-- 
GitLab