~tfetherston/ipython/demoFixes

« back to all changes in this revision

Viewing changes to IPython/lib/demo.py

  • Committer: Tom Fetherston
  • Date: 2010-03-16 12:07:38 UTC
  • Revision ID: tfetherston@aol.com-20100316120738-8jzxadrsqu8svb8m
fix of demo.py, move of demo-exercizer.py.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
 
45
45
  - post_cmd(): run right after the execution of each block.  If the block
46
46
    raises an exception, this is NOT called.
47
 
    
 
47
 
48
48
 
49
49
Operation
50
50
=========
72
72
  word 'stop', to help visually distinguish the blocks in a text editor:
73
73
 
74
74
  # <demo> --- stop ---
75
 
  
 
75
 
76
76
 
77
77
# <demo> silent
78
78
 
79
79
  Make a block execute silently (and hence automatically).  Typically used in
80
80
  cases where you have some boilerplate or initialization code which you need
81
81
  executed but do not want to be seen in the demo.
82
 
  
 
82
 
83
83
# <demo> auto
84
84
 
85
85
  Make a block execute automatically, but still being printed.  Useful for
116
116
an IPython session, and type::
117
117
 
118
118
  %run demo-exercizer.py
119
 
  
 
119
 
120
120
and then follow the directions.
121
121
 
122
122
Example
151
151
print 'It is executed without asking for confirmation, but printed.'
152
152
z = x+y
153
153
 
154
 
print 'z=',x
 
154
print 'z=',z
155
155
 
156
156
# <demo> stop
157
157
# This is just another normal block.
200
200
        IPython.Demo? in IPython to see it).
201
201
 
202
202
        Inputs:
203
 
        
 
203
 
204
204
          - src is either a file, or file-like object, or a
205
205
              string that can be resolved to a filename.
206
206
 
207
207
        Optional inputs:
208
 
        
 
208
 
209
209
          - title: a string to use as the demo name.  Of most use when the demo
210
210
          you are making comes from an object that has no filename, or if you 
211
211
          want an alternate denotation distinct from the filename.
239
239
                self.title = title
240
240
        self.sys_argv = [src] + shlex.split(arg_str)
241
241
        self.auto_all = auto_all
242
 
        
 
242
 
243
243
        # get a few things from ipython.  While it's a bit ugly design-wise,
244
244
        # it ensures that things like color scheme and the like are always in
245
245
        # sync with the ipython mode being used.  This class is only meant to
246
246
        # be used inside ipython anyways,  so it's OK.
247
 
        self.ip_ns       = __IPYTHON__.user_ns
248
 
        self.ip_colorize = __IPYTHON__.pycolorize
249
 
        self.ip_showtb   = __IPYTHON__.showtraceback
250
 
        self.ip_runlines = __IPYTHON__.runlines
251
 
        self.shell       = __IPYTHON__
 
247
        gip = get_ipython()
 
248
        self.shell       = gip
 
249
        self.ip_ns       = gip.user_ns
 
250
        self.ip_colorize = gip.pycolorize
 
251
        self.ip_showtb   = gip.showtraceback
 
252
        self.ip_runlines = gip.runlines
 
253
 
252
254
 
253
255
        # load user data and initialize data structures
254
256
        self.reload()
255
257
 
256
258
    def reload(self):
257
 
        """Reload source from disk and initialize state."""
 
259
        """Reload source from its file object and initialize state."""
258
260
        # read data and parse into blocks
259
261
        self.src     = self.fobj.read()
260
262
        src_b        = [b.strip() for b in self.re_stop.split(self.src) if b]
302
304
        """Get the current block index, validating and checking status.
303
305
 
304
306
        Returns None if the demo is finished"""
305
 
        
 
307
 
306
308
        if index is None:
307
309
            if self.finished:
308
310
                print >>Term.cout, 'Demo finished.  Use <demo_name>.reset() if you want to rerun it.'
356
358
        # that the default demo.edit() call opens up the sblock we've last run
357
359
        if index>0:
358
360
            index -= 1
359
 
            
 
361
 
360
362
        filename = self.shell.mktempfile(self.src_blocks[index])
361
363
        self.shell.hooks.editor(filename,1)
362
364
        new_block = file_read(filename)
366
368
        self.block_index = index
367
369
        # call to run with the newly edited index
368
370
        self()
369
 
        
 
371
 
370
372
    def show(self,index=None):
371
373
        """Show a single block on screen"""
372
374
 
401
403
        """Execute a string with one or more lines of code"""
402
404
 
403
405
        exec source in self.user_ns
404
 
        
 
406
 
405
407
    def __call__(self,index=None):
406
408
        """run a block of the demo.
407
409
 
439
441
                self.post_cmd()
440
442
            finally:
441
443
                sys.argv = save_argv
442
 
            
 
444
 
443
445
        except:
444
446
            self.ip_showtb(filename=self.fname)
445
447
        else:
486
488
        """Execute a string with one or more lines of code"""
487
489
 
488
490
        self.shell.runlines(source)
489
 
        
 
491
 
490
492
class LineDemo(Demo):
491
493
    """Demo where each line is executed as a separate block.
492
494
 
496
498
    scripts (with no nesting or any kind of indentation) which consist of
497
499
    multiple lines of input to be executed, one at a time, as if they had been
498
500
    typed in the interactive prompt."""
499
 
    
 
501
 
500
502
    def reload(self):
501
 
        """Reload source from disk and initialize state."""
 
503
        """Reload source from the file object and initialize state."""
502
504
        # read data and parse into blocks
503
505
        src_b           = [l for l in self.fobj.readline() if l.strip()]
504
506
        nblocks         = len(src_b)
523
525
 
524
526
class ClearMixin(object):
525
527
    """Use this mixin to make Demo classes with less visual clutter.
526
 
    
 
528
 
527
529
    Demos using this mixin will clear the screen before every block and use
528
530
    blank marquees.
529
 
    
 
531
 
530
532
    Note that in order for the methods defined here to actually override those
531
533
    of the classes it's mixed with, it must go /first/ in the inheritance
532
534
    tree.  For example:
533
 
    
 
535
 
534
536
        class ClearIPDemo(ClearMixin,IPythonDemo): pass
535
 
    
 
537
 
536
538
    will provide an IPythonDemo class with the mixin's features.
537
539
    """
538
 
    
 
540
 
539
541
    def marquee(self,txt='',width=78,mark='*'):
540
542
        """Blank marquee that returns '' no matter what the input."""
541
543
        return ''
542
 
    
 
544
 
543
545
    def pre_cmd(self):
544
546
        """Method called before executing each block.
545
 
        
 
547
 
546
548
        This one simply clears the screen."""
547
549
        from IPython.utils.terminal import term_clear
548
550
        term_clear()