@@ -52,29 +52,24 @@ def remove_vertex(self, name):
52
52
node_obj .adjacent .remove (name )
53
53
54
54
def add_edge (self , source , target , cost = None ):
55
+ error_msg = "Vertex %s is not present in the graph. \
56
+ Call Graph.add_vertex() to add a new \
57
+ vertex. Graph.add_edge is only responsible \
58
+ for adding edges and it will not add new \
59
+ vertices on its own. This is done to maintain \
60
+ clear separation between the functionality of \
61
+ the two methods."
55
62
if not hasattr (self , source ):
56
- raise ValueError ("Vertex %s is not present in the graph."
57
- " Call Graph.add_vertex() to add a new"
58
- " vertex. Graph.add_edge is only responsible"
59
- " for adding edges and it will not add new"
60
- " vertices on its own. This is done to maintain"
61
- " clear separation between the functionality of"
62
- " the two methods." % (source ))
63
- elif not hasattr (self , target ):
64
- raise ValueError ("Vertex %s is not present in the graph."
65
- " Call Graph.add_vertex() to add a new"
66
- " vertex. Graph.add_edge is only responsible"
67
- " for adding edges and it will not add new"
68
- " vertices on its own. This is done to maintain"
69
- " clear separation between the functionality of"
70
- " the two methods." % (target ))
71
- else :
72
- source , target = self .__getattribute__ (source ), \
73
- self .__getattribute__ (target )
74
- source .add_adjacent_node (target .name )
75
- if cost is not None :
76
- self .edge_weights [source .name + "_" + target .name ] = \
77
- GraphEdge (source , target , cost )
63
+ raise ValueError (error_msg % (source ))
64
+ if not hasattr (self , target ):
65
+ raise ValueError (error_msg % (target ))
66
+
67
+ source , target = self .__getattribute__ (source ), \
68
+ self .__getattribute__ (target )
69
+ source .add_adjacent_node (target .name )
70
+ if cost is not None :
71
+ self .edge_weights [source .name + "_" + target .name ] = \
72
+ GraphEdge (source , target , cost )
78
73
79
74
def get_edge (self , source , target ):
80
75
return self .edge_weights .get (
0 commit comments