##// END OF EJS Templates
Deal with ansi escape codes when nbconverting to latex...
Deal with ansi escape codes when nbconverting to latex Correctly deals with "reset" codes and short codes. Interprets blinking style as bold.

File last commit:

r13353:0ca701d5
r14215:0c6c304d
Show More
storemagic.py
243 lines | 8.1 KiB | text/x-python | PythonLexer
vivainio
aliases can be %store'd
r166 # -*- coding: utf-8 -*-
"""
%store magic for lightweight persistence.
Thomas Kluyver
Update docs on %store magic.
r5543 Stores variables, aliases and macros in IPython's database.
Fernando Perez
Add docstring explaining how to enable the `storemagic` extension....
r5379
Thomas Kluyver
Update docs on %store magic.
r5543 To automatically restore stored variables at startup, add this to your
:file:`ipython_config.py` file::
Fernando Perez
Add docstring explaining how to enable the `storemagic` extension....
r5379
Thomas Kluyver
Update docs on %store magic.
r5543 c.StoreMagic.autorestore = True
vivainio
aliases can be %store'd
r166 """
Thomas Kluyver
Convert print statements to print function calls...
r13348 from __future__ import print_function
Fernando Perez
Add docstrings as per review.
r7001 #-----------------------------------------------------------------------------
# Copyright (c) 2012, The IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Stdlib
Fernando Perez
Cleanup of storemagic with pyflakes.
r6958 import inspect, os, sys, textwrap
Fernando Perez
Add docstrings as per review.
r7001 # Our own
Fernando Perez
Cleanup of storemagic with pyflakes.
r6958 from IPython.core.error import UsageError
Fernando Perez
Renamed @register_magics to @magics_class to avoid confusion....
r6973 from IPython.core.magic import Magics, magics_class, line_magic
Thomas Kluyver
Skip useless doctest for %store magic.
r5544 from IPython.testing.skipdoctest import skip_doctest
Thomas Kluyver
Restore autorestore option for storemagic....
r12331 from IPython.utils.traitlets import Bool
Thomas Kluyver
Replace references to unicode and basestring
r13353 from IPython.utils.py3compat import string_types
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165
Fernando Perez
Add docstrings as per review.
r7001 #-----------------------------------------------------------------------------
# Functions and classes
#-----------------------------------------------------------------------------
Michael Shuffett
Added enhancement to allow for specifying variable name to restore....
r10343
Thomas Kluyver
Restore pspersistence, including %store magic, as an extension.
r5378 def restore_aliases(ip):
fperez
Defaults rename, clean up api to use properties or direct access rather than...
r284 staliases = ip.db.get('stored_aliases', {})
vivainio
aliases can be %store'd
r166 for k,v in staliases.items():
#print "restore alias",k,v # dbg
vivainio
callable alias fixes
r783 #self.alias_table[k] = v
Thomas Kluyver
Restore pspersistence, including %store magic, as an extension.
r5378 ip.alias_manager.define_alias(k,v)
vivainio
aliases can be %store'd
r166
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165 def refresh_variables(ip):
fperez
Defaults rename, clean up api to use properties or direct access rather than...
r284 db = ip.db
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165 for key in db.keys('autorestore/*'):
# strip autorestore
justkey = os.path.basename(key)
try:
obj = db[key]
except KeyError:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print("Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey)
print("The error was:", sys.exc_info()[0])
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165 else:
#print "restored",justkey,"=",obj #dbg
fperez
Defaults rename, clean up api to use properties or direct access rather than...
r284 ip.user_ns[justkey] = obj
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165
vivainio
store dhist persistently in db
r713 def restore_dhist(ip):
Thomas Kluyver
Restore pspersistence, including %store magic, as an extension.
r5378 ip.user_ns['_dh'] = ip.db.get('dhist',[])
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935
Thomas Kluyver
Restore pspersistence, including %store magic, as an extension.
r5378 def restore_data(ip):
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165 refresh_variables(ip)
Thomas Kluyver
Restore pspersistence, including %store magic, as an extension.
r5378 restore_aliases(ip)
restore_dhist(ip)
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165
Fernando Perez
Update storemagic extension to new API
r6935
Fernando Perez
Renamed @register_magics to @magics_class to avoid confusion....
r6973 @magics_class
Matthias BUSSONNIER
By default, Magics inherit from Configurable
r13237 class StoreMagics(Magics):
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165 """Lightweight persistence for python variables.
Fernando Perez
Update storemagic extension to new API
r6935 Provides the %store magic."""
Thomas Kluyver
Restore autorestore option for storemagic....
r12331
autorestore = Bool(False, config=True, help=
"""If True, any %store-d variables will be automatically restored
when IPython starts.
"""
)
def __init__(self, shell):
Matthias BUSSONNIER
By default, Magics inherit from Configurable
r13237 super(StoreMagics, self).__init__(shell=shell)
Thomas Kluyver
Restore autorestore option for storemagic....
r12331 self.shell.configurables.append(self)
if self.autorestore:
restore_data(self.shell)
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 @skip_doctest
@line_magic
def store(self, parameter_s=''):
"""Lightweight persistence for python variables.
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 Example::
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 In [1]: l = ['hello',10,'world']
In [2]: %store l
In [3]: exit
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 (IPython session is closed and started again...)
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 ville@badger:~$ ipython
In [1]: l
MinRK
update %store magic docstring...
r11155 NameError: name 'l' is not defined
In [2]: %store -r
In [3]: l
Out[3]: ['hello', 10, 'world']
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 Usage:
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 * ``%store`` - Show list of all variables and their current
values
* ``%store spam`` - Store the *current* value of the variable spam
to disk
* ``%store -d spam`` - Remove the variable and its value from storage
* ``%store -z`` - Remove all variables from storage
MinRK
update %store magic docstring...
r11155 * ``%store -r`` - Refresh all variables from store (overwrite
Fernando Perez
Update storemagic extension to new API
r6935 current vals)
Michael Shuffett
Added handling of multiple args to store -r and removed error message
r10344 * ``%store -r spam bar`` - Refresh specified variables from store
(delete current val)
Fernando Perez
Update storemagic extension to new API
r6935 * ``%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
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 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.
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 Note also that the variables will need to be pickleable; most basic
python types can be safely %store'd.
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165
Fernando Perez
Update storemagic extension to new API
r6935 Also aliases can be %store'd across sessions.
"""
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
args = argsl.split(None,1)
ip = self.shell
db = ip.db
# delete
Bradley M. Froehle
2to3: Apply has_key fixer.
r7859 if 'd' in opts:
Fernando Perez
Update storemagic extension to new API
r6935 try:
todel = args[0]
except IndexError:
raise UsageError('You must provide the variable to forget')
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165 else:
Fernando Perez
Update storemagic extension to new API
r6935 try:
del db['autorestore/' + todel]
except:
raise UsageError("Can't delete variable '%s'" % todel)
# reset
Bradley M. Froehle
2to3: Apply has_key fixer.
r7859 elif 'z' in opts:
Fernando Perez
Update storemagic extension to new API
r6935 for k in db.keys('autorestore/*'):
del db[k]
Bradley M. Froehle
2to3: Apply has_key fixer.
r7859 elif 'r' in opts:
Michael Shuffett
Added enhancement to allow for specifying variable name to restore....
r10343 if args:
Michael Shuffett
Added handling of multiple args to store -r and removed error message
r10344 for arg in args:
try:
obj = db['autorestore/' + arg]
except KeyError:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print("no stored variable %s" % arg)
Michael Shuffett
Added handling of multiple args to store -r and removed error message
r10344 else:
ip.user_ns[arg] = obj
Michael Shuffett
Added enhancement to allow for specifying variable name to restore....
r10343 else:
Thomas Kluyver
%store -r restores saved aliases and directory history, as well as variables.
r11107 restore_data(ip)
Fernando Perez
Update storemagic extension to new API
r6935
# run without arguments -> list variables & values
elif not args:
Cavendish McKay
added test for %store, fixed storemagic
r8002 vars = db.keys('autorestore/*')
Fernando Perez
Update storemagic extension to new API
r6935 vars.sort()
if vars:
Fernando Perez
Small fixes as per @certik's review.
r6986 size = max(map(len, vars))
vivainio
Fix %store to avoid "%store obj.attr" half-success (and fail explicitly).
r243 else:
Fernando Perez
Update storemagic extension to new API
r6935 size = 0
Thomas Kluyver
Convert print statements to print function calls...
r13348 print('Stored variables and their in-db values:')
Fernando Perez
Update storemagic extension to new API
r6935 fmt = '%-'+str(size)+'s -> %s'
get = db.get
for var in vars:
justkey = os.path.basename(var)
# print 30 first characters from every var
Thomas Kluyver
Convert print statements to print function calls...
r13348 print(fmt % (justkey, repr(get(var, '<unavailable>'))[:50]))
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Update storemagic extension to new API
r6935 # default action - store the variable
vivainio
aliases can be %store'd
r166 else:
Fernando Perez
Update storemagic extension to new API
r6935 # %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('>>'):
Fernando Perez
Small fixes as per @certik's review.
r6986 fil = open(fnam, 'a')
Fernando Perez
Update storemagic extension to new API
r6935 else:
Fernando Perez
Small fixes as per @certik's review.
r6986 fil = open(fnam, 'w')
Fernando Perez
Update storemagic extension to new API
r6935 obj = ip.ev(args[0])
Thomas Kluyver
Convert print statements to print function calls...
r13348 print("Writing '%s' (%s) to file '%s'." % (args[0],
obj.__class__.__name__, fnam))
Fernando Perez
Update storemagic extension to new API
r6935
Thomas Kluyver
Replace references to unicode and basestring
r13353 if not isinstance (obj, string_types):
Fernando Perez
Update storemagic extension to new API
r6935 from pprint import pprint
Fernando Perez
Small fixes as per @certik's review.
r6986 pprint(obj, fil)
Fernando Perez
Update storemagic extension to new API
r6935 else:
fil.write(obj)
if not obj.endswith('\n'):
fil.write('\n')
fil.close()
vivainio
aliases can be %store'd
r166 return
Fernando Perez
Update storemagic extension to new API
r6935
# %store foo
try:
obj = ip.user_ns[args[0]]
except KeyError:
# it might be an alias
Thomas Kluyver
Allow new style aliases to be stored
r12596 name = args[0]
try:
cmd = ip.alias_manager.retrieve_alias(name)
except ValueError:
raise UsageError("Unknown variable '%s'" % name)
staliases = db.get('stored_aliases',{})
staliases[name] = cmd
db['stored_aliases'] = staliases
Thomas Kluyver
Convert print statements to print function calls...
r13348 print("Alias stored: %s (%s)" % (name, cmd))
Thomas Kluyver
Allow new style aliases to be stored
r12596 return
Fernando Perez
Update storemagic extension to new API
r6935
else:
Thomas Kluyver
Update storemagic not to use FakeModule
r12563 modname = getattr(inspect.getmodule(obj), '__name__', '')
if modname == '__main__':
Thomas Kluyver
Convert print statements to print function calls...
r13348 print(textwrap.dedent("""\
Fernando Perez
Update storemagic extension to new API
r6935 Warning:%s is %s
Proper storage of interactively declared classes (or instances
of those classes) is not possible! Only instances
of classes in real modules on file system can be %%store'd.
Thomas Kluyver
Convert print statements to print function calls...
r13348 """ % (args[0], obj) ))
Fernando Perez
Update storemagic extension to new API
r6935 return
#pickled = pickle.dumps(obj)
Cavendish McKay
added test for %store, fixed storemagic
r8002 db[ 'autorestore/' + args[0] ] = obj
Thomas Kluyver
Convert print statements to print function calls...
r13348 print("Stored '%s' (%s)" % (args[0], obj.__class__.__name__))
vivainio
Grand Persistence Overhaul, featuring PickleShare. startup...
r165
Thomas Kluyver
Use plugin API for storemagic, so autorestore is configurable.
r5541
Thomas Kluyver
Restore pspersistence, including %store magic, as an extension.
r5378 def load_ipython_extension(ip):
Thomas Kluyver
Use plugin API for storemagic, so autorestore is configurable.
r5541 """Load the extension in IPython."""
Thomas Kluyver
Extensions no longer responsible for checking if they are already loaded
r8552 ip.register_magics(StoreMagics)
Thomas Kluyver
Restore autorestore option for storemagic....
r12331