##// END OF EJS Templates
Demo fixes, see changelog
fperez -
Show More
@@ -1,4 +1,6 b''
1 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 6 # Copyright (C) 2005 Fernando Perez. <Fernando.Perez@colorado.edu>
@@ -9,6 +11,7 b''
9 11 #*****************************************************************************
10 12
11 13 import exceptions
14 import re
12 15
13 16 from IPython.PyColorize import Parser
14 17 from IPython.genutils import marquee
@@ -16,10 +19,16 b' from IPython.genutils import marquee'
16 19 class DemoError(exceptions.Exception): pass
17 20
18 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 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 32 self.auto = auto
24 33
25 34 # get a few things from ipython. While it's a bit ugly design-wise,
@@ -34,7 +43,8 b' class Demo:'
34 43 fobj = file(fname,'r')
35 44 self.src = fobj.read()
36 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 48 self.nblocks = len(self.src_blocks)
39 49
40 50 # try to colorize blocks
@@ -47,33 +57,54 b' class Demo:'
47 57 self.reset()
48 58
49 59 def reset(self):
60 """Reset the namespace and seek pointer to restart the demo"""
50 61 self.user_ns = {}
51 62 self.finished = False
52 63 self.block_index = 0
53 64
54 65 def again(self):
66 """Repeat the last block"""
55 67 self.block_index -= 1
56 68 self()
57 69
58
59 70 def _validate_index(self,index):
60 71 if index<0 or index>=self.nblocks:
61 72 raise ValueError('invalid block index %s' % index)
62 73
63 74 def seek(self,index):
75 """Move the current seek pointer to the given block"""
64 76 self._validate_index(index)
65 77 self.block_index = index-1
66 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 82 if index is None:
83 if self.finished:
84 print 'Demo finished. Use reset() if you want to rerun it.'
85 return
70 86 index = self.block_index
71 87 else:
72 88 self._validate_index(index)
73 89 print marquee('<%s> block # %s (%s/%s)' %
74 90 (self.fname,index,index+1,self.nblocks))
75 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 108 def __call__(self,index=None):
78 109 """run a block of the demo.
79 110
@@ -92,13 +123,17 b' class Demo:'
92 123 try:
93 124 next_block = self.src_blocks[index]
94 125 self.block_index += 1
95 self.show(index)
96 if not self.auto:
97 print marquee('Press <q> to quit, <Enter> to execute...'),
98 ans = raw_input().strip()
99 if ans:
100 print marquee('Block NOT executed')
101 return
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)
131 if not self.auto:
132 print marquee('Press <q> to quit, <Enter> to execute...'),
133 ans = raw_input().strip()
134 if ans:
135 print marquee('Block NOT executed')
136 return
102 137
103 138 exec next_block in self.user_ns
104 139
@@ -112,4 +147,3 b' class Demo:'
112 147 print marquee(' END OF DEMO ')
113 148 print marquee('Use reset() if you want to rerun it.')
114 149 self.finished = True
115
@@ -1,5 +1,8 b''
1 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 6 * IPython/genutils.py (marquee): little utility used by the demo
4 7 code, handy in general.
5 8
General Comments 0
You need to be logged in to leave comments. Login now