Show More
@@ -207,28 +207,12 b' class IPApi:' | |||
|
207 | 207 | else: |
|
208 | 208 | self.IP.runlines('\n'.join(lines)) |
|
209 | 209 | |
|
210 |
def to_user_ns(self, |
|
|
210 | def to_user_ns(self,vars): | |
|
211 | 211 | """Inject a group of variables into the IPython user namespace. |
|
212 | 212 | |
|
213 | 213 | Inputs: |
|
214 | 214 | |
|
215 | - *vars: one or more variables from the caller's namespace to be put | |
|
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 | ||
|
215 | - vars: string with variable names separated by whitespace | |
|
232 | 216 | |
|
233 | 217 | This utility routine is meant to ease interactive debugging work, |
|
234 | 218 | where you want to easily propagate some internal variable in your code |
@@ -257,46 +241,31 b' class IPApi:' | |||
|
257 | 241 | |
|
258 | 242 | # This pushes x and y to the interactive prompt immediately, even |
|
259 | 243 | # if this routine crashes on the next line after: |
|
260 |
ip.to_user_ns('x |
|
|
261 | ... | |
|
262 | # return | |
|
263 | ||
|
264 | The following example shows you how to rename variables to avoid | |
|
265 | clashes: | |
|
266 | ||
|
267 | def bar(): | |
|
244 | ip.to_user_ns('x y') | |
|
268 | 245 | ... |
|
269 | x,y,z,w = foo() | |
|
270 |
|
|
|
271 | # Push these variables with different names, so they don't | |
|
272 | # overwrite x and y from before | |
|
273 | ip.to_user_ns(('x1',x),('y1',y),('z1',z),('w1',w)) | |
|
274 | # which is more conveniently written as: | |
|
275 | ip.to_user_ns(*zip(('x1','y1','z1','w1'),(x,y,z,w))) | |
|
276 | ||
|
277 |
|
|
|
278 |
|
|
|
246 | # return | |
|
247 | ||
|
248 | If you need to rename variables, just use ip.user_ns with dict | |
|
249 | and update: | |
|
250 | ||
|
251 | # exposes variables 'foo' as 'x' and 'bar' as 'y' in IPython | |
|
252 | # user namespace | |
|
253 | ip.user_ns.update(dict(x=foo,y=bar)) | |
|
254 | ||
|
255 | """ | |
|
279 | 256 | |
|
280 | 257 | # print 'vars given:',vars # dbg |
|
281 | 258 | # Get the caller's frame to evaluate the given names in |
|
282 | 259 | cf = sys._getframe(1) |
|
283 | 260 | |
|
284 | # XXX fix this after Ville replies... | |
|
285 | 261 | user_ns = self.user_ns |
|
286 | ||
|
287 | if isinstance(vars[0],basestring): | |
|
288 | # assume that all variables are given as strings | |
|
262 | ||
|
263 | for name in vars.split(): | |
|
289 | 264 | try: |
|
290 | for name in vars: | |
|
291 | user_ns[name] = eval(name,cf.f_globals,cf.f_locals) | |
|
265 | user_ns[name] = eval(name,cf.f_globals,cf.f_locals) | |
|
292 | 266 | except: |
|
293 | 267 | error('could not get var. %s from %s' % |
|
294 |
|
|
|
295 | ||
|
296 | else: | |
|
297 | # assume they are all given as pairs of name,object | |
|
298 | user_ns.update(dict(vars)) | |
|
299 | ||
|
268 | (name,cf.f_code.co_name)) | |
|
300 | 269 | |
|
301 | 270 | def launch_new_instance(user_ns = None): |
|
302 | 271 | """ Create and start a new ipython instance. |
General Comments 0
You need to be logged in to leave comments.
Login now