##// END OF EJS Templates
ipapi: to_user_ns takes "x y", and doesn't support renaming anymore
vivainio -
Show More
@@ -1,325 +1,294 b''
1 ''' IPython customization API
1 ''' IPython customization API
2
2
3 Your one-stop module for configuring & extending ipython
3 Your one-stop module for configuring & extending ipython
4
4
5 The API will probably break when ipython 1.0 is released, but so
5 The API will probably break when ipython 1.0 is released, but so
6 will the other configuration method (rc files).
6 will the other configuration method (rc files).
7
7
8 All names prefixed by underscores are for internal use, not part
8 All names prefixed by underscores are for internal use, not part
9 of the public api.
9 of the public api.
10
10
11 Below is an example that you can just put to a module and import from ipython.
11 Below is an example that you can just put to a module and import from ipython.
12
12
13 A good practice is to install the config script below as e.g.
13 A good practice is to install the config script below as e.g.
14
14
15 ~/.ipython/my_private_conf.py
15 ~/.ipython/my_private_conf.py
16
16
17 And do
17 And do
18
18
19 import_mod my_private_conf
19 import_mod my_private_conf
20
20
21 in ~/.ipython/ipythonrc
21 in ~/.ipython/ipythonrc
22
22
23 That way the module is imported at startup and you can have all your
23 That way the module is imported at startup and you can have all your
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
25 stuff) in there.
25 stuff) in there.
26
26
27 -----------------------------------------------
27 -----------------------------------------------
28 import IPython.ipapi
28 import IPython.ipapi
29 ip = IPython.ipapi.get()
29 ip = IPython.ipapi.get()
30
30
31 def ankka_f(self, arg):
31 def ankka_f(self, arg):
32 print "Ankka",self,"says uppercase:",arg.upper()
32 print "Ankka",self,"says uppercase:",arg.upper()
33
33
34 ip.expose_magic("ankka",ankka_f)
34 ip.expose_magic("ankka",ankka_f)
35
35
36 ip.magic('alias sayhi echo "Testing, hi ok"')
36 ip.magic('alias sayhi echo "Testing, hi ok"')
37 ip.magic('alias helloworld echo "Hello world"')
37 ip.magic('alias helloworld echo "Hello world"')
38 ip.system('pwd')
38 ip.system('pwd')
39
39
40 ip.ex('import re')
40 ip.ex('import re')
41 ip.ex("""
41 ip.ex("""
42 def funcci(a,b):
42 def funcci(a,b):
43 print a+b
43 print a+b
44 print funcci(3,4)
44 print funcci(3,4)
45 """)
45 """)
46 ip.ex("funcci(348,9)")
46 ip.ex("funcci(348,9)")
47
47
48 def jed_editor(self,filename, linenum=None):
48 def jed_editor(self,filename, linenum=None):
49 print "Calling my own editor, jed ... via hook!"
49 print "Calling my own editor, jed ... via hook!"
50 import os
50 import os
51 if linenum is None: linenum = 0
51 if linenum is None: linenum = 0
52 os.system('jed +%d %s' % (linenum, filename))
52 os.system('jed +%d %s' % (linenum, filename))
53 print "exiting jed"
53 print "exiting jed"
54
54
55 ip.set_hook('editor',jed_editor)
55 ip.set_hook('editor',jed_editor)
56
56
57 o = ip.options
57 o = ip.options
58 o.autocall = 2 # FULL autocall mode
58 o.autocall = 2 # FULL autocall mode
59
59
60 print "done!"
60 print "done!"
61 '''
61 '''
62
62
63 # stdlib imports
63 # stdlib imports
64 import sys
64 import sys
65
65
66 # our own
66 # our own
67 from IPython.genutils import warn,error
67 from IPython.genutils import warn,error
68
68
69 class TryNext(Exception):
69 class TryNext(Exception):
70 """Try next hook exception.
70 """Try next hook exception.
71
71
72 Raise this in your hook function to indicate that the next hook handler
72 Raise this in your hook function to indicate that the next hook handler
73 should be used to handle the operation. If you pass arguments to the
73 should be used to handle the operation. If you pass arguments to the
74 constructor those arguments will be used by the next hook instead of the
74 constructor those arguments will be used by the next hook instead of the
75 original ones.
75 original ones.
76 """
76 """
77
77
78 def __init__(self, *args, **kwargs):
78 def __init__(self, *args, **kwargs):
79 self.args = args
79 self.args = args
80 self.kwargs = kwargs
80 self.kwargs = kwargs
81
81
82 # contains the most recently instantiated IPApi
82 # contains the most recently instantiated IPApi
83
83
84 class IPythonNotRunning:
84 class IPythonNotRunning:
85 """Dummy do-nothing class.
85 """Dummy do-nothing class.
86
86
87 Instances of this class return a dummy attribute on all accesses, which
87 Instances of this class return a dummy attribute on all accesses, which
88 can be called and warns. This makes it easier to write scripts which use
88 can be called and warns. This makes it easier to write scripts which use
89 the ipapi.get() object for informational purposes to operate both with and
89 the ipapi.get() object for informational purposes to operate both with and
90 without ipython. Obviously code which uses the ipython object for
90 without ipython. Obviously code which uses the ipython object for
91 computations will not work, but this allows a wider range of code to
91 computations will not work, but this allows a wider range of code to
92 transparently work whether ipython is being used or not."""
92 transparently work whether ipython is being used or not."""
93
93
94 def __str__(self):
94 def __str__(self):
95 return "<IPythonNotRunning>"
95 return "<IPythonNotRunning>"
96
96
97 __repr__ = __str__
97 __repr__ = __str__
98
98
99 def __getattr__(self,name):
99 def __getattr__(self,name):
100 return self.dummy
100 return self.dummy
101
101
102 def dummy(self,*args,**kw):
102 def dummy(self,*args,**kw):
103 """Dummy function, which doesn't do anything but warn."""
103 """Dummy function, which doesn't do anything but warn."""
104 warn("IPython is not running, this is a dummy no-op function")
104 warn("IPython is not running, this is a dummy no-op function")
105
105
106 _recent = None
106 _recent = None
107
107
108
108
109 def get(allow_dummy=False):
109 def get(allow_dummy=False):
110 """Get an IPApi object.
110 """Get an IPApi object.
111
111
112 If allow_dummy is true, returns an instance of IPythonNotRunning
112 If allow_dummy is true, returns an instance of IPythonNotRunning
113 instead of None if not running under IPython.
113 instead of None if not running under IPython.
114
114
115 Running this should be the first thing you do when writing extensions that
115 Running this should be the first thing you do when writing extensions that
116 can be imported as normal modules. You can then direct all the
116 can be imported as normal modules. You can then direct all the
117 configuration operations against the returned object.
117 configuration operations against the returned object.
118 """
118 """
119 global _recent
119 global _recent
120 if allow_dummy and not _recent:
120 if allow_dummy and not _recent:
121 _recent = IPythonNotRunning()
121 _recent = IPythonNotRunning()
122 return _recent
122 return _recent
123
123
124 class IPApi:
124 class IPApi:
125 """ The actual API class for configuring IPython
125 """ The actual API class for configuring IPython
126
126
127 You should do all of the IPython configuration by getting an IPApi object
127 You should do all of the IPython configuration by getting an IPApi object
128 with IPython.ipapi.get() and using the attributes and methods of the
128 with IPython.ipapi.get() and using the attributes and methods of the
129 returned object."""
129 returned object."""
130
130
131 def __init__(self,ip):
131 def __init__(self,ip):
132
132
133 # All attributes exposed here are considered to be the public API of
133 # All attributes exposed here are considered to be the public API of
134 # IPython. As needs dictate, some of these may be wrapped as
134 # IPython. As needs dictate, some of these may be wrapped as
135 # properties.
135 # properties.
136
136
137 self.magic = ip.ipmagic
137 self.magic = ip.ipmagic
138
138
139 self.system = ip.ipsystem
139 self.system = ip.ipsystem
140
140
141 self.set_hook = ip.set_hook
141 self.set_hook = ip.set_hook
142
142
143 self.set_custom_exc = ip.set_custom_exc
143 self.set_custom_exc = ip.set_custom_exc
144
144
145 self.user_ns = ip.user_ns
145 self.user_ns = ip.user_ns
146
146
147 # Session-specific data store, which can be used to store
147 # Session-specific data store, which can be used to store
148 # data that should persist through the ipython session.
148 # data that should persist through the ipython session.
149 self.meta = ip.meta
149 self.meta = ip.meta
150
150
151 # The ipython instance provided
151 # The ipython instance provided
152 self.IP = ip
152 self.IP = ip
153
153
154 global _recent
154 global _recent
155 _recent = self
155 _recent = self
156
156
157 # Use a property for some things which are added to the instance very
157 # Use a property for some things which are added to the instance very
158 # late. I don't have time right now to disentangle the initialization
158 # late. I don't have time right now to disentangle the initialization
159 # order issues, so a property lets us delay item extraction while
159 # order issues, so a property lets us delay item extraction while
160 # providing a normal attribute API.
160 # providing a normal attribute API.
161 def get_db(self):
161 def get_db(self):
162 """A handle to persistent dict-like database (a PickleShareDB object)"""
162 """A handle to persistent dict-like database (a PickleShareDB object)"""
163 return self.IP.db
163 return self.IP.db
164
164
165 db = property(get_db,None,None,get_db.__doc__)
165 db = property(get_db,None,None,get_db.__doc__)
166
166
167 def get_options(self):
167 def get_options(self):
168 """All configurable variables."""
168 """All configurable variables."""
169 return self.IP.rc
169 return self.IP.rc
170
170
171 options = property(get_options,None,None,get_options.__doc__)
171 options = property(get_options,None,None,get_options.__doc__)
172
172
173 def expose_magic(self,magicname, func):
173 def expose_magic(self,magicname, func):
174 ''' Expose own function as magic function for ipython
174 ''' Expose own function as magic function for ipython
175
175
176 def foo_impl(self,parameter_s=''):
176 def foo_impl(self,parameter_s=''):
177 """My very own magic!. (Use docstrings, IPython reads them)."""
177 """My very own magic!. (Use docstrings, IPython reads them)."""
178 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
178 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
179 print 'The self object is:',self
179 print 'The self object is:',self
180
180
181 ipapi.expose_magic("foo",foo_impl)
181 ipapi.expose_magic("foo",foo_impl)
182 '''
182 '''
183
183
184 import new
184 import new
185 im = new.instancemethod(func,self.IP, self.IP.__class__)
185 im = new.instancemethod(func,self.IP, self.IP.__class__)
186 setattr(self.IP, "magic_" + magicname, im)
186 setattr(self.IP, "magic_" + magicname, im)
187
187
188 def ex(self,cmd):
188 def ex(self,cmd):
189 """ Execute a normal python statement in user namespace """
189 """ Execute a normal python statement in user namespace """
190 exec cmd in self.user_ns
190 exec cmd in self.user_ns
191
191
192 def ev(self,expr):
192 def ev(self,expr):
193 """ Evaluate python expression expr in user namespace
193 """ Evaluate python expression expr in user namespace
194
194
195 Returns the result of evaluation"""
195 Returns the result of evaluation"""
196 return eval(expr,self.user_ns)
196 return eval(expr,self.user_ns)
197
197
198 def runlines(self,lines):
198 def runlines(self,lines):
199 """ Run the specified lines in interpreter, honoring ipython directives.
199 """ Run the specified lines in interpreter, honoring ipython directives.
200
200
201 This allows %magic and !shell escape notations.
201 This allows %magic and !shell escape notations.
202
202
203 Takes either all lines in one string or list of lines.
203 Takes either all lines in one string or list of lines.
204 """
204 """
205 if isinstance(lines,basestring):
205 if isinstance(lines,basestring):
206 self.IP.runlines(lines)
206 self.IP.runlines(lines)
207 else:
207 else:
208 self.IP.runlines('\n'.join(lines))
208 self.IP.runlines('\n'.join(lines))
209
209
210 def to_user_ns(self,*vars):
210 def to_user_ns(self,vars):
211 """Inject a group of variables into the IPython user namespace.
211 """Inject a group of variables into the IPython user namespace.
212
212
213 Inputs:
213 Inputs:
214
214
215 - *vars: one or more variables from the caller's namespace to be put
215 - vars: string with variable names separated by whitespace
216 into the interactive IPython namespace. The arguments can be given
217 in one of two forms, but ALL arguments must follow the same
218 convention (the first is checked and the rest are assumed to follow
219 it):
220
221 a) All strings, naming variables in the caller. These names are
222 evaluated in the caller's frame and put in, with the same name, in
223 the IPython namespace.
224
225 b) Pairs of (name, value), where the name is a string (a valid
226 python identifier). In this case, the value is put into the
227 IPython namespace labeled by the given name. This allows you to
228 rename your local variables so they don't collide with other names
229 you may already be using globally, or elsewhere and which you also
230 want to propagate.
231
232
216
233 This utility routine is meant to ease interactive debugging work,
217 This utility routine is meant to ease interactive debugging work,
234 where you want to easily propagate some internal variable in your code
218 where you want to easily propagate some internal variable in your code
235 up to the interactive namespace for further exploration.
219 up to the interactive namespace for further exploration.
236
220
237 When you run code via %run, globals in your script become visible at
221 When you run code via %run, globals in your script become visible at
238 the interactive prompt, but this doesn't happen for locals inside your
222 the interactive prompt, but this doesn't happen for locals inside your
239 own functions and methods. Yet when debugging, it is common to want
223 own functions and methods. Yet when debugging, it is common to want
240 to explore some internal variables further at the interactive propmt.
224 to explore some internal variables further at the interactive propmt.
241
225
242 Examples:
226 Examples:
243
227
244 To use this, you first must obtain a handle on the ipython object as
228 To use this, you first must obtain a handle on the ipython object as
245 indicated above, via:
229 indicated above, via:
246
230
247 import IPython.ipapi
231 import IPython.ipapi
248 ip = IPython.ipapi.get()
232 ip = IPython.ipapi.get()
249
233
250 Once this is done, inside a routine foo() where you want to expose
234 Once this is done, inside a routine foo() where you want to expose
251 variables x and y, you do the following:
235 variables x and y, you do the following:
252
236
253 def foo():
237 def foo():
254 ...
238 ...
255 x = your_computation()
239 x = your_computation()
256 y = something_else()
240 y = something_else()
257
241
258 # This pushes x and y to the interactive prompt immediately, even
242 # This pushes x and y to the interactive prompt immediately, even
259 # if this routine crashes on the next line after:
243 # if this routine crashes on the next line after:
260 ip.to_user_ns('x','y')
244 ip.to_user_ns('x y')
261 ...
262 # return
263
264 The following example shows you how to rename variables to avoid
265 clashes:
266
267 def bar():
268 ...
245 ...
269 x,y,z,w = foo()
246 # return
270
247
271 # Push these variables with different names, so they don't
248 If you need to rename variables, just use ip.user_ns with dict
272 # overwrite x and y from before
249 and update:
273 ip.to_user_ns(('x1',x),('y1',y),('z1',z),('w1',w))
250
274 # which is more conveniently written as:
251 # exposes variables 'foo' as 'x' and 'bar' as 'y' in IPython
275 ip.to_user_ns(*zip(('x1','y1','z1','w1'),(x,y,z,w)))
252 # user namespace
276
253 ip.user_ns.update(dict(x=foo,y=bar))
277 ...
254
278 # return """
255 """
279
256
280 # print 'vars given:',vars # dbg
257 # print 'vars given:',vars # dbg
281 # Get the caller's frame to evaluate the given names in
258 # Get the caller's frame to evaluate the given names in
282 cf = sys._getframe(1)
259 cf = sys._getframe(1)
283
260
284 # XXX fix this after Ville replies...
285 user_ns = self.user_ns
261 user_ns = self.user_ns
286
262
287 if isinstance(vars[0],basestring):
263 for name in vars.split():
288 # assume that all variables are given as strings
289 try:
264 try:
290 for name in vars:
265 user_ns[name] = eval(name,cf.f_globals,cf.f_locals)
291 user_ns[name] = eval(name,cf.f_globals,cf.f_locals)
292 except:
266 except:
293 error('could not get var. %s from %s' %
267 error('could not get var. %s from %s' %
294 (name,cf.f_code.co_name))
268 (name,cf.f_code.co_name))
295
296 else:
297 # assume they are all given as pairs of name,object
298 user_ns.update(dict(vars))
299
300
269
301 def launch_new_instance(user_ns = None):
270 def launch_new_instance(user_ns = None):
302 """ Create and start a new ipython instance.
271 """ Create and start a new ipython instance.
303
272
304 This can be called even without having an already initialized
273 This can be called even without having an already initialized
305 ipython session running.
274 ipython session running.
306
275
307 This is also used as the egg entry point for the 'ipython' script.
276 This is also used as the egg entry point for the 'ipython' script.
308
277
309 """
278 """
310 ses = create_session(user_ns)
279 ses = create_session(user_ns)
311 ses.mainloop()
280 ses.mainloop()
312
281
313
282
314 def create_session(user_ns = None):
283 def create_session(user_ns = None):
315 """ Creates, but does not launch an IPython session.
284 """ Creates, but does not launch an IPython session.
316
285
317 Later on you can call obj.mainloop() on the returned object.
286 Later on you can call obj.mainloop() on the returned object.
318
287
319 This should *not* be run when a session exists already.
288 This should *not* be run when a session exists already.
320
289
321 """
290 """
322 if user_ns is not None:
291 if user_ns is not None:
323 user_ns["__name__"] = user_ns.get("__name__",'ipy_session')
292 user_ns["__name__"] = user_ns.get("__name__",'ipy_session')
324 import IPython
293 import IPython
325 return IPython.Shell.start(user_ns = user_ns)
294 return IPython.Shell.start(user_ns = user_ns)
General Comments 0
You need to be logged in to leave comments. Login now