Skip to content

Commit

Permalink
Merge branch (revised pr) 'Joilnen-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mapio committed Apr 3, 2019
2 parents 4653d02 + a95d273 commit 6c839fe
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/heapsort.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6f45a62e8def48d38d9720bde9ab736b",
"model_id": "0f5267abc9e3478587dd7b61d9cb0c1c",
"version_major": 2,
"version_minor": 0
},
Expand Down
4 changes: 3 additions & 1 deletion examples/simple.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
ae 1 2
ae 1 "a \"node\""
ae 2 "a \"node\""
le 1 2 "an \"edge\""
ns
hn 1
ns
hn 2
ln 2 "foo bar"
ln 2 "foo \"bar\""
ue 1 2
ns
hn "a \"node\""
# this is a comment
Expand Down
31 changes: 28 additions & 3 deletions gvanim/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def __init__( self, v, label ):
self.label = label
def __call__( self, steps ):
steps[ -1 ].V.add( self.v )
steps[ -1 ].L[ self.v ] = self.label
steps[ -1 ].lV[ self.v ] = self.label

class UnlabelNode( object ):
def __init__( self, v ):
self.v = v
def __call__( self, steps ):
steps[ -1 ].V.add( self.v )
try:
del steps[ -1 ].L[ self.v ]
del steps[ -1 ].lV[ self.v ]
except KeyError:
pass

Expand All @@ -64,7 +64,7 @@ def __call__( self, steps ):
except KeyError:
pass
try:
del steps[ -1 ].L[ self.v ]
del steps[ -1 ].lV[ self.v ]
except KeyError:
pass
dE = set( e for e in steps[ -1 ].E if self.v in e )
Expand Down Expand Up @@ -93,6 +93,30 @@ def __call__( self, steps ):
steps[ -1 ].E.add( ( self.u, self.v ) )
steps[ -1 ].hE[ ( self.u, self.v ) ] = self.color

class LabelEdge( object ):
def __init__( self, u, v, label ):
self.u = u
self.v = v
self.label_edge = label
def __call__( self, steps ):
steps[ -1 ].V.add( self.u )
steps[ -1 ].V.add( self.v )
steps[ -1 ].E.add( ( self.u, self.v ) )
steps[ -1 ].lE[ ( self.u, self.v ) ] = self.label_edge

class UnlabelEdge( object ):
def __init__( self, u, v ):
self.u = u
self.v = v
def __call__( self, steps ):
steps[ -1 ].V.add( self.u )
steps[ -1 ].V.add( self.v )
steps[ -1 ].E.add( ( self.u, self.v ) )
try:
del steps[ -1 ].lE[ ( self.u, self.v ) ]
except KeyError:
pass

class RemoveEdge( object ):
def __init__( self, u, v ):
self.u = u
Expand All @@ -101,5 +125,6 @@ def __call__( self, steps ):
steps[ -1 ].E.discard( ( self.u, self.v ) )
try:
del steps[ -1 ].hE[ ( self.u, self.v ) ]
del steps[ -1 ].lE[ ( self.u, self.v ) ]
except KeyError:
pass
39 changes: 27 additions & 12 deletions gvanim/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,42 @@ def __init__( self, step = None ):
if step:
self.V = step.V.copy()
self.E = step.E.copy()
self.L = step.L.copy()
self.lV = step.lV.copy()
self.lE = step.lE.copy()
else:
self.V = set()
self.E = set()
self.L = dict()
self.lV = dict()
self.lE = dict()
self.hV = dict()
self.hE = dict()

def node_format( self, v ):
fmt = []
try:
fmt.append( 'label="{}"'.format( self.L[ v ] ) )
except KeyError:
pass
if v in self.lV:
fmt.append( 'label="{}"'.format( quote( str( self.lV[ v ] ) ) ) )
if v in self.hV:
fmt.append( 'color={}'.format( self.hV[ v ] ) )
elif v not in self.V:
fmt.append( 'style=invis' )
if fmt: return '[{}]'.format( ','.join( fmt ) )
if fmt:
return '[{}]'.format( ', '.join( fmt ) )
return ''

def edge_format( self, e ):
fmt = []
if e in self.lE:
fmt.append('label="{}"'.format( quote( str( self.lE[ e ] ) ) ) )
if e in self.hE:
return '[color={}]'.format( self.hE[ e ] )
elif e in self.E:
return ''
return '[style=invis]'
fmt.append('color={}'.format( self.hE[ e ] ) )
elif e not in self.E:
fmt.append('style=invis')
if fmt:
return '[{}]'.format( ', '.join( fmt ) )
return ''

def __repr__( self ):
return '{{ V = {}, E = {}, hV = {}, hE = {}, L = {} }}'.format( self.V, self.E, self.hV, self.hE, self.L )
return '{{ V = {}, E = {}, hV = {}, hE = {}, L = {}, lE = {} }}'.format( self.V, self.E, self.hV, self.hE, self.lV, self.lE )

class Animation( object ):

Expand Down Expand Up @@ -91,6 +97,12 @@ def add_edge( self, u, v ):
def highlight_edge( self, u, v, color = 'red' ):
self._actions.append( action.HighlightEdge( u, v, color = color ) )

def label_edge( self, u, v, label ):
self._actions.append( action.LabelEdge( u, v, label ) )

def unlabel_edge( self, u, v ):
self._actions.append( action.UnlabelEdge( u, v ) )

def remove_edge( self, u, v ):
self._actions.append( action.RemoveEdge( u, v ) )

Expand All @@ -104,6 +116,8 @@ def parse( self, lines ):
'rn' : self.remove_node,
'ae' : self.add_edge,
'he' : self.highlight_edge,
'le' : self.label_edge,
'ue' : self.unlabel_edge,
're' : self.remove_edge,
}
for line in lines:
Expand Down Expand Up @@ -137,4 +151,5 @@ def graphs( self ):
for e in E: graph.append( '"{}" -> "{}" {};'.format( quote( str( e[ 0 ] ) ), quote( str( e[ 1 ] ) ), s.edge_format( e ) ) )
graph.append( '}' )
graphs.append( '\n'.join( graph ) )

return graphs
4 changes: 3 additions & 1 deletion gvanim/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def render( graphs, basename, fmt = 'png', size = 320 ):
_map = map
return _map( _render, [ ( '{}_{:03}.{}'.format( basename, n, fmt ), fmt, size, graph ) for n, graph in enumerate( graphs ) ] )

def gif( files, basename, delay = 100 ):
def gif( files, basename, delay = 100, size = 320 ):
for file in files:
call([ 'mogrify', '-gravity', 'center', '-background', 'white', '-extent', str(size), file ])
cmd = [ 'convert' ]
for file in files:
cmd.extend( ( '-delay', str( delay ), file ) )
Expand Down
Binary file removed tests/action.pyc
Binary file not shown.

0 comments on commit 6c839fe

Please sign in to comment.