Skip to content
This repository

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

Merged
merged 1 commit into from over 2 years ago

2 participants

Thomas Kluyver Fernando Perez
Thomas Kluyver
Collaborator

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.

Fernando Perez
Owner

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

Fernando Perez
Owner

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

Fernando Perez fperez merged commit 26afef6 into from November 22, 2011
Fernando Perez fperez closed this November 22, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 22, 2011
Thomas Kluyver Restore pspersistence, including %store magic, as an extension. 26afef6
This page is out of date. Refresh to see the latest.
2  IPython/core/alias.py
@@ -174,7 +174,7 @@ def validate_alias(self, name, cmd):
174 174
                                     "because it is a keyword or builtin." % name)
175 175
         if not (isinstance(cmd, basestring)):
176 176
             raise InvalidAliasError("An alias command must be a string, "
177  
-                                    "got: %r" % name)
  177
+                                    "got: %r" % cmd)
178 178
         nargs = cmd.count('%s')
179 179
         if nargs>0 and cmd.find('%l')>=0:
180 180
             raise InvalidAliasError('The %s and %l specifiers are mutually '
37  IPython/quarantine/pspersistence.py → IPython/extensions/pspersistence.py
@@ -5,22 +5,18 @@
5 5
 Stores variables, aliases etc. in PickleShare database.
6 6
 """
7 7
 
8  
-from IPython.core import ipapi
9 8
 from IPython.core.error import TryNext, UsageError
10  
-ip = ipapi.get()
11  
-
12  
-import pickleshare
  9
+from IPython.utils import pickleshare
13 10
 
14 11
 import inspect,pickle,os,sys,textwrap
15 12
 from IPython.core.fakemodule import FakeModule
16 13
 
17  
-def restore_aliases(self):
18  
-    ip = self.getapi()
  14
+def restore_aliases(ip):
19 15
     staliases = ip.db.get('stored_aliases', {})
20 16
     for k,v in staliases.items():
21 17
         #print "restore alias",k,v # dbg
22 18
         #self.alias_table[k] = v
23  
-        ip.define_alias(k,v)
  19
+        ip.alias_manager.define_alias(k,v)
24 20
 
25 21
 
26 22
 def refresh_variables(ip):
@@ -39,17 +35,12 @@ def refresh_variables(ip):
39 35
 
40 36
 
41 37
 def restore_dhist(ip):
42  
-    db = ip.db
43  
-    ip.user_ns['_dh'] = db.get('dhist',[])
  38
+    ip.user_ns['_dh'] = ip.db.get('dhist',[])
44 39
 
45  
-def restore_data(self):
46  
-    ip = self.getapi()
  40
+def restore_data(ip):
47 41
     refresh_variables(ip)
48  
-    restore_aliases(self)
49  
-    restore_dhist(self)
50  
-    raise TryNext
51  
-
52  
-ip.set_hook('late_startup_hook', restore_data)
  42
+    restore_aliases(ip)
  43
+    restore_dhist(ip)
53 44
 
54 45
 def magic_store(self, parameter_s=''):
55 46
     """Lightweight persistence for python variables.
@@ -88,7 +79,7 @@ def magic_store(self, parameter_s=''):
88 79
 
89 80
     opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
90 81
     args = argsl.split(None,1)
91  
-    ip = self.getapi()
  82
+    ip = self.shell
92 83
     db = ip.db
93 84
     # delete
94 85
     if opts.has_key('d'):
@@ -158,11 +149,13 @@ def magic_store(self, parameter_s=''):
158 149
         except KeyError:
159 150
             # it might be an alias
160 151
             # This needs to be refactored to use the new AliasManager stuff.
161  
-            if args[0] in self.alias_table:
  152
+            if args[0] in self.alias_manager:
  153
+                name = args[0]
  154
+                nargs, cmd = self.alias_manager.alias_table[ name ]
162 155
                 staliases = db.get('stored_aliases',{})
163  
-                staliases[ args[0] ] = self.alias_table[ args[0] ]
  156
+                staliases[ name ] = cmd
164 157
                 db['stored_aliases'] = staliases
165  
-                print "Alias stored:", args[0], self.alias_table[ args[0] ]
  158
+                print "Alias stored: %s (%s)" % (name, cmd)
166 159
                 return
167 160
             else:
168 161
                 raise UsageError("Unknown variable '%s'" % args[0])
@@ -180,4 +173,6 @@ def magic_store(self, parameter_s=''):
180 173
             self.db[ 'autorestore/' + args[0] ] = obj
181 174
             print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
182 175
 
183  
-ip.define_magic('store',magic_store)
  176
+def load_ipython_extension(ip):
  177
+    ip.define_magic('store', magic_store)
  178
+    restore_data(ip)
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.