Skip to content

Loading…

Restore pspersistence, including %store magic, as an extension. #1029

Merged
merged 1 commit into from

2 participants

@takluyver
IPython member

We've had quite a few requests to bring back the %store magic of earlier versions.

This simply restores variables when the extension is loaded, so if it's specified in a config file, it will behave the same way as before.

@fperez
IPython member

Looks great, I'll add a note in the docstring about how to enable it and will merge now. Thanks!

@fperez
IPython member

Note that I renamed the extension file to storemagic.py, which is more indicative of the provided functionality.

@fperez fperez merged commit 26afef6 into ipython:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 17 additions and 22 deletions.
  1. +1 −1 IPython/core/alias.py
  2. +16 −21 IPython/{quarantine → extensions}/pspersistence.py
View
2 IPython/core/alias.py
@@ -174,7 +174,7 @@ def validate_alias(self, name, cmd):
"because it is a keyword or builtin." % name)
if not (isinstance(cmd, basestring)):
raise InvalidAliasError("An alias command must be a string, "
- "got: %r" % name)
+ "got: %r" % cmd)
nargs = cmd.count('%s')
if nargs>0 and cmd.find('%l')>=0:
raise InvalidAliasError('The %s and %l specifiers are mutually '
View
37 IPython/quarantine/pspersistence.py → IPython/extensions/pspersistence.py
@@ -5,22 +5,18 @@
Stores variables, aliases etc. in PickleShare database.
"""
-from IPython.core import ipapi
from IPython.core.error import TryNext, UsageError
-ip = ipapi.get()
-
-import pickleshare
+from IPython.utils import pickleshare
import inspect,pickle,os,sys,textwrap
from IPython.core.fakemodule import FakeModule
-def restore_aliases(self):
- ip = self.getapi()
+def restore_aliases(ip):
staliases = ip.db.get('stored_aliases', {})
for k,v in staliases.items():
#print "restore alias",k,v # dbg
#self.alias_table[k] = v
- ip.define_alias(k,v)
+ ip.alias_manager.define_alias(k,v)
def refresh_variables(ip):
@@ -39,17 +35,12 @@ def refresh_variables(ip):
def restore_dhist(ip):
- db = ip.db
- ip.user_ns['_dh'] = db.get('dhist',[])
+ ip.user_ns['_dh'] = ip.db.get('dhist',[])
-def restore_data(self):
- ip = self.getapi()
+def restore_data(ip):
refresh_variables(ip)
- restore_aliases(self)
- restore_dhist(self)
- raise TryNext
-
-ip.set_hook('late_startup_hook', restore_data)
+ restore_aliases(ip)
+ restore_dhist(ip)
def magic_store(self, parameter_s=''):
"""Lightweight persistence for python variables.
@@ -88,7 +79,7 @@ def magic_store(self, parameter_s=''):
opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
args = argsl.split(None,1)
- ip = self.getapi()
+ ip = self.shell
db = ip.db
# delete
if opts.has_key('d'):
@@ -158,11 +149,13 @@ def magic_store(self, parameter_s=''):
except KeyError:
# it might be an alias
# This needs to be refactored to use the new AliasManager stuff.
- if args[0] in self.alias_table:
+ if args[0] in self.alias_manager:
+ name = args[0]
+ nargs, cmd = self.alias_manager.alias_table[ name ]
staliases = db.get('stored_aliases',{})
- staliases[ args[0] ] = self.alias_table[ args[0] ]
+ staliases[ name ] = cmd
db['stored_aliases'] = staliases
- print "Alias stored:", args[0], self.alias_table[ args[0] ]
+ print "Alias stored: %s (%s)" % (name, cmd)
return
else:
raise UsageError("Unknown variable '%s'" % args[0])
@@ -180,4 +173,6 @@ def magic_store(self, parameter_s=''):
self.db[ 'autorestore/' + args[0] ] = obj
print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
-ip.define_magic('store',magic_store)
+def load_ipython_extension(ip):
+ ip.define_magic('store', magic_store)
+ restore_data(ip)
Something went wrong with that request. Please try again.