##// END OF EJS Templates
Add docstring explaining how to enable the `storemagic` extension....
Fernando Perez -
Show More
@@ -1,178 +1,187 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 %store magic for lightweight persistence.
4 4
5 5 Stores variables, aliases etc. in PickleShare database.
6
7 To enable this functionality, run::
8
9 %load_ext storemagic
10
11 in your IPython session. If you always want it enabled, you can list it in
12 your default profile `ipython_config.py` file::
13
14 c.InteractiveShellApp.extensions = ['storemagic']
6 15 """
7 16
8 17 from IPython.core.error import TryNext, UsageError
9 18 from IPython.utils import pickleshare
10 19
11 20 import inspect,pickle,os,sys,textwrap
12 21 from IPython.core.fakemodule import FakeModule
13 22
14 23 def restore_aliases(ip):
15 24 staliases = ip.db.get('stored_aliases', {})
16 25 for k,v in staliases.items():
17 26 #print "restore alias",k,v # dbg
18 27 #self.alias_table[k] = v
19 28 ip.alias_manager.define_alias(k,v)
20 29
21 30
22 31 def refresh_variables(ip):
23 32 db = ip.db
24 33 for key in db.keys('autorestore/*'):
25 34 # strip autorestore
26 35 justkey = os.path.basename(key)
27 36 try:
28 37 obj = db[key]
29 38 except KeyError:
30 39 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
31 40 print "The error was:",sys.exc_info()[0]
32 41 else:
33 42 #print "restored",justkey,"=",obj #dbg
34 43 ip.user_ns[justkey] = obj
35 44
36 45
37 46 def restore_dhist(ip):
38 47 ip.user_ns['_dh'] = ip.db.get('dhist',[])
39 48
40 49 def restore_data(ip):
41 50 refresh_variables(ip)
42 51 restore_aliases(ip)
43 52 restore_dhist(ip)
44 53
45 54 def magic_store(self, parameter_s=''):
46 55 """Lightweight persistence for python variables.
47 56
48 57 Example:
49 58
50 59 ville@badger[~]|1> A = ['hello',10,'world']\\
51 60 ville@badger[~]|2> %store A\\
52 61 ville@badger[~]|3> Exit
53 62
54 63 (IPython session is closed and started again...)
55 64
56 65 ville@badger:~$ ipython -p pysh\\
57 66 ville@badger[~]|1> print A
58 67
59 68 ['hello', 10, 'world']
60 69
61 70 Usage:
62 71
63 72 %store - Show list of all variables and their current values\\
64 73 %store <var> - Store the *current* value of the variable to disk\\
65 74 %store -d <var> - Remove the variable and its value from storage\\
66 75 %store -z - Remove all variables from storage\\
67 76 %store -r - Refresh all variables from store (delete current vals)\\
68 77 %store foo >a.txt - Store value of foo to new file a.txt\\
69 78 %store foo >>a.txt - Append value of foo to file a.txt\\
70 79
71 80 It should be noted that if you change the value of a variable, you
72 81 need to %store it again if you want to persist the new value.
73 82
74 83 Note also that the variables will need to be pickleable; most basic
75 84 python types can be safely %stored.
76 85
77 86 Also aliases can be %store'd across sessions.
78 87 """
79 88
80 89 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
81 90 args = argsl.split(None,1)
82 91 ip = self.shell
83 92 db = ip.db
84 93 # delete
85 94 if opts.has_key('d'):
86 95 try:
87 96 todel = args[0]
88 97 except IndexError:
89 98 raise UsageError('You must provide the variable to forget')
90 99 else:
91 100 try:
92 101 del db['autorestore/' + todel]
93 102 except:
94 103 raise UsageError("Can't delete variable '%s'" % todel)
95 104 # reset
96 105 elif opts.has_key('z'):
97 106 for k in db.keys('autorestore/*'):
98 107 del db[k]
99 108
100 109 elif opts.has_key('r'):
101 110 refresh_variables(ip)
102 111
103 112
104 113 # run without arguments -> list variables & values
105 114 elif not args:
106 115 vars = self.db.keys('autorestore/*')
107 116 vars.sort()
108 117 if vars:
109 118 size = max(map(len,vars))
110 119 else:
111 120 size = 0
112 121
113 122 print 'Stored variables and their in-db values:'
114 123 fmt = '%-'+str(size)+'s -> %s'
115 124 get = db.get
116 125 for var in vars:
117 126 justkey = os.path.basename(var)
118 127 # print 30 first characters from every var
119 128 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
120 129
121 130 # default action - store the variable
122 131 else:
123 132 # %store foo >file.txt or >>file.txt
124 133 if len(args) > 1 and args[1].startswith('>'):
125 134 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
126 135 if args[1].startswith('>>'):
127 136 fil = open(fnam,'a')
128 137 else:
129 138 fil = open(fnam,'w')
130 139 obj = ip.ev(args[0])
131 140 print "Writing '%s' (%s) to file '%s'." % (args[0],
132 141 obj.__class__.__name__, fnam)
133 142
134 143
135 144 if not isinstance (obj,basestring):
136 145 from pprint import pprint
137 146 pprint(obj,fil)
138 147 else:
139 148 fil.write(obj)
140 149 if not obj.endswith('\n'):
141 150 fil.write('\n')
142 151
143 152 fil.close()
144 153 return
145 154
146 155 # %store foo
147 156 try:
148 157 obj = ip.user_ns[args[0]]
149 158 except KeyError:
150 159 # it might be an alias
151 160 # This needs to be refactored to use the new AliasManager stuff.
152 161 if args[0] in self.alias_manager:
153 162 name = args[0]
154 163 nargs, cmd = self.alias_manager.alias_table[ name ]
155 164 staliases = db.get('stored_aliases',{})
156 165 staliases[ name ] = cmd
157 166 db['stored_aliases'] = staliases
158 167 print "Alias stored: %s (%s)" % (name, cmd)
159 168 return
160 169 else:
161 170 raise UsageError("Unknown variable '%s'" % args[0])
162 171
163 172 else:
164 173 if isinstance(inspect.getmodule(obj), FakeModule):
165 174 print textwrap.dedent("""\
166 175 Warning:%s is %s
167 176 Proper storage of interactively declared classes (or instances
168 177 of those classes) is not possible! Only instances
169 178 of classes in real modules on file system can be %%store'd.
170 179 """ % (args[0], obj) )
171 180 return
172 181 #pickled = pickle.dumps(obj)
173 182 self.db[ 'autorestore/' + args[0] ] = obj
174 183 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
175 184
176 185 def load_ipython_extension(ip):
177 186 ip.define_magic('store', magic_store)
178 187 restore_data(ip)
General Comments 0
You need to be logged in to leave comments. Login now