From 4f31355aa0309e865019c8d834ff6e1a4ede9374 2005-09-24 00:59:56 From: fperez Date: 2005-09-24 00:59:56 Subject: [PATCH] # auto tag for blocks, minor pycat fix. --- diff --git a/IPython/Magic.py b/IPython/Magic.py index 1dac934..8dd0426 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Magic functions for InteractiveShell. -$Id: Magic.py 906 2005-09-24 00:26:14Z fperez $""" +$Id: Magic.py 907 2005-09-24 00:59:56Z fperez $""" #***************************************************************************** # Copyright (C) 2001 Janko Hauser and @@ -2463,6 +2463,7 @@ Defaulting color scheme to 'NoColor'""" filename = get_py_filename(parameter_s) except IndexError: warn('you must provide at least a filename.') + return fobj=open(filename,'r') source = fobj.read() fobj.close() diff --git a/IPython/demo.py b/IPython/demo.py index c32bed3..be57f65 100644 --- a/IPython/demo.py +++ b/IPython/demo.py @@ -21,7 +21,7 @@ class DemoError(exceptions.Exception): pass class Demo: def __init__(self,fname,arg_str='',mark_pause='# pause', - mark_silent='# silent',auto=False): + mark_silent='# silent',mark_auto='# auto',auto=False): """Make a new demo object. To run the demo, simply call the object. Inputs: @@ -34,48 +34,70 @@ class Demo: just like sys.argv, so the demo script can see a similar environment. - - mark_pause ('# pause'), mark_silent('# silent'): marks for pausing - (block boundaries) and to tag blocks as silent. The marks are - turned into regexps which match them as standalone in a line, with - all leading/trailing whitespace ignored. - - - auto(False): flag to run each block automatically without - confirmation. Note that silent blocks are always automatically - executed. This flag is an attribute of the object, and can be - changed at runtime simply by reassigning it. + - mark_pause ('# pause'): marks for pausing (block boundaries). The + marks are turned into regexps which match them as standalone in a + line, with all leading/trailing whitespace ignored. + + - mark_silent('# silent'): mark blocks as silent, which means that + they are executed without printing their content to screen. Silent + blocks are always automatically executed. + + - mark_auto ('# auto'): mark individual blocks as automatically + executed (without asking for confirmation). + + - auto(False): global flag to run all blocks automatically without + confirmation. This attribute overrides the block-level tags and + applies to the whole demo. It is an attribute of the object, and + can be changed at runtime simply by reassigning it to a boolean + value. """ - self.fname = fname - self.mark_pause = mark_pause - self.re_pause = re.compile(r'^\s*%s\s*$' % mark_pause,re.MULTILINE) + self.fname = fname + self.sys_argv = [fname] + shlex_split(arg_str) + self.mark_pause = mark_pause self.mark_silent = mark_silent - self.re_silent = re.compile(r'^\s*%s\s*$' % mark_silent,re.MULTILINE) - self.auto = auto - self.sys_argv = [fname]+shlex_split(arg_str) + self.re_pause = re.compile(r'^\s*%s\s*$' % mark_pause,re.MULTILINE) + self.re_silent = re.compile(r'^\s*%s\s*$' % mark_silent,re.MULTILINE) + self.re_auto = re.compile(r'^\s*%s\s*$' % mark_auto,re.MULTILINE) + self.auto = auto # get a few things from ipython. While it's a bit ugly design-wise, # it ensures that things like color scheme and the like are always in # sync with the ipython mode being used. This class is only meant to # be used inside ipython anyways, so it's OK. - self.ip_showtraceback = __IPYTHON__.showtraceback - self.ip_ns = __IPYTHON__.user_ns + self.ip_showtb = __IPYTHON__.showtraceback + self.ip_ns = __IPYTHON__.user_ns self.ip_colors = __IPYTHON__.rc['colors'] + self.colorize = Parser().format + + # load user data and initialize data structures + self.reload() + def reload(self): + """Reload source from disk and initialize state.""" # read data and parse into blocks - fobj = file(fname,'r') + fobj = file(self.fname,'r') self.src = fobj.read() fobj.close() - self.src_blocks = [b.strip() for b in self.re_pause.split(self.src) if b] - self.silent = [bool(self.re_silent.findall(b)) for b in self.src_blocks] - self.nblocks = len(self.src_blocks) + src_blocks = [b.strip() for b in self.re_pause.split(self.src) if b] + self._silent = [bool(self.re_silent.findall(b)) for b in src_blocks] + self._auto = [bool(self.re_auto.findall(b)) for b in src_blocks] + # strip out the 'auto' markers + src_b = [] + auto_strip = lambda s: self.re_auto.sub('',s) + for i,b in enumerate(src_blocks): + if self._auto[i]: + src_b.append(auto_strip(b)) + else: + src_b.append(b) + self.nblocks = len(src_b) + self.src_blocks = src_b # try to colorize blocks - colorize = Parser().format col_scheme = self.ip_colors - self.src_blocks_colored = [colorize(s_blk,'str',col_scheme) + self.src_blocks_colored = [self.colorize(s_blk,'str',col_scheme) for s_blk in self.src_blocks] - - # finish initialization + # ensure clean namespace and seek offset self.reset() def reset(self): @@ -97,10 +119,10 @@ class Demo: def seek(self,index): """Move the current seek pointer to the given block""" self._validate_index(index) - self.block_index = index-1 + self.block_index = index self.finished = False - def show_block(self,index=None): + def show(self,index=None): """Show a single block on screen""" if index is None: if self.finished: @@ -113,12 +135,12 @@ class Demo: (self.fname,index,self.nblocks-index-1)) print self.src_blocks_colored[index], - def show(self): + def show_all(self): """Show entire demo on screen, block by block""" fname = self.fname nblocks = self.nblocks - silent = self.silent + silent = self._silent for index,block in enumerate(self.src_blocks_colored): if silent[index]: print marquee('<%s> SILENT block # %s (%s remaining)' % @@ -146,12 +168,12 @@ class Demo: try: next_block = self.src_blocks[index] self.block_index += 1 - if self.silent[index]: + if self._silent[index]: print marquee('Executing silent block # %s (%s remaining)' % (index,self.nblocks-index-1)) else: - self.show_block(index) - if self.auto: + self.show(index) + if self.auto or self._auto[index]: print marquee('output') else: print marquee('Press to quit, to execute...'), @@ -167,7 +189,7 @@ class Demo: sys.argv = save_argv except: - self.ip_showtraceback(filename=self.fname) + self.ip_showtb(filename=self.fname) else: self.ip_ns.update(self.user_ns) @@ -176,3 +198,4 @@ class Demo: print marquee(' END OF DEMO ') print marquee('Use reset() if you want to rerun it.') self.finished = True + diff --git a/doc/ChangeLog b/doc/ChangeLog index 597602f..44e6353 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,5 +1,8 @@ 2005-09-23 Fernando Perez + * IPython/demo.py (Demo.__init__): added support for individually + tagging blocks for automatic execution. + * IPython/Magic.py (magic_pycat): new %pycat magic for showing syntax-highlighted python sources, requested by John.