##// END OF EJS Templates
ipapi: to_user_ns takes "x y", and doesn't support renaming anymore
vivainio -
Show More
@@ -207,28 +207,12 b' class IPApi:'
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
@@ -257,46 +241,31 b' class IPApi:'
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.
General Comments 0
You need to be logged in to leave comments. Login now