##// END OF EJS Templates
ipapi.get() only returns dummy obj when asked for
vivainio -
Show More
@@ -1,321 +1,325 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 = IPythonNotRunning()
106 _recent = None
107
107
108 def get():
108
109 def get(allow_dummy=False):
109 """Get an IPApi object.
110 """Get an IPApi object.
110
111
111 Returns an instance of IPythonNotRunning if not running under IPython.
112 If allow_dummy is true, returns an instance of IPythonNotRunning
113 instead of None if not running under IPython.
112
114
113 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
114 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
115 configuration operations against the returned object.
117 configuration operations against the returned object.
116 """
118 """
117
119 global _recent
120 if allow_dummy and not _recent:
121 _recent = IPythonNotRunning()
118 return _recent
122 return _recent
119
123
120 class IPApi:
124 class IPApi:
121 """ The actual API class for configuring IPython
125 """ The actual API class for configuring IPython
122
126
123 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
124 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
125 returned object."""
129 returned object."""
126
130
127 def __init__(self,ip):
131 def __init__(self,ip):
128
132
129 # 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
130 # IPython. As needs dictate, some of these may be wrapped as
134 # IPython. As needs dictate, some of these may be wrapped as
131 # properties.
135 # properties.
132
136
133 self.magic = ip.ipmagic
137 self.magic = ip.ipmagic
134
138
135 self.system = ip.ipsystem
139 self.system = ip.ipsystem
136
140
137 self.set_hook = ip.set_hook
141 self.set_hook = ip.set_hook
138
142
139 self.set_custom_exc = ip.set_custom_exc
143 self.set_custom_exc = ip.set_custom_exc
140
144
141 self.user_ns = ip.user_ns
145 self.user_ns = ip.user_ns
142
146
143 # Session-specific data store, which can be used to store
147 # Session-specific data store, which can be used to store
144 # data that should persist through the ipython session.
148 # data that should persist through the ipython session.
145 self.meta = ip.meta
149 self.meta = ip.meta
146
150
147 # The ipython instance provided
151 # The ipython instance provided
148 self.IP = ip
152 self.IP = ip
149
153
150 global _recent
154 global _recent
151 _recent = self
155 _recent = self
152
156
153 # 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
154 # 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
155 # order issues, so a property lets us delay item extraction while
159 # order issues, so a property lets us delay item extraction while
156 # providing a normal attribute API.
160 # providing a normal attribute API.
157 def get_db(self):
161 def get_db(self):
158 """A handle to persistent dict-like database (a PickleShareDB object)"""
162 """A handle to persistent dict-like database (a PickleShareDB object)"""
159 return self.IP.db
163 return self.IP.db
160
164
161 db = property(get_db,None,None,get_db.__doc__)
165 db = property(get_db,None,None,get_db.__doc__)
162
166
163 def get_options(self):
167 def get_options(self):
164 """All configurable variables."""
168 """All configurable variables."""
165 return self.IP.rc
169 return self.IP.rc
166
170
167 options = property(get_options,None,None,get_options.__doc__)
171 options = property(get_options,None,None,get_options.__doc__)
168
172
169 def expose_magic(self,magicname, func):
173 def expose_magic(self,magicname, func):
170 ''' Expose own function as magic function for ipython
174 ''' Expose own function as magic function for ipython
171
175
172 def foo_impl(self,parameter_s=''):
176 def foo_impl(self,parameter_s=''):
173 """My very own magic!. (Use docstrings, IPython reads them)."""
177 """My very own magic!. (Use docstrings, IPython reads them)."""
174 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
178 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
175 print 'The self object is:',self
179 print 'The self object is:',self
176
180
177 ipapi.expose_magic("foo",foo_impl)
181 ipapi.expose_magic("foo",foo_impl)
178 '''
182 '''
179
183
180 import new
184 import new
181 im = new.instancemethod(func,self.IP, self.IP.__class__)
185 im = new.instancemethod(func,self.IP, self.IP.__class__)
182 setattr(self.IP, "magic_" + magicname, im)
186 setattr(self.IP, "magic_" + magicname, im)
183
187
184 def ex(self,cmd):
188 def ex(self,cmd):
185 """ Execute a normal python statement in user namespace """
189 """ Execute a normal python statement in user namespace """
186 exec cmd in self.user_ns
190 exec cmd in self.user_ns
187
191
188 def ev(self,expr):
192 def ev(self,expr):
189 """ Evaluate python expression expr in user namespace
193 """ Evaluate python expression expr in user namespace
190
194
191 Returns the result of evaluation"""
195 Returns the result of evaluation"""
192 return eval(expr,self.user_ns)
196 return eval(expr,self.user_ns)
193
197
194 def runlines(self,lines):
198 def runlines(self,lines):
195 """ Run the specified lines in interpreter, honoring ipython directives.
199 """ Run the specified lines in interpreter, honoring ipython directives.
196
200
197 This allows %magic and !shell escape notations.
201 This allows %magic and !shell escape notations.
198
202
199 Takes either all lines in one string or list of lines.
203 Takes either all lines in one string or list of lines.
200 """
204 """
201 if isinstance(lines,basestring):
205 if isinstance(lines,basestring):
202 self.IP.runlines(lines)
206 self.IP.runlines(lines)
203 else:
207 else:
204 self.IP.runlines('\n'.join(lines))
208 self.IP.runlines('\n'.join(lines))
205
209
206 def to_user_ns(self,*vars):
210 def to_user_ns(self,*vars):
207 """Inject a group of variables into the IPython user namespace.
211 """Inject a group of variables into the IPython user namespace.
208
212
209 Inputs:
213 Inputs:
210
214
211 - *vars: one or more variables from the caller's namespace to be put
215 - *vars: one or more variables from the caller's namespace to be put
212 into the interactive IPython namespace. The arguments can be given
216 into the interactive IPython namespace. The arguments can be given
213 in one of two forms, but ALL arguments must follow the same
217 in one of two forms, but ALL arguments must follow the same
214 convention (the first is checked and the rest are assumed to follow
218 convention (the first is checked and the rest are assumed to follow
215 it):
219 it):
216
220
217 a) All strings, naming variables in the caller. These names are
221 a) All strings, naming variables in the caller. These names are
218 evaluated in the caller's frame and put in, with the same name, in
222 evaluated in the caller's frame and put in, with the same name, in
219 the IPython namespace.
223 the IPython namespace.
220
224
221 b) Pairs of (name, value), where the name is a string (a valid
225 b) Pairs of (name, value), where the name is a string (a valid
222 python identifier). In this case, the value is put into the
226 python identifier). In this case, the value is put into the
223 IPython namespace labeled by the given name. This allows you to
227 IPython namespace labeled by the given name. This allows you to
224 rename your local variables so they don't collide with other names
228 rename your local variables so they don't collide with other names
225 you may already be using globally, or elsewhere and which you also
229 you may already be using globally, or elsewhere and which you also
226 want to propagate.
230 want to propagate.
227
231
228
232
229 This utility routine is meant to ease interactive debugging work,
233 This utility routine is meant to ease interactive debugging work,
230 where you want to easily propagate some internal variable in your code
234 where you want to easily propagate some internal variable in your code
231 up to the interactive namespace for further exploration.
235 up to the interactive namespace for further exploration.
232
236
233 When you run code via %run, globals in your script become visible at
237 When you run code via %run, globals in your script become visible at
234 the interactive prompt, but this doesn't happen for locals inside your
238 the interactive prompt, but this doesn't happen for locals inside your
235 own functions and methods. Yet when debugging, it is common to want
239 own functions and methods. Yet when debugging, it is common to want
236 to explore some internal variables further at the interactive propmt.
240 to explore some internal variables further at the interactive propmt.
237
241
238 Examples:
242 Examples:
239
243
240 To use this, you first must obtain a handle on the ipython object as
244 To use this, you first must obtain a handle on the ipython object as
241 indicated above, via:
245 indicated above, via:
242
246
243 import IPython.ipapi
247 import IPython.ipapi
244 ip = IPython.ipapi.get()
248 ip = IPython.ipapi.get()
245
249
246 Once this is done, inside a routine foo() where you want to expose
250 Once this is done, inside a routine foo() where you want to expose
247 variables x and y, you do the following:
251 variables x and y, you do the following:
248
252
249 def foo():
253 def foo():
250 ...
254 ...
251 x = your_computation()
255 x = your_computation()
252 y = something_else()
256 y = something_else()
253
257
254 # This pushes x and y to the interactive prompt immediately, even
258 # This pushes x and y to the interactive prompt immediately, even
255 # if this routine crashes on the next line after:
259 # if this routine crashes on the next line after:
256 ip.to_user_ns('x','y')
260 ip.to_user_ns('x','y')
257 ...
261 ...
258 # return
262 # return
259
263
260 The following example shows you how to rename variables to avoid
264 The following example shows you how to rename variables to avoid
261 clashes:
265 clashes:
262
266
263 def bar():
267 def bar():
264 ...
268 ...
265 x,y,z,w = foo()
269 x,y,z,w = foo()
266
270
267 # Push these variables with different names, so they don't
271 # Push these variables with different names, so they don't
268 # overwrite x and y from before
272 # overwrite x and y from before
269 ip.to_user_ns(('x1',x),('y1',y),('z1',z),('w1',w))
273 ip.to_user_ns(('x1',x),('y1',y),('z1',z),('w1',w))
270 # which is more conveniently written as:
274 # which is more conveniently written as:
271 ip.to_user_ns(*zip(('x1','y1','z1','w1'),(x,y,z,w)))
275 ip.to_user_ns(*zip(('x1','y1','z1','w1'),(x,y,z,w)))
272
276
273 ...
277 ...
274 # return """
278 # return """
275
279
276 # print 'vars given:',vars # dbg
280 # print 'vars given:',vars # dbg
277 # Get the caller's frame to evaluate the given names in
281 # Get the caller's frame to evaluate the given names in
278 cf = sys._getframe(1)
282 cf = sys._getframe(1)
279
283
280 # XXX fix this after Ville replies...
284 # XXX fix this after Ville replies...
281 user_ns = self.user_ns
285 user_ns = self.user_ns
282
286
283 if isinstance(vars[0],basestring):
287 if isinstance(vars[0],basestring):
284 # assume that all variables are given as strings
288 # assume that all variables are given as strings
285 try:
289 try:
286 for name in vars:
290 for name in vars:
287 user_ns[name] = eval(name,cf.f_globals,cf.f_locals)
291 user_ns[name] = eval(name,cf.f_globals,cf.f_locals)
288 except:
292 except:
289 error('could not get var. %s from %s' %
293 error('could not get var. %s from %s' %
290 (name,cf.f_code.co_name))
294 (name,cf.f_code.co_name))
291
295
292 else:
296 else:
293 # assume they are all given as pairs of name,object
297 # assume they are all given as pairs of name,object
294 user_ns.update(dict(vars))
298 user_ns.update(dict(vars))
295
299
296
300
297 def launch_new_instance(user_ns = None):
301 def launch_new_instance(user_ns = None):
298 """ Create and start a new ipython instance.
302 """ Create and start a new ipython instance.
299
303
300 This can be called even without having an already initialized
304 This can be called even without having an already initialized
301 ipython session running.
305 ipython session running.
302
306
303 This is also used as the egg entry point for the 'ipython' script.
307 This is also used as the egg entry point for the 'ipython' script.
304
308
305 """
309 """
306 ses = create_session(user_ns)
310 ses = create_session(user_ns)
307 ses.mainloop()
311 ses.mainloop()
308
312
309
313
310 def create_session(user_ns = None):
314 def create_session(user_ns = None):
311 """ Creates, but does not launch an IPython session.
315 """ Creates, but does not launch an IPython session.
312
316
313 Later on you can call obj.mainloop() on the returned object.
317 Later on you can call obj.mainloop() on the returned object.
314
318
315 This should *not* be run when a session exists already.
319 This should *not* be run when a session exists already.
316
320
317 """
321 """
318 if user_ns is not None:
322 if user_ns is not None:
319 user_ns["__name__"] = user_ns.get("__name__",'ipy_session')
323 user_ns["__name__"] = user_ns.get("__name__",'ipy_session')
320 import IPython
324 import IPython
321 return IPython.Shell.start(user_ns = user_ns)
325 return IPython.Shell.start(user_ns = user_ns)
General Comments 0
You need to be logged in to leave comments. Login now