##// END OF EJS Templates
Demo fixes, see changelog
fperez -
Show More
@@ -1,4 +1,6 b''
1 """Module for interactive demos using IPython.
1 """Module for interactive demos using IPython.
2
3 Sorry, but this uses Python 2.3 features, so it won't work in 2.2 environments.
2 """
4 """
3 #*****************************************************************************
5 #*****************************************************************************
4 # Copyright (C) 2005 Fernando Perez. <Fernando.Perez@colorado.edu>
6 # Copyright (C) 2005 Fernando Perez. <Fernando.Perez@colorado.edu>
@@ -9,6 +11,7 b''
9 #*****************************************************************************
11 #*****************************************************************************
10
12
11 import exceptions
13 import exceptions
14 import re
12
15
13 from IPython.PyColorize import Parser
16 from IPython.PyColorize import Parser
14 from IPython.genutils import marquee
17 from IPython.genutils import marquee
@@ -16,10 +19,16 b' from IPython.genutils import marquee'
16 class DemoError(exceptions.Exception): pass
19 class DemoError(exceptions.Exception): pass
17
20
18 class Demo:
21 class Demo:
19 def __init__(self,fname,pause_mark='# pause',auto=False):
22 def __init__(self,fname,mark_pause='# pause',mark_silent='# silent',
23 auto=False):
24 """The marks are turned into regexps which match them as standalone in
25 a line, with all leading/trailing whitespace ignored."""
20
26
21 self.fname = fname
27 self.fname = fname
22 self.pause_mark = pause_mark
28 self.mark_pause = mark_pause
29 self.re_pause = re.compile(r'^\s*%s\s*$' % mark_pause,re.MULTILINE)
30 self.mark_silent = mark_silent
31 self.re_silent = re.compile(r'^\s*%s\s*$' % mark_silent,re.MULTILINE)
23 self.auto = auto
32 self.auto = auto
24
33
25 # get a few things from ipython. While it's a bit ugly design-wise,
34 # get a few things from ipython. While it's a bit ugly design-wise,
@@ -34,7 +43,8 b' class Demo:'
34 fobj = file(fname,'r')
43 fobj = file(fname,'r')
35 self.src = fobj.read()
44 self.src = fobj.read()
36 fobj.close()
45 fobj.close()
37 self.src_blocks = [b.strip() for b in self.src.split(pause_mark) if b]
46 self.src_blocks = [b.strip() for b in self.re_pause.split(self.src) if b]
47 self.silent = [bool(self.re_silent.findall(b)) for b in self.src_blocks]
38 self.nblocks = len(self.src_blocks)
48 self.nblocks = len(self.src_blocks)
39
49
40 # try to colorize blocks
50 # try to colorize blocks
@@ -47,26 +57,32 b' class Demo:'
47 self.reset()
57 self.reset()
48
58
49 def reset(self):
59 def reset(self):
60 """Reset the namespace and seek pointer to restart the demo"""
50 self.user_ns = {}
61 self.user_ns = {}
51 self.finished = False
62 self.finished = False
52 self.block_index = 0
63 self.block_index = 0
53
64
54 def again(self):
65 def again(self):
66 """Repeat the last block"""
55 self.block_index -= 1
67 self.block_index -= 1
56 self()
68 self()
57
69
58
59 def _validate_index(self,index):
70 def _validate_index(self,index):
60 if index<0 or index>=self.nblocks:
71 if index<0 or index>=self.nblocks:
61 raise ValueError('invalid block index %s' % index)
72 raise ValueError('invalid block index %s' % index)
62
73
63 def seek(self,index):
74 def seek(self,index):
75 """Move the current seek pointer to the given block"""
64 self._validate_index(index)
76 self._validate_index(index)
65 self.block_index = index-1
77 self.block_index = index-1
66 self.finished = False
78 self.finished = False
67
79
68 def show(self,index=None):
80 def show_block(self,index=None):
81 """Show a single block on screen"""
69 if index is None:
82 if index is None:
83 if self.finished:
84 print 'Demo finished. Use reset() if you want to rerun it.'
85 return
70 index = self.block_index
86 index = self.block_index
71 else:
87 else:
72 self._validate_index(index)
88 self._validate_index(index)
@@ -74,6 +90,21 b' class Demo:'
74 (self.fname,index,index+1,self.nblocks))
90 (self.fname,index,index+1,self.nblocks))
75 print self.src_blocks_colored[index],
91 print self.src_blocks_colored[index],
76
92
93 def show(self):
94 """Show entire demo on screen, block by block"""
95
96 fname = self.fname
97 nblocks = self.nblocks
98 silent = self.silent
99 for index,block in enumerate(self.src_blocks_colored):
100 if silent[index]:
101 print marquee('<%s> SILENT block # %s (%s/%s)' %
102 (fname,index,index+1,nblocks))
103 else:
104 print marquee('<%s> block # %s (%s/%s)' %
105 (fname,index,index+1,nblocks))
106 print block,
107
77 def __call__(self,index=None):
108 def __call__(self,index=None):
78 """run a block of the demo.
109 """run a block of the demo.
79
110
@@ -92,7 +123,11 b' class Demo:'
92 try:
123 try:
93 next_block = self.src_blocks[index]
124 next_block = self.src_blocks[index]
94 self.block_index += 1
125 self.block_index += 1
95 self.show(index)
126 if self.silent[index]:
127 print marquee('Executing silent block # %s (%s/%s)' %
128 (index,index+1,self.nblocks))
129 else:
130 self.show_block(index)
96 if not self.auto:
131 if not self.auto:
97 print marquee('Press <q> to quit, <Enter> to execute...'),
132 print marquee('Press <q> to quit, <Enter> to execute...'),
98 ans = raw_input().strip()
133 ans = raw_input().strip()
@@ -112,4 +147,3 b' class Demo:'
112 print marquee(' END OF DEMO ')
147 print marquee(' END OF DEMO ')
113 print marquee('Use reset() if you want to rerun it.')
148 print marquee('Use reset() if you want to rerun it.')
114 self.finished = True
149 self.finished = True
115
@@ -1,5 +1,8 b''
1 2005-09-22 Fernando Perez <Fernando.Perez@colorado.edu>
1 2005-09-22 Fernando Perez <Fernando.Perez@colorado.edu>
2
2
3 * IPython/demo.py (Demo.__init__): added support for silent
4 blocks, improved marks as regexps, docstrings written.
5
3 * IPython/genutils.py (marquee): little utility used by the demo
6 * IPython/genutils.py (marquee): little utility used by the demo
4 code, handy in general.
7 code, handy in general.
5
8
General Comments 0
You need to be logged in to leave comments. Login now