From b785f971a380dc638f6e1fb31c294ee31525895c 2005-12-29 17:15:38 From: fperez Date: 2005-12-29 17:15:38 Subject: [PATCH] Add Ville's patch for the new %store persistence magic. --- diff --git a/IPython/Magic.py b/IPython/Magic.py index 15f4f2a..a986545 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Magic functions for InteractiveShell. -$Id: Magic.py 966 2005-12-29 08:34:07Z fperez $""" +$Id: Magic.py 968 2005-12-29 17:15:38Z fperez $""" #***************************************************************************** # Copyright (C) 2001 Janko Hauser and @@ -30,6 +30,7 @@ import sys import re import tempfile import time +import cPickle as pickle from cStringIO import StringIO from getopt import getopt from pprint import pprint, pformat @@ -2525,6 +2526,80 @@ Defaulting color scheme to 'NoColor'""" self.shell.jobs.new(parameter_s,self.shell.user_ns) + def magic_store(self, parameter_s=''): + """Lightweight persistence for ipython variables + + Example: + + ville@badger[~]|1> A = ['hello',10,'world'] + ville@badger[~]|2> %store A + ville@badger[~]|3> Exit + + (IPython session is closed and started again...) + + ville@badger:~$ ipython -p pysh + ville@badger[~]|1> print A + + ['hello', 10, 'world'] + + Usage: + + %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 + + 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. + + Note also that the variables will need to be pickleable; most basic + python types can be safely %stored. + """ + + opts,args = self.parse_options(parameter_s,'dr',mode='list') + # delete + if opts.has_key('d'): + try: + todel = args[0] + except IndexError: + error('You must provide the variable to forget') + else: + try: + del self.shell.persist['S:' + todel] + except: + error("Can't delete variable '%s'" % todel) + # reset + elif opts.has_key('r'): + for k in self.shell.persist.keys(): + if k.startswith('S:'): + del self.shell.persist[k] + + # run without arguments -> list variables & values + elif not args: + vars = [v[2:] for v in self.shell.persist.keys() if v.startswith('S:')] + vars.sort() + if vars: + size = max(map(len,vars)) + else: + size = 0 + + fmt = '%-'+str(size)+'s -> %s' + print 'Stored variables and their in-memory values:' + for var in vars: + # print 30 first characters from every var + print fmt % (var,repr(self.shell.user_ns.get(var, ''))[:50]) + + # default action - store the variable + else: + pickled = pickle.dumps(self.shell.user_ns[args[0] ]) + self.shell.persist[ 'S:' + args[0] ] = pickled + print "Stored '%s' (%d bytes)" % (args[0], len(pickled)) + + + + + + def magic_bookmark(self, parameter_s=''): """Manage IPython's bookmark system. diff --git a/IPython/iplib.py b/IPython/iplib.py index 8cbf24f..1ceccb7 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -6,7 +6,7 @@ Requires Python 2.1 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 967 2005-12-29 09:02:13Z fperez $ +$Id: iplib.py 968 2005-12-29 17:15:38Z fperez $ """ #***************************************************************************** @@ -691,6 +691,22 @@ class InteractiveShell(Magic): self.persist = pickle.load(file(self.persist_fname)) except: self.persist = {} + + + for (key, value) in [(k[2:],v) for (k,v) in self.persist.items() if k.startswith('S:')]: + try: + obj = pickle.loads(value) + except: + + print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % key + print "The error was:",sys.exc_info()[0] + continue + + + self.user_ns[key] = obj + + + def set_hook(self,name,hook): """set_hook(name,hook) -> sets an internal IPython hook. diff --git a/doc/ChangeLog b/doc/ChangeLog index a60a1aa..05771fa 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -5,6 +5,10 @@ time, yet I was still avoiding subclassing the builtin types. No more (I'm also starting to use properties, though I won't shift to 2.3-specific features quite yet). + (magic_store): added Ville's patch for lightweight variable + persistence, after a request on the user list by Matt Wilkie + . The new %store magic's docstring has full + details. * IPython/iplib.py (InteractiveShell.post_config_initialization): changed the default logfile name from 'ipython.log' to