diff --git a/IPython/Magic.py b/IPython/Magic.py index f45647b..fe32416 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Magic functions for InteractiveShell. -$Id: Magic.py 1089 2006-01-27 19:04:59Z vivainio $""" +$Id: Magic.py 1090 2006-01-27 21:24:05Z vivainio $""" #***************************************************************************** # Copyright (C) 2001 Janko Hauser and @@ -2665,7 +2665,9 @@ Defaulting color scheme to 'NoColor'""" %store - Show list of all variables and their current values\\ %store - Store the *current* value of the variable to disk\\ %store -d - Remove the variable and its value from storage\\ - %store -r - Remove all variables from storage + %store -r - Remove all variables from storage\\ + %store foo >a.txt - Store value of foo to new file a.txt\\ + %store foo >>a.txt - Append value of foo to file a.txt\\ It should be noted that if you change the value of a variable, you need to %store it again if you want to persist the new value. @@ -2674,7 +2676,9 @@ Defaulting color scheme to 'NoColor'""" python types can be safely %stored. """ - opts,args = self.parse_options(parameter_s,'dr',mode='list') + opts,argsl = self.parse_options(parameter_s,'dr',mode='string') + args = argsl.split(None,1) + ip = self.getapi() # delete if opts.has_key('d'): try: @@ -2711,6 +2715,29 @@ Defaulting color scheme to 'NoColor'""" # default action - store the variable else: + # %store foo >file.txt or >>file.txt + if len(args) > 1 and args[1].startswith('>'): + fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) + if args[1].startswith('>>'): + fil = open(fnam,'a') + else: + fil = open(fnam,'w') + obj = ip.ev(args[0]) + print "Writing '%s' (%s) to file '%s'." % (args[0], + obj.__class__.__name__, fnam) + + + if not isinstance (obj,basestring): + pprint(obj,fil) + else: + fil.write(obj) + if not obj.endswith('\n'): + fil.write('\n') + + fil.close() + return + + # %store foo obj = self.shell.user_ns[args[0] ] if isinstance(inspect.getmodule(obj), FakeModule): print textwrap.dedent("""\ @@ -2722,7 +2749,7 @@ Defaulting color scheme to 'NoColor'""" return pickled = pickle.dumps(obj) self.shell.persist[ 'S:' + args[0] ] = pickled - print "Stored '%s' (%d bytes)" % (args[0], len(pickled)) + print "Stored '%s' (%s, %d bytes)" % (args[0], obj.__class__.__name__,len(pickled)) def magic_bookmark(self, parameter_s=''): """Manage IPython's bookmark system. @@ -2824,7 +2851,7 @@ Defaulting color scheme to 'NoColor'""" if l ==sentinel: break lines.append(l) - block = "\n".join(lines) + block = "\n".join(lines) + '\n' #print "block:\n",block if not par: b = textwrap.dedent(block) diff --git a/IPython/iplib.py b/IPython/iplib.py index 0dc7b59..d3aa401 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -6,7 +6,7 @@ Requires Python 2.3 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 1089 2006-01-27 19:04:59Z vivainio $ +$Id: iplib.py 1090 2006-01-27 21:24:05Z vivainio $ """ #***************************************************************************** @@ -2097,6 +2097,18 @@ want to merge them back into the new files.""" % locals() # If the code compiles ok, we should handle it normally return self.handle_normal(line,continue_prompt) + def getapi(self): + """ Get an IPApi object for this shell instance + + Getting an IPApi object is always preferable to accessing the shell + directly, but this holds true especially for extensions. + + It should always be possible to implement an extension with IPApi + alone. If not, contact maintainer to request an addition. + + """ + return self.api + def handle_emacs(self,line,continue_prompt=None, pre=None,iFun=None,theRest=None): """Handle input lines marked by python-mode.""" diff --git a/doc/ChangeLog b/doc/ChangeLog index c790d26..5af7ede 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -14,6 +14,13 @@ applies for magics, 'var = %alias' assigns alias list to var. * ipapi.py: added meta() for accessing extension-usable data store. + + * iplib.py: added InteractiveShell.getapi(). New magics should be + written doing self.getapi() instead of using the shell directly. + + * Magic.py: %store now allows doing %store foo > ~/myfoo.txt and + %store foo >> ~/myfoo.txt to store variables to files (in clean + textual form, not a restorable pickle). 2006-01-25 Fernando Perez