Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

storemagic extension enhancement #3015

Closed
wants to merge 1 commit into from

4 participants

@shuga
- List stored aliases can be displayed by %store
- Variables and aliasses can be deleted or refreshed by name - %store -d or %store -r accordingly
- %store -r also restores stored aliases
- %store -h restores %dhist
- %store -z has been replaced by %store -d
@shuga shuga storemagic extension enhancement.
    - List stored aliases can be displayed by %store
    - Variables and aliasses can be deleted or refreshed by name - %store -d or %store -r accordingly
    - %store -r also restores stored aliases
    - %store -h restores %dhist
    - %store -z has been replaced by %store -d
9861722
@Carreau
Owner

Hi,

Didn't look deeplly into the code but :

  • test are failing, cannot load store magic anymore.
  • I would at least leave -z as a -d alias (not sure why you changed -z for -d I don't have any preferences)
  • You don't need to track the _loaded state of an extension. IPython do it by himself now.

I don't use store myself, so I will wait fo other dev feedback.

@Carreau
Owner

Oh, and Thanks for the contribution ! feel free to ask if you have any question,
It's too early in the morning, I forgot my manners :-)

@takluyver takluyver commented on the diff
IPython/extensions/storemagic.py
@@ -28,33 +28,37 @@
from IPython.core.error import UsageError
from IPython.core.fakemodule import FakeModule
from IPython.core.magic import Magics, magics_class, line_magic
+from IPython.core.plugin import Plugin
@takluyver Owner

IPython.core.plugin has been removed, so this won't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@takluyver
Owner

It looks like you might have edited storemagic from an older version of IPython, and copied it over the version in master, because it reintroduces various things that had been removed (like IPython.core.plugin and the _loaded flag).

@ellisonbg
Owner

@shuga hi, I am wondering that the status of this work is? Were you planning on continuing it? Do you feel like you need further review before continuing? We like to keep our pull request queue moving. If you aren't going to work on this right for a while, we would probably close the PR - you could always reopen it if you restarted work at some later point. Let us know what you are thinking and thanks for your work. Cheers, Brian

@ellisonbg
Owner

I am going to close this PR do to inactivity. Please feel free to reopen at any time when work restarts.

@ellisonbg
Owner

Closing until work restarts. I have opened issue #3167 to track this work.

@ellisonbg ellisonbg closed this
@mshuffett mshuffett referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@mshuffett mshuffett referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 12, 2013
  1. @shuga

    storemagic extension enhancement.

    shuga authored
        - List stored aliases can be displayed by %store
        - Variables and aliasses can be deleted or refreshed by name - %store -d or %store -r accordingly
        - %store -r also restores stored aliases
        - %store -h restores %dhist
        - %store -z has been replaced by %store -d
This page is out of date. Refresh to see the latest.
Showing with 112 additions and 30 deletions.
  1. +112 −30 IPython/extensions/storemagic.py
View
142 IPython/extensions/storemagic.py
@@ -28,33 +28,37 @@
from IPython.core.error import UsageError
from IPython.core.fakemodule import FakeModule
from IPython.core.magic import Magics, magics_class, line_magic
+from IPython.core.plugin import Plugin
@takluyver Owner

IPython.core.plugin has been removed, so this won't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
from IPython.testing.skipdoctest import skip_doctest
+from IPython.utils.traitlets import Bool, Instance
#-----------------------------------------------------------------------------
# Functions and classes
#-----------------------------------------------------------------------------
-def restore_aliases(ip):
- staliases = ip.db.get('stored_aliases', {})
- for k,v in staliases.items():
+def restore_alias(ip, k, v):
+ try:
#print "restore alias",k,v # dbg
- #self.alias_table[k] = v
ip.alias_manager.define_alias(k,v)
+ except:
+ print "Unable to restore alias '%s', ignoring (use %%store -d to forget!)" % key
+ print "The error was:", sys.exc_info()[0]
-def refresh_variables(ip):
+def refresh_variable(ip, key):
db = ip.db
- for key in db.keys('autorestore/*'):
- # strip autorestore
- justkey = os.path.basename(key)
- try:
- obj = db[key]
- except KeyError:
- print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
- print "The error was:", sys.exc_info()[0]
- else:
- #print "restored",justkey,"=",obj #dbg
- ip.user_ns[justkey] = obj
+
+ # strip autorestore
+ justkey = os.path.basename(key)
+ try:
+ #print "restore variable",justkey # dbg
+ obj = db[key]
+ except:
+ print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
+ print "The error was:", sys.exc_info()[0]
+ else:
+ #print "restored",justkey,"=",obj #dbg
+ ip.user_ns[justkey] = obj
def restore_dhist(ip):
@@ -62,8 +66,11 @@ def restore_dhist(ip):
def restore_data(ip):
- refresh_variables(ip)
- restore_aliases(ip)
+ for k in ip.db.keys('autorestore/*'):
+ refresh_variable(ip, k)
+ staliases = ip.db.get('stored_aliases', {})
+ for k, v in staliases.items():
+ restore_alias(ip, k, v)
restore_dhist(ip)
@@ -78,6 +85,8 @@ class StoreMagics(Magics):
def store(self, parameter_s=''):
"""Lightweight persistence for python variables.
+ Stores variables, aliases and macros in IPython's database.
+
Example::
In [1]: l = ['hello',10,'world']
@@ -96,10 +105,14 @@ def store(self, parameter_s=''):
values
* ``%store spam`` - Store the *current* value of the variable spam
to disk
+ * ``%store -d`` - Remove all variables and aliases from storage
* ``%store -d spam`` - Remove the variable and its value from storage
- * ``%store -z`` - Remove all variables from storage
+ * ``%store -h`` - Refresh %dhist (delete current vals)
* ``%store -r`` - Refresh all variables from store (delete
current vals)
+ * ``%store -r spam`` - Refresh the variable from store (delete
+ current val)
+ * ``%store -r spam`` - Refresh the variable 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
@@ -110,30 +123,70 @@ def store(self, parameter_s=''):
python types can be safely %store'd.
Also aliases can be %store'd across sessions.
+
+ To automatically restore stored variables at startup, add this to your
+ :file:`ipython_config.py` file::
+
+ c.StoreMagic.autorestore = True
"""
- opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
+ opts,argsl = self.parse_options(parameter_s,'dhr',mode='string')
args = argsl.split(None,1)
ip = self.shell
db = ip.db
# delete
- if 'd' in opts:
+ if opts.has_key('d'):
try:
todel = args[0]
except IndexError:
- raise UsageError('You must provide the variable to forget')
+ for k in db.keys('autorestore/*'):
+ del db[k]
+
+ del db['stored_aliases']
+ print 'All variables and aliases have been deleted'
else:
try:
+ dummy = db['autorestore/' + todel]
del db['autorestore/' + todel]
+ except KeyError:
+ staliases = db.get('stored_aliases',{})
+ try:
+ dummy = staliases[todel]
+ del staliases[todel]
+ except:
+ raise UsageError("Can't delete '%s'" % todel)
+ else:
+ db['stored_aliases'] = staliases
+ print "Alias deleted '%s'" % todel
except:
raise UsageError("Can't delete variable '%s'" % todel)
+ else:
+ print "Variable deleted '%s'" % todel
# reset
- elif 'z' in opts:
- for k in db.keys('autorestore/*'):
- del db[k]
+ elif opts.has_key('h'):
+ restore_dhist(ip)
+
+ elif opts.has_key('r'):
+ try:
+ torestore = args[0]
+ except IndexError:
+ for k in db.keys('autorestore/*'):
+ refresh_variable(ip, k)
- elif 'r' in opts:
- refresh_variables(ip)
+ staliases = db.get('stored_aliases', {})
+ for k, v in staliases.items():
+ restore_alias(ip, k, v)
+
+ print 'All variables and aliases have been refreshed'
+
+ else:
+ try:
+ dummy = db['autorestore/' + torestore]
+ refresh_variable(ip, 'autorestore/' + torestore)
+ except KeyError:
+ staliases = db.get('stored_aliases',{})
+ val = staliases[torestore]
+ restore_alias(ip, torestore, val)
# run without arguments -> list variables & values
@@ -141,7 +194,7 @@ def store(self, parameter_s=''):
vars = db.keys('autorestore/*')
vars.sort()
if vars:
- size = max(map(len, vars))
+ size = max(map(lambda v: len(os.path.basename(v)), vars))
else:
size = 0
@@ -150,9 +203,20 @@ def store(self, parameter_s=''):
get = db.get
for var in vars:
justkey = os.path.basename(var)
- # print 30 first characters from every var
+ # print 50 first characters from every var
print fmt % (justkey, repr(get(var, '<unavailable>'))[:50])
+ # aliasses listing
+ staliases = db.get('stored_aliases',{})
+ asize = max(map(len, staliases.keys())) if staliases else 0
+
+ print '\nStored aliases and their in-db values:'
+ fmt = '%-'+str(asize)+'s -> %s'
+ for aname, avalue in staliases.items():
+ # print 50 first characters from every alias
+ print fmt % (aname, repr(avalue)[:50])
+
+
# default action - store the variable
else:
# %store foo >file.txt or >>file.txt
@@ -209,6 +273,24 @@ def store(self, parameter_s=''):
print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
+class StoreMagic(Plugin):
+ shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
+ autorestore = Bool(False, config=True)
+
+ def __init__(self, shell, config):
+ super(StoreMagic, self).__init__(shell=shell, config=config)
+ shell.register_magics(StoreMagics)
+
+ if self.autorestore:
+ restore_data(shell)
+
+
+_loaded = False
+
def load_ipython_extension(ip):
"""Load the extension in IPython."""
- ip.register_magics(StoreMagics)
+ global _loaded
+ if not _loaded:
+ plugin = StoreMagic(shell=ip, config=ip.config)
+ ip.plugin_manager.register_plugin('storemagic', plugin)
+ _loaded = True
Something went wrong with that request. Please try again.