@@ -31,36 +31,42 @@ def __init__( self, step = None ):
3131 if step :
3232 self .V = step .V .copy ()
3333 self .E = step .E .copy ()
34- self .L = step .L .copy ()
34+ self .lV = step .lV .copy ()
35+ self .lE = step .lE .copy ()
3536 else :
3637 self .V = set ()
3738 self .E = set ()
38- self .L = dict ()
39+ self .lV = dict ()
40+ self .lE = dict ()
3941 self .hV = dict ()
4042 self .hE = dict ()
4143
4244 def node_format ( self , v ):
4345 fmt = []
44- try :
45- fmt .append ( 'label="{}"' .format ( self .L [ v ] ) )
46- except KeyError :
47- pass
46+ if v in self .lV :
47+ fmt .append ( 'label="{}"' .format ( quote ( str ( self .lV [ v ] ) ) ) )
4848 if v in self .hV :
4949 fmt .append ( 'color={}' .format ( self .hV [ v ] ) )
5050 elif v not in self .V :
5151 fmt .append ( 'style=invis' )
52- if fmt : return '[{}]' .format ( ',' .join ( fmt ) )
52+ if fmt :
53+ return '[{}]' .format ( ', ' .join ( fmt ) )
5354 return ''
5455
5556 def edge_format ( self , e ):
57+ fmt = []
58+ if e in self .lE :
59+ fmt .append ('label="{}"' .format ( quote ( str ( self .lE [ e ] ) ) ) )
5660 if e in self .hE :
57- return '[color={}]' .format ( self .hE [ e ] )
58- elif e in self .E :
59- return ''
60- return '[style=invis]'
61+ fmt .append ('color={}' .format ( self .hE [ e ] ) )
62+ elif e not in self .E :
63+ fmt .append ('style=invis' )
64+ if fmt :
65+ return '[{}]' .format ( ', ' .join ( fmt ) )
66+ return ''
6167
6268 def __repr__ ( self ):
63- return '{{ V = {}, E = {}, hV = {}, hE = {}, L = {} }} ' .format ( self .V , self .E , self .hV , self .hE , self .L )
69+ return '{{ V = {}, E = {}, hV = {}, hE = {}, L = {}, lE = {} }} ' .format ( self .V , self .E , self .hV , self .hE , self .lV , self . lE )
6470
6571class Animation ( object ):
6672
@@ -91,6 +97,12 @@ def add_edge( self, u, v ):
9197 def highlight_edge ( self , u , v , color = 'red' ):
9298 self ._actions .append ( action .HighlightEdge ( u , v , color = color ) )
9399
100+ def label_edge ( self , u , v , label ):
101+ self ._actions .append ( action .LabelEdge ( u , v , label ) )
102+
103+ def unlabel_edge ( self , u , v ):
104+ self ._actions .append ( action .UnlabelEdge ( u , v ) )
105+
94106 def remove_edge ( self , u , v ):
95107 self ._actions .append ( action .RemoveEdge ( u , v ) )
96108
@@ -104,6 +116,8 @@ def parse( self, lines ):
104116 'rn' : self .remove_node ,
105117 'ae' : self .add_edge ,
106118 'he' : self .highlight_edge ,
119+ 'le' : self .label_edge ,
120+ 'ue' : self .unlabel_edge ,
107121 're' : self .remove_edge ,
108122 }
109123 for line in lines :
@@ -137,4 +151,5 @@ def graphs( self ):
137151 for e in E : graph .append ( '"{}" -> "{}" {};' .format ( quote ( str ( e [ 0 ] ) ), quote ( str ( e [ 1 ] ) ), s .edge_format ( e ) ) )
138152 graph .append ( '}' )
139153 graphs .append ( '\n ' .join ( graph ) )
154+
140155 return graphs
0 commit comments